Elasticsearch、Easy-es 快速入门 SearchAfterPage分页 若依前后端分离 Ruoyi-Vue SpringBoot

一、环境安装

Elasticsearch
ik分词器

1.1 下载解压Elasticsearch-7.x版本,越高越好,低版本有Log4j漏洞,Easy-es目前支持7.x
1.2 IK中文分词器

将对应Elasticsearch版本IK放进文件夹,Elasticsearch-7.6.1,ik对应版本就是7.6.1,不配置分词器,会将国家 解析为国、家。
在这里插入图片描述

1.3 运行Elasticsearch

在这里插入图片描述

1.4查看运行状态
http://127.0.0.1:9200/

在这里插入图片描述

1.5 修改密码(正式环境)

Elasticsearch7.x——设置用户密码认证

Multi Elasticsearch Head密码连接

http://127.0.0.1:9200/?auth_user=elastic&auth_password=xxx
1.6 使用可视化连接工具

需求简单,我用谷歌浏览器插件足够了 Multi Elasticsearch Head

在这里插入图片描述

二、可视化工具检索数据

2.1 查看表数据

在这里插入图片描述

2.2 查询数据

查询 标题包含中国通史的数据
在这里插入图片描述

三、Easy-es

easy-es官网

3.1 pom文件
        <dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>easy-es支持版本</version></dependency><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-client</artifactId><version>ES版本</version></dependency><dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>ES版本</version></dependency><dependency><groupId>org.dromara.easy-es</groupId><artifactId>easy-es-boot-starter</artifactId><version>easy-es支持版本</version></dependency>
