前言

在最近做的流媒体项目中需要集成 ES 搜索引擎,目前 ES 最新版本为 7.x 版本,在以往的项目中我都采用的是 spring 集成的 spring-data-es, 使用自定义类集成 elasticsearchRepository 来实现 crud 操作,目前 spring 家族的更新较慢,采用 es 官网推荐的 rest-client 会有不错的效率提升


提示:以下是本篇文章正文内容,下面案例可供参考

一、为什么不用 spring 封装的 spring-data-es?

这位博主进行较为详细的说明

简单的说明:

1.springdataes采用的即将废弃的TransportClient,而TransportClient在es7版本中已经为弃用
2.spring更新速度慢,但是在springboot2.3.0后已经支持到es7版本
3.hightLevelClient虽然crud操作较为麻烦,但是在语法上更像dsl语言,也就是说可以参考dsl语言进行编程
4.rest-client有两种客户端,版本低的采用low-level-client,版本能跟上的采用high-level-client

二、springboot 集成 es 的两种方式

虽然我采用的是 es 推荐的 client,但是这里也给出 springdataes 的方式

1.spring-data-es 使用 elasticsearch

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId></dependency>

注意:springboot 在 2.3.0 才集成 es 7.x 版本,如果 springboot 是 2.2.x 版本需要手动指定 es 版本,因为 2.2.x 版本内置 es 为 6.x 版本,而 springboot 2.1.x 不建议使用 es7.x 版本,可能一些类找不到

我使用的是 springboot 2.2.5 版本,在代码中指定 es 版本

<properties><java.version>1.8</java.version><elasticsearch.version>7.6.2</elasticsearch.version></properties>

2.doc 对象的注解

代码如下(示例):

@Data
@ApiModel("ES文件存储对象")
@NoArgsConstructor
@AllArgsConstructor
@Document(indexName = "upload-file") //取消type,
public class UploadFileDoc {@Idprivate String id;@ApiModelProperty("原始文件名")@Field(type = FieldType.Text,analyzer = "ik_max_word",searchAnalyzer = "ik_max_word")private String fileName;@ApiModelProperty("文件类型")@Field(type = FieldType.Keyword)private String type;@ApiModelProperty("缩略图")private String picUrl;@ApiModelProperty(value = "创建人")@Field(type = FieldType.Keyword)private String createBy;@Field(type = FieldType.Date)private Date createTime;@Field(type = FieldType.Date)private Date updateTime;
}

在 @filed 注解中指定分词器细粒度为 ik_max_word,也可以指定 ik_smart,这种注解方式只能指定单一的分词器,而拼音分词器,可以将 ik_max_word 替换为 analyzer = "pinyin_analyzer"

在 es7.x 版本里不需要指定 type 类型,默认为_doc,在 es8 会废除 type 类型,原因是不同 type 是存储在同一个索引中,会影响 lucene 的压缩性能。

2.doc 对象的注解

@Repository
public interface UploadFileDocRepository extends ElasticsearchRepository<UploadFileDoc,String{
}

es 中的 index 索引会在程序启动的时候扫描到这个类,并对该类指定的对象上的注解进行分析,从而创建对应的索引和字段,已经字段的类型

至此,在 controller 或者其他层调用 UploadFileDocRepository 对象,进行 crud 操作


二、hightLevelClient 对 ES 进行操作

1.doc 对象

