javascript
SpringBoot 集成 ES 7.6.2 并对字段进行中文和拼音分词处理
前言
在最近做的流媒體項目中需要集成 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 插件
谷歌瀏覽器支持 head 插件,直接使用
自己安裝,下載鏈接 https://codechina.csdn.net/mirrors/mobz/elasticsearch-head?utm_source=csdn_github_accelerator
自己安裝需要 node.js 環境,因為下載包貌似是一個前端頁面包,擦 4.node.js 下載鏈接 https://nodejs.org/en/download/
安裝 grunt,在 node.js 的目錄使用 cmd,輸入命令 npm install -g grunt -cli
進入 head 的根目錄下,cmd 輸入 npm install 對文件進行編譯
在同樣的根目錄下 cmd 輸入 grunt server 即可啟動
不安裝 grunt,可以使用 npm run start 同樣可以啟動 9.linux 后臺運行 grunt server & 或者 npm run start &
修改 es-head 默認訪問地址為 localhost:9200,可以修改_site/app.js 中修改 localhost 為目標地址
作者:超多多和劉寶寶的代碼世界
來源鏈接:
https://blog.csdn.net/qq_48329942/article/details/120717444
總結
以上是生活随笔為你收集整理的SpringBoot 集成 ES 7.6.2 并对字段进行中文和拼音分词处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 转: qemu-kvm内存管理
- 下一篇: 计算机毕业设计android的学生考勤请