3.2 springboot配置
easy-es:enable: true #默认为true,若为false则认为不启用本框架address : 60.205.186.155:9200 # es的连接地址,必须含端口 若为集群,则可以用逗号隔开 例如:127.0.0.1:9200,127.0.0.2:9200username: elastic #若无 则可省略此行配置password:  #若无 则可省略此行配置
3.4 实体类
package com.huida.framework.es.domain;import lombok.Data;
import org.dromara.easyes.annotation.IndexField;
import org.dromara.easyes.annotation.IndexId;
import org.dromara.easyes.annotation.IndexName;
import org.dromara.easyes.annotation.rely.Analyzer;
import org.dromara.easyes.annotation.rely.FieldType;
import org.dromara.easyes.annotation.rely.IdType;/*** titleContent*/
@Data
@IndexName("title_content")
public class EsTitleContent {/*** es中的唯一id*/@IndexId(type = IdType.UUID)private String id;/*** mysql主键*/private String businessId;/*** mysql表名*/private String tableName;/*** 跳转页面*/private String pages;/*** 标题*/@IndexField(fieldType = FieldType.TEXT, analyzer = Analyzer.IK_SMART, searchAnalyzer = Analyzer.IK_MAX_WORD)private String title;/*** 内容*/@IndexField(fieldType = FieldType.TEXT, analyzer = Analyzer.IK_SMART, searchAnalyzer = Analyzer.IK_MAX_WORD)private String content;}
3.5 mapper
package com.huida.framework.es.mapper;import com.huida.framework.es.domain.EsTitleContent;
import org.dromara.easyes.core.core.BaseEsMapper;/*** Mapper* <p>* Copyright © 2021 xpc1024 All Rights Reserved**/
public interface EsTitleContentMapper extends BaseEsMapper<EsTitleContent> {}
3.6 springboot包扫描
@EsMapperScan("com.huida.es.mapper")
3.7 查询实体
package com.huida.framework.es.domain;import lombok.Data;
import lombok.NonNull;import java.util.List;@Data
public class EsTitleContentQuery {/*** 每页的数量*/private Integer pageSize;/*** 总页数*/private Integer pageNum;/*** 下一页sort*/private List<Object> nextSearchAfter;/*** 搜索关键字*/private String keyword;}
3.8 业务实现代码,查询,初始化数据
    public void selectTrainingCourseToEs() {List<TrainingCourse> trainingCourseList = trainingCourseService.selectTrainingCourseToEs();List<EsTitleContent> esTitleContentList = new ArrayList<>(trainingCourseList.size() * 2);EsTitleContent item = null;for (TrainingCourse trainingCourse : trainingCourseList) {item = new EsTitleContent();item.setSystemType(trainingCourse.getSystemType());item.setTitle(trainingCourse.getClassName());if (StringUtils.isNotBlank(trainingCourse.getContent())) {//用hutool工具类会保存(空格字符 &nbsp; &ensp; &emsp;item.setContent(Jsoup.parse(trainingCourse.getContent()).text());}item.setBusinessId(trainingCourse.getRecordId());item.setTableName("training_course_new");if (StringUtils.equals(trainingCourse.getType(), "1")) {item.setModule("同心培训");} else {item.setModule("同心活动");}item.setDataJsonStr(JSON.toJSONString(trainingCourse));esTitleContentList.add(item);}batchInsert(esTitleContentList);}public boolean createIndex() {// 1.初始化-> 创建索引(相当于mysql中的表)return esTitleContentMapper.createIndex();}public void deleteAllData() {Wrapper<EsTitleContent> esTitleContentWrapper = new LambdaEsQueryWrapper<>();esTitleContentMapper.delete(esTitleContentWrapper);}public void batchInsert(List<EsTitleContent> list) {if (CollectionUtil.isNotEmpty(list)){esTitleContentMapper.insertBatch(list);}}public SAPageInfo<EsTitleContent> search(EsTitleContentQuery esTitleContentQuery) {LambdaEsQueryWrapper<EsTitleContent> wrapper = new LambdaEsQueryWrapper<>();wrapper.sortByScore(SortOrder.DESC);wrapper.orderByDesc(EsTitleContent::getId);
//        wrapper.eq(EsTitleContent::getSystemType, "bgtx");//SQL搜索if (StringUtils.isNotBlank(esTitleContentQuery.getKeyword())) {wrapper.and(i -> i.match(EsTitleContent::getTitle, esTitleContentQuery.getKeyword(), 10f).or().match(EsTitleContent::getContent, esTitleContentQuery.getKeyword(), 5f));return esTitleContentMapper.searchAfterPage(wrapper, esTitleContentQuery.getNextSearchAfter(), esTitleContentQuery.getPageSize());}return new SAPageInfo<>();}
3.9 前端调用查询
{"pageNum": 1,"pageSize": 10,"keyword": "2024","nextSearchAfter": null
}// {
//     "pageNum": 2,
//     "pageSize": 10,
//     "keyword": "2024",
//     "nextSearchAfter": [
//             "3b5332ed-7a9b-4ee5-b31a-1c5eadbd519a"
//         ]
// }

四、前端搜索组件

在这里插入图片描述

<template><view class="container"><view class="search-header"><view class="input-box"><view class="cuIcon-search"></view><input name="input" class="keyword" :focus="true" v-model="queryParams.keyword"@input="inputChange" @focus="inputFocus" @confirm="onKeywordConfirm":placeholder="defaultKeyword.keyword" confirm-type="search"/><text v-if="queryParams.keyword" class="cuIcon-close del" @tap="clearKeyword"></text></view><view class="right" @tap="closeSearch">取消</view></view><view class="search-result" v-if="searchResultOpen && searchResultList"><u-cell-group><view class="result-box"><view class="result-item" v-for="item in searchResultList" :key="item.id" @click="clickResult(item)"><text class="train-type">{{ item.module }}</text><view class="title u-line-1" v-html="item.title"></view><view v-html="item.content"></view></view></view></u-cell-group></view><view class="search-result-empty" v-if="searchResultOpen  && searchResultList.length===0"><image class="icon" :src="`${TrainFileBasUrl}/app_resource/portal/static/images/zgt/search.png`"></image><text class="text">换个关键词试试</text></view><view class="padding-tb" v-if="searchResultOpen && searchResultList.length>0"><u-loadmore :status="loadStatus" :load-text="loadText"/></view></view>
</template><script>
import tools from '@/commons/tools.js';export default {data() {return {queryParams: {keyword: '',pageNum: 1,pageSize: 10,nextSearchAfter: null,hasData: true},//搜索结果展示searchResultOpen: false,//搜索结果searchResultList: [],//帮助搜索词helpKeyword: [],//历史搜索词historyKeyword: [],//默认搜索词defaultKeyword: '',//热门搜索词hotKeyword: [],//过滤搜索结果categoryFilter: false,filterCategory: [],//排序字段currentSortType: 'default',//排序方式currentSortOrder: 'desc',loadStatus: 'loadmore',loadText: {loadmore: '加载更多',loading: '正在加载...',nomore: '没有更多数据了'},}},onReachBottom() {if (this.queryParams.hasData) {this.queryParams.pageNum += 1this.getSearchResultList()}},onShow() {tools.checkSession()},methods: {clickResult: function (item) {if (item.tableName === 'tz_ly_book') {tools.openPage('/pagesBook/pages/book/view', item.title, {id: item.businessId});}},closeSearch: function () {tools.back()},clearKeyword: function () {this.queryParams = {keyword: '',pageNum: 1,pageSize: 10,nextSearchAfter: null,hasData: true}this.searchResultOpen = falsethis.searchResultList = []},inputFocus: function () {this.searchResultOpen = falsethis.searchResultList = []if (this.keyword) {this.getHelpKeyword();}},inputChange: function (e) {this.keyword = e.detail.valuethis.searchResultOpen = falsethis.getHelpKeyword();},onKeywordConfirm(event) {this.getSearchResult(event.detail.value);},getSearchResult(keyword) {this.keyword = keywordthis.queryParams.pageNum = 1this.queryParams.nextSearchAfter = nullthis.getSearchResultList();},getSearchResultList() {this.loadStatus = 'loading';tools.post(tools.Server + '/es/titleContent/search', this.queryParams, {isPostData: true,showLoading: false}).then((res) => {if (res.success) {if (this.queryParams.pageNum < res.data.pages) {this.loadStatus = 'loadmore';this.queryParams.hasData = true;this.queryParams.nextSearchAfter = res.data.nextSearchAfter} else {this.loadStatus = 'nomore';this.queryParams.hasData = false;}if (res.data.list) {if (1 === this.queryParams.pageNum) {this.searchResultList = res.data.list} else {res.data.list.forEach(item => {this.searchResultList.push(item)})}}this.searchResultOpen = true}})},getSearchKeyword() {// let that = this;// util.request(api.SearchIndex).then(function(res) {//   if (res.errno === 0) {// that.historyKeyword = res.data.historyKeywordList// that.defaultKeyword = res.data.defaultKeyword// that.hotKeyword = res.data.hotKeywordList//   }// });},getHelpKeyword: function () {let that = this;// util.request(api.SearchHelper, {//   keyword: that.keyword// }).then(function(res) {//   if (res.errno === 0) {//     that.helpKeyword = res.data;//   }// });},},onLoad: function () {this.getSearchKeyword();}
}
</script><style lang="scss">
page {min-height: 100%;background-color: #f4f4f4;
}.container {min-height: 100%;background-color: #f4f4f4;
}.search-header {position: fixed;top: 44px;width: 750rpx;height: 91rpx;display: flex;background: #fff;border-bottom: 1px solid rgba(0, 0, 0, .15);padding: 0 31.25rpx;font-size: 29rpx;color: #333;
}.search-header .input-box {position: relative;margin-top: 16rpx;float: left;width: 0;flex: 1;height: 59rpx;line-height: 59rpx;padding: 0 20rpx;background: #f4f4f4;
}.search-header .icon {position: absolute;top: 14rpx;left: 20rpx;width: 31rpx;height: 31rpx;
}.search-header .del {position: absolute;top: 3rpx;right: 10rpx;width: 53rpx;height: 53rpx;z-index: 10;
}.search-header .keyword {position: absolute;top: 0;left: 40rpx;width: 506rpx;height: 59rpx;padding-left: 30rpx;
}.search-header .right {margin-top: 24rpx;margin-left: 31rpx;margin-right: 6rpx;width: 58rpx;height: 43rpx;line-height: 43rpx;float: right;
}.search-result {margin-top: 90rpx;
}.no-search {height: auto;overflow: hidden;margin-top: 91rpx;
}.serach-keywords {background: #fff;width: 750rpx;height: auto;overflow: hidden;margin-bottom: 20rpx;
}.serach-keywords .h {padding: 0 31.25rpx;height: 93rpx;line-height: 93rpx;width: 100%;color: #999;font-size: 29rpx;
}.serach-keywords .title {display: block;width: 120rpx;float: left;
}.serach-keywords .icon {margin-top: 19rpx;float: right;display: block;margin-left: 511rpx;height: 55rpx;width: 55rpx;
}.serach-keywords .b {width: 750rpx;height: auto;overflow: hidden;padding-left: 31.25rpx;
}.serach-keywords .item {display: inline-block;width: auto;height: 48rpx;line-height: 48rpx;padding: 0 15rpx;border: 1px solid #999;margin: 0 31.25rpx 31.25rpx 0;font-size: 24rpx;color: #333;
}.serach-keywords .item.active {color: #b4282d;border: 1px solid #b4282d;
}.shelper-list {width: 750rpx;height: auto;overflow: hidden;background: #fff;padding: 0 31.25rpx;
}.shelper-list .item {height: 93rpx;width: 687.5rpx;line-height: 93rpx;font-size: 24rpx;color: #333;border-bottom: 1px solid #f4f4f4;
}.search-result-empty {width: 100%;height: 100%;padding-top: 300rpx;
}.search-result-empty .icon {margin: 0 auto;display: block;width: 240rpx;height: 240rpx;
}.search-result-empty .text {display: block;width: 100%;height: 40rpx;font-size: 28rpx;text-align: center;color: #999;
}.result-box {padding-bottom: 1px;.result-item {margin: 10px 12px;background-color: #f1f1f1;border-radius: 8px;padding: 10px;position: relative;overflow: hidden;.train-type {color: #F96345;border: 1px solid #F96345;border-radius: 4rpx;padding: 2px 5px;}.status-name {background-color: #FBB653;position: absolute;z-index: 1;top: 0px;right: 0px;color: #fff;font-size: 12px;padding: 4px 10px 3px;border-bottom-left-radius: 8px;}.title {color: #000;font-weight: 600;// font-size: 16px;padding: 8px 0;}.org,.date-time {// font-size: 14px;color: #878787;padding: 3px 0;}.state {position: absolute;right: 0;top: 40%;padding: 5px 15px;border-radius: 15px 0 0 15px;// color: #FBB653;// background-color: #FFF9F0;}.data-info {display: flex;justify-content: space-between;align-items: center;margin-top: 6px;.sign-up {background-color: #F1F1F1;padding: 4px 5px;// font-size: 13px;color: #97a2a4;border-radius: 3px;.number {color: #cb3f48;margin-left: 5px;}}.btn-box {display: flex;justify-content: flex-end;.cu-btn {margin-left: 10px;}}}}
}
</style>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/403605.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Postgres 超时 (Timeout) 详解

原文地址 https://www.bytebase.com/blog/postgres-timeout/ PostgreSQL 提供各种超时 (Timeout) 设置&#xff0c;通过控制某些进程的持续时间来帮助管理和优化数据库操作。这些超时对于确保系统的稳定性和性能至关重要&#xff0c;尤其是在高流量或复杂查询的环境中。让我们…

STM32CubeMX生成stm32MP135中断优先级配置错误修正方法

0 修改方法 使用STM32CubeMX生成stm32MP135代码的中断优先级配置错误&#xff0c;将导致所有中断优先级设置不对。 如果设置EXTI0中断优先级为10&#xff0c;在STM32CubeMX中配置如下&#xff1a; 生成的中断优先级配置代码为&#xff1a; 正确写法应该将中断优先级左移3位&…

python从入门到精通:函数

目录 1、函数介绍 2、函数的定义 3、函数的传入参数 4、函数的返回值 5、函数说明文档 6、函数的嵌套调用 7、变量的作用域 1、函数介绍 函数是组织好的&#xff0c;可重复使用的&#xff0c;用来实现特定功能的代码段。 name "zhangsan"; length len(nam…

二叉树学习笔记

一、树的概念 树是一种非线性的结构&#xff0c;它是由n个有限结点组成的一个具层次关系的集合。&#xff08;像一颗倒着的树&#xff09; 特点&#xff1a; 有一个特殊的结点&#xff0c;称之为根结点&#xff0c;根结点没有前驱结点 除了根节点以外&#xff0c;其余节点别分…

centos 7.9 迁移到 openEuler22.03-LTS-SP3

openEuler移植案例 | 移植操作指南 | openEuler社区官网 cat /etc/redhat-release CentOS Linux release 7.9.2009 (Core) 需要两台机器&#xff0c; 不通过原因 在待升级节点检查是否有安装x2openEuler-core时, 发现已经安装了,不能作为升级节点。该节点为&#xff1a; 解…

MySQL中处理JSON数据

​ 大家好&#xff0c;我是程序员小羊&#xff01; 前言 在大数据时代&#xff0c;处理和分析结构化与非结构化数据的能力对于企业的成功至关重要。MySQL作为一种广泛使用的关系型数据库管理系统&#xff08;RDBMS&#xff09;&#xff0c;在应对传统结构化数据方面表现出色。然…

c++每日练习记录第1天

笔记&#xff1a; C 中&#xff0c;isalnum 函数用于检查一个字符是否是字母数字字符&#xff0c;isalnum 函数定义在 头文件中。双指针法&#xff0c;双指针法是一种常用的算法技巧&#xff0c;特别适用于处理数组、字符串等线性数据结构中的问题。这种方法通常涉及到两个指针…

12、springboot3 vue3开发平台-前端-记住我功能实现

文章目录 1. 前端用户信息保存2. 登录页面添加3. 后端实现 1. 前端用户信息保存 使用pinia持久化保存用户名密码 src/stores/remember-me.js // 定义 store import { defineStore } from "pinia" import {reactive} from vueexport const useRememberMeStore defi…

图书管理管理系统 (GUI)

目录 Java程序设计课程设计 图书管理管理系统 一、前言 1 研究背景 2 目的和意义 3编程环境与工具 二、图书管理系统概述 1主要业务流程 三、需求分析与设计 1 系统需求分析 2.功能需求 3.性能需求 4. 安全需求 2 数据库设计 3 界面设计 四、 总…

Solidworks二次开发:通过XYZ点的曲线

在SolidWorks中&#xff0c;通过XYZ点创建曲线是一种根据一组点的坐标生成三维曲线的方法。这种方法适用于需要根据特定点集设计曲线的情况&#xff0c;比如在建模复杂几何形状或执行逆向工程时。在SolidWorks中通过XYZ点创建曲线&#xff0c;操作步骤如下 打开SolidWorks并新建…

利用modelscope下载模型

1. modelscope的简介 ModelScope作为一个先进的“模型即服务”(MaaS)平台&#xff0c;它的核心在于汇聚人工智能领域的尖端模型&#xff0c;降低了在现实世界应用这些前沿技术的门槛。该平台通过ModelScope库展现了其强大功能&#xff0c;这一库专为简化开发者体验而设计&…

Element-06.案例

一.目标 实现下面这个页面&#xff0c;表格中的数据使用axois异步加载数据 二.实现步骤 首先在vue项目的views文件夹中新建一个tlias文件夹&#xff0c;用来存储该案例的相关组件。员工页面组件&#xff08;EmpView.vue&#xff09;和部门页面组件&#xff08;DeptView.vue&…

C语言指针详解-上

C语言指针详解-上 前言1.指针的基本概念1.1指针是什么1.2指针的声明与初始化1.3取地址符&和解引用符*& 运算符用于**获取变量的地址*** 运算符用于访问指针指向的值 2.指针的类型常见数据类型的指针指针与数组、字符串数组指针结构体指针函数指针二级指针void指针 3.指…

【数据结构】二叉树(二)遍历

上篇已经了解对二叉树有了大概了解&#xff0c;本篇学习二叉树的前序、中序、后序及层序遍历的递归与非递归共7种遍历方法&#xff0c;快收藏吧~ 目录 1、前序遍历 递归方式&#xff1a; 迭代方式&#xff1a; 2、中序遍历 递归方式&#xff1a; 迭代方式&#xff1a; …

XXX【4】策略模式

如上图所示&#xff0c;如果要加入一个新的货币&#xff0c;那么就需要对类中的Calculate函数进行修改&#xff0c;这违背了封闭开放原则。 上图中的方式更加合适&#xff0c;搞一个抽象类&#xff08;方法中可以用多态调用&#xff09;&#xff0c;然后每个货币自己是一个类&a…

每日学习笔记:C++ STL之堆栈容器stack

目录 stack定义 核心接口 stack class声明 stack class定义 用户自定义的Stack Class C11特色的插入元素的新形式 运用实例 stack定义 核心接口 stack class声明 stack class定义 用户自定义的Stack Class C11特色的插入元素的新形式 运用实例

数据结构(邓俊辉)学习笔记】优先级队列 07——堆排序

1.算法 作为完全二叉堆的一个应用&#xff0c;这节来介绍堆排序算法。 是的&#xff0c;谈到优先级队列&#xff0c;我们很自然地就会联想到排序。因为就其功能而言&#xff0c;包括完全二叉堆在内的任何一种优先级队列都天生地具有选取功能&#xff0c;也就是选取其中的最大…

【mkdir rmdir】Centos/Linux mkdir rmdir命令详细介绍

【mkdir & rmdir】Centos/Linux mkdir & rmdir命令详细介绍 简介 mkdir rmdir 简介 mkdir 命令和 rmdir 命令是在 linux 当中比较常用的两个命令&#xff0c;这两个命令前者是创建空目录&#xff0c;后者是删除空目录。rmdir 命令的定位比较尴尬它的功能可以被 rm 命…

“论面向服务架构设计及其应用”写作框架,软考高级,系统架构设计师

论文真题 面向服务架构&#xff08;Service-Oriented Architecture, SOA&#xff09; 是一种应用框架&#xff0c;将日常的业务应用划分为单独的业务功能服务和流程&#xff0c;通过采用良好定义的接口和标准协议将这些服务关联起来。通过实施基于SOA的系统架构&#xff0c;用…

版本更新 《坚持学习计时器》软件V3.1 更新内容:自动实时显出

&#x1f31f; 嗨&#xff0c;我是命运之光&#xff01; &#x1f30d; 2024&#xff0c;每日百字&#xff0c;记录时光&#xff0c;感谢有你一路同行。 &#x1f680; 携手启航&#xff0c;探索未知&#xff0c;激发潜能&#xff0c;每一步都意义非凡。 版本更新 《坚持学习…