@Builder
@Data
@ApiModel("ES文件存储对象")
@NoArgsConstructor
@AllArgsConstructor
@Document(indexName = "upload-file")
@Setting(settingPath = "uploadfile_setting.json")
@Mapping(mappingPath = "uploadfile_mapping.json")
public class UploadFileDoc {@Idprivate String id;@ApiModelProperty("原始文件名")//@Field(type = FieldType.Text,analyzer = "ik_max_word",searchAnalyzer = "ik_max_word")private String fileName;@ApiModelProperty("文件类型")//@Field(type = FieldType.Keyword)private String type;@ApiModelProperty("缩略图")private String picUrl;@ApiModelProperty(value = "创建人")//@Field(type = FieldType.Keyword)private String createBy;//@Field(type = FieldType.Date)private Date createTime;//@Field(type = FieldType.Date)private Date updateTime;}

2.  中文,拼音分词器同时使用

springBoot 提供多种字段映射方式,第一种说的是使用 @Field 注解对字段进行类型分类并指定分词器,但是这种方式不满足一些特定场景,现在使用 @setting 和 @mapping 指定 json 文件来实现

直接贴代码:

uploadfile_setting.json

{"index": {"number_of_shards" : "3","number_of_replicas" : "1","analysis": {"analyzer": {"default" : {"tokenizer" : "ik_max_word"},"pinyin_analyzer": {"tokenizer": "my_pinyin"}},"tokenizer": {"my_pinyin": {"type": "pinyin",//true:支持首字母"keep_first_letter": true,//false:首字母搜索只有两个首字母相同才能命中,全拼能命中//true:任何情况全拼,首字母都能命中"keep_separate_first_letter": false,//true:支持全拼  eg: 刘德华 -[liu,de,hua]"keep_full_pinyin": true,"keep_original": true,//设置最大长度"limit_first_letter_length": 16,"lowercase": true,//重复的项将被删除,eg: 德的 -de"remove_duplicated_term": true}}}}
}

setting 文件指定分片数,默认为 5 和 1,指定默认分词器为 ik 分词器,拼音分词器为 my_pinyin,并且设置 my_pinyin 的分词属性,该文件可以在百度找到。但是建议使用 es-head 来查看索引属性获取

uploadfile_mapping.json

该文件百度上很多都是错的,可能是版本问题,很多加上的 mappings:{} 字段,也有加上 index 和 type 名的字段,在项目启动时,会报格式错误,这就是为什么我推荐大家参考 es-head 里面,如果生成不了,可以使用第一种方式来生成后进行查看

{"properties": {"createBy": {"type": "keyword"},"createTime": {"type": "date"},"type": {"type": "keyword"},"fileName": {"type": "text","analyzer": "ik_max_word","search_analyzer": "ik_max_word","fields": {"pinyin": {"type": "text","term_vector": "with_positions_offsets","analyzer": "pinyin_analyzer","boost": 10.0}}}}
}

3.  使用 client 进行 crud 操作

这篇博客写的很详细,并且例举了 dsl 对比

简单说,就是创建 request 作为参数来代入 client 方法中

比如:删除 :创建 DeleteRequest 设置参数后 restHighLevelClient.delete(request, RequestOptions.DEFAULT); 更新:创建 UpdateRequest 设置参数后 restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT); 其他高级查询,需要了解 dsl 语言来封装对象

特别提醒

ES对版本一致性要求较为严格,所以使用es7.x那么es服务器也建议采用对应的版本
1.分词器下载连接 https://github.com/medcl/elasticsearch-analysis-ik/releases
2.因为是同一哥们写的,所以ik和pinyin分词器在一起
3.pinyin分词器如果没有7.6.2版本,那么修改配置文件强行转为对应版本,否则肯定报错

安装 es head 插件

  1. 谷歌浏览器支持 head 插件,直接使用

  2. 自己安装,下载链接 https://codechina.csdn.net/mirrors/mobz/elasticsearch-head?utm_source=csdn_github_accelerator

  3. 自己安装需要 node.js 环境,因为下载包貌似是一个前端页面包,擦 4.node.js 下载链接 https://nodejs.org/en/download/

  4. 安装 grunt,在 node.js 的目录使用 cmd,输入命令 npm install -g grunt -cli

  5. 进入 head 的根目录下,cmd 输入 npm install 对文件进行编译

  6. 在同样的根目录下 cmd 输入 grunt server 即可启动

  7. 不安装 grunt,可以使用 npm run start 同样可以启动 9.linux 后台运行 grunt server & 或者 npm run start &

  8. 修改 es-head 默认访问地址为 localhost:9200,可以修改_site/app.js 中修改 localhost 为目标地址


作者:超多多和刘宝宝的代码世界

来源链接:

https://blog.csdn.net/qq_48329942/article/details/120717444

SpringBoot 集成 ES 7.6.2 并对字段进行中文和拼音分词处理相关推荐

  1. SpringBoot集成ES 7.6.2 并对字段进行中文和拼音分词处理

    文章目录 前言 一.为什么不用spring封装的spring-data-es? 二.springboot集成es的两种方式 1.spring-data-es使用elasticsearch 2.doc对 ...

  2. SpringBoot集成Es使用ElasticSearchTemplate7.x版本自动注入失败解决

    SpringBoot集成Es使用ElasticSearchTemplate7.x版本自动注入失败解决 错误: Caused by: org.springframework.beans.factory. ...

  3. Java使用Springboot集成Es官方推荐(RestHighLevelClient)

    SpringBoot集成ElasticSearch的四种方式(主要讲解ES官方推荐方式) TransportClient:这种方式即将弃用 官方将在8.0版本彻底去除 Data-Es:Spring提供 ...

  4. ElasticSearch - SpringBoot集成ES

    文章目录 ElasticSearch - SpringBoot集成ES 1.整体设计思路(仿NBA中国官网) 2.项目搭建 3.ES API的基本使用 3.1 新增球员信息 3.2 查看球员信息 3. ...

  5. SpringBoot 集成ES集群CRUD及分页解决方案

    1 SpringBoot 集成ES集群 1.2 pom <parent><groupId>org.springframework.boot</groupId>< ...

  6. 从ElasticSearch 认识到实战(SpringBoot集成ES)

    ElasticSearch 认识到实战 目录 搜索引擎介绍 ElasticSearch知识 安装 使用restful风格查询ES SpringBoot配置ES SpringBoot集成使用 一.搜索引 ...

  7. SpringBoot集成ES+京东搜索

    SpringBoot集成elasticsearch 引入依赖 <dependency><groupId>com.alibaba</groupId><artif ...

  8. springboot集成ES实现磁盘文件全文检索

    有个朋友咨询如何实现对海量磁盘资料进行目录.文件名及文件正文进行搜索,要求实现简单高效.维护方便.成本低廉.我想了想利用ES来实现文档的索引及搜索是适当的选择,于是就着手写了一些代码来实现,下面就将设 ...

  9. Springboot集成ES启动报错

    报错内容 None of the configured nodes are available elasticsearch.yml配置 cluster.name: ftest node.name: n ...

最新文章

  1. linux hdparm 测试磁盘io,hdparm测试硬盘性能
  2. python csv读取-python如何读取csv数据
  3. Java 什么叫做实例化
  4. CodeForces 864E Fire dp递推
  5. python频率_Python中的频率分析
  6. input复选框checkbox默认样式纯css修改
  7. RT-Thread 简介 https://www.rt-thread.org/
  8. 从后台获取的数据渲染到页面中的dom操作
  9. 20200516每日一句
  10. 游戏美术基础(一):游戏贴图
  11. win7计算机摄像头怎么打开,Win7笔记本摄像头怎么打开?Win7笔记本打开摄像头的方法...
  12. SpringBoot与MongoDB的集成使用
  13. 初学盲打,免费,免安装,高颜值的在线打字练习网站
  14. ubuntu查看MAC地址
  15. java-初始化数组
  16. 产业分析:短视频平台研究报告
  17. MySQL查询分析工具-Explain
  18. 算法训练 24点游戏
  19. APP移动应用测试策略与工具思维导图
  20. 光格科技将于12月6日上会:拟募资6亿元,姜明武为实控人

热门文章

  1. mysql对表中添加属性_MySQL数据库增删改字段(属性)
  2. (一)安卓修改屏幕背光默认亮度(framework修改法)
  3. excel 公式标多级目录序号
  4. PPT一次性压缩图片
  5. 人脸脸部识别技术_面部识别技术基础
  6. 怎么登录163邮箱?TOMvip邮箱登录详情介绍
  7. Linux磁盘管理基础——分区(MBR分区格式)
  8. 罗敏:趣店不存在违规催收,最多打电话提醒还款
  9. OpenMV的资料下载
  10. 手机从哈林子弹节省人力