springboot集成ElasticSearch使用completion实现补全功能

news/2025/1/10 15:28:15/文章来源:https://www.cnblogs.com/bigcat26/p/18522486

@

目录
  • 摘要
  • springboot代码
    • 依赖
    • 代码
  • kibana代码
    • 第一部分:设置index、type、mapping
    • 第二部分:批量插入
    • 第三部分:执行
    • 第四部分:结果展示
  • 本人先关其他文章链接

摘要

所谓自动补全功能就是“百度搜索框”中每敲下一个字符下面的提示框就会动态改变提示的功能,就是下面的效果:↓

说明:使用RestHighLevelClient 即可实现输入框补全功能

  • springboot代码
  • kibana代码

springboot代码

依赖

<!--ES-->
<dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>6.8.12</version><exclusions><exclusion><artifactId>elasticsearch</artifactId><groupId>org.elasticsearch</groupId></exclusion></exclusions>
</dependency>
<dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>6.8.12</version>
</dependency>

代码

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.search.suggest.Suggest;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder;
@Autowired
private RestHighLevelClient client;
public static final int NINE = 9;
public static final int TEN = 10;/***  输入框自动补全提示功能(ElasticSearch使用completion实现补全功能)* @param request request* @param suggestValue 输入参数* @author liudz* @date 2021/4/19* @return 执行结果**/@GetMapping(value = "/completion")public Response<List<String>> getSuggestCompletion(@RequestParam String suggestValue, HttpServletRequest request) {log.info("--getSearchSuggest--begin--");Long userId = Long.valueOf(request.getUserPrincipal().getName());//指定在哪个字段搜索String suggestField = "region";SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();//suggestField为指定在哪个字段搜索,suggestValue为输入内容,TEN为10,代表输出显示最大条数CompletionSuggestionBuilder suggestionBuilderDistrict =new CompletionSuggestionBuilder(suggestField).prefix(suggestValue).size(TEN);SuggestBuilder suggestBuilder = new SuggestBuilder();suggestBuilder.addSuggestion("my_suggest", suggestionBuilderDistrict);searchSourceBuilder.suggest(suggestBuilder);SearchRequest searchRequest = new SearchRequest(userId + esIndex);searchRequest.types(esType);searchRequest.source(searchSourceBuilder);SearchResponse response = null;try {response = client.search(searchRequest, RequestOptions.DEFAULT);} catch (IOException e) {log.error("IOException:{}", e);}Suggest suggest = response.getSuggest();List<String> keywords = null;if (suggest != null) {keywords = new ArrayList<>();List<? extends Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option>> entries =suggest.getSuggestion("my_suggest").getEntries();for (Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option> entry: entries) {for (Suggest.Suggestion.Entry.Option option: entry.getOptions()) {String keyword = option.getText().string();if (!StringUtils.isEmpty(keyword)) {if (keywords.contains(keyword)) {continue;}keywords.add(keyword);if (keywords.size() >= NINE) {break;}}}}}return Response.success(keywords);}

kibana代码

第一部分:设置index、type、mapping

说明:设置某个字段"type": "completion"即可

PUT 12_assets_directory_v1/
{"index_patterns": "test-logs-*","settings": {"number_of_shards": 5,"number_of_replicas": 1,"analysis": {"analyzer": {"my_analyzer": {"type": "pattern","pattern":["_","-"]}}}},"mappings": {"_doc":{"properties": {"file_name": {"type": "text","copy_to": "region"},"file_type": {"type": "keyword"},"database_name": {"type": "text","analyzer": "ik_max_word","copy_to": "region"},"table_name": {"type": "text","analyzer": "ik_max_word","copy_to": "region"},"include_fields": {"type": "text","index": false},"source_business": {"type": "integer","index": false},"store_type": {"type": "text","index": false},"whether_online": {"type": "byte","index": false},"foreign_id": {"type": "integer","index": false},"update_time": {"type": "long","index": false},"region": {"type": "completion","analyzer": "ik_max_word"}}}}
}

第二部分:批量插入

批量插入每个json必须单独存在一行

POST _bulk/?refresh=true
{ "index" : { "_index" : "12_assets_directory_v1","_type" : "_doc" }}
{ "file_name": "Lucene is cool","file_type": "file","database_name": "","table_name": "","include_fields": "","source_business": 1,"store_type": "hdfs","whether_online": 0,"foreign_id": 10,"update_time": 1618560193000}
{ "index" : { "_index" : "12_assets_directory_v1","_type" : "_doc" }}
{ "file_name": "Lucene用户文件","file_type": "file","database_name": "","table_name": "","include_fields": "","source_business": 1,"store_type": "hdfs","whether_online": 0,"foreign_id": 11,"update_time": 1618560193010}
{ "index" : { "_index" : "12_assets_directory_v1","_type" : "_doc" }}
{ "file_name": "103-is-cool","file_type": "file","database_name": "","table_name": "","include_fields": "","source_business": 1,"store_type": "hdfs","whether_online": 0,"foreign_id": 12,"update_time": 1618560193040}
{ "index" : { "_index" : "12_assets_directory_v1","_type" : "_doc" }}
{ "file_name": "103_is_cool","file_type": "file","database_name": "","table_name": "","include_fields": "","source_business": 1,"store_type": "hdfs","whether_online": 0,"foreign_id": 20,"update_time": 1618560193120}
{ "index" : { "_index" : "12_assets_directory_v1","_type" : "_doc" }}
{ "file_name": "103 is cool","file_type": "file","database_name": "","table_name": "","include_fields": "","source_business": 1,"store_type": "hdfs","whether_online": 0,"foreign_id": 17,"update_time": 1618560193070}
{ "index" : { "_index" : "12_assets_directory_v1","_type" : "_doc" }}
{ "file_name": "test001","file_type": "file","database_name": "","table_name": "","include_fields": "","source_business": 1,"store_type": "hdfs","whether_online": 0,"foreign_id": 18,"update_time": 1618560193080}
{ "index" : { "_index" : "12_assets_directory_v1","_type" : "_doc" }}
{ "file_name": "test002 ldz","file_type": "file","database_name": "","table_name": "","include_fields": "","source_business": 1,"store_type": "hdfs","whether_online": 0,"foreign_id": 19,"update_time": 1618560193090}
{ "index" : { "_index" : "12_assets_directory_v1","_type" : "_doc" }}
{ "file_name": "美丽心之所想","file_type": "file","database_name": "","table_name": "","include_fields": "","source_business": 1,"store_type": "hdfs","whether_online": 0,"foreign_id": 13,"update_time": 1618560193050}
{ "index" : { "_index" : "12_assets_directory_v1","_type" : "_doc" }}
{ "file_name": "魅力","file_type": "file","database_name": "美好","table_name": "","include_fields": "","source_business": 1,"store_type": "hdfs","whether_online": 0,"foreign_id": 14,"update_time": 1618560193060}
{ "index" : { "_index" : "12_assets_directory_v1","_type" : "_doc" }}
{ "file_name": "","file_type": "table","database_name": "geespace_bd_platform_dev","table_name": "12_mysql-1","include_fields": "","source_business": 1,"store_type": "mysql","whether_online": 0,"foreign_id": 10,"update_time": 1618560193020}
{ "index" : { "_index" : "12_assets_directory_v1","_type" : "_doc" }}
{ "file_name": "","file_type": "table","database_name": "geespace_bd_platform_dev","table_name": "103_addserialnumber_2","include_fields": "","source_business": 1,"store_type": "mysql","whether_online": 0,"foreign_id": 11,"update_time": 1618560193030}

第三部分:执行

“song-suggest”为自定义的别名,“field”为要查询的字段

POST 12_assets_directory_v1/_search?pretty
{"suggest": {"song-suggest": {"prefix": "g","completion": {"field": "region"}}}
}

第四部分:结果展示

返回json中options集合中的text即为补全提示的内容

{"took" : 5,"timed_out" : false,"_shards" : {"total" : 5,"successful" : 5,"skipped" : 0,"failed" : 0},"hits" : {"total" : 0,"max_score" : 0.0,"hits" : [ ]},"suggest" : {"song-suggest" : [{"text" : "g","offset" : 0,"length" : 1,"options" : [{"text" : "geespace_bd_platform_dev","_index" : "12_assets_directory_v1","_type" : "_doc","_id" : "jIkR6HgBdsS-EtqKoYwv","_score" : 1.0,"_ignored" : ["region"],"_source" : {"file_name" : "","file_type" : "table","database_name" : "geespace_bd_platform_dev","table_name" : "12_mysql-1","include_fields" : "","source_business" : 1,"store_type" : "mysql","whether_online" : 0,"foreign_id" : 10,"update_time" : 1618560193020}},{"text" : "geespace_bd_platform_dev","_index" : "12_assets_directory_v1","_type" : "_doc","_id" : "jYkR6HgBdsS-EtqKoYwv","_score" : 1.0,"_ignored" : ["region"],"_source" : {"file_name" : "","file_type" : "table","database_name" : "geespace_bd_platform_dev","table_name" : "103_addserialnumber_2","include_fields" : "","source_business" : 1,"store_type" : "mysql","whether_online" : 0,"foreign_id" : 11,"update_time" : 1618560193030}}]}]}
}

本人先关其他文章链接

1.ElasticSearch7.6.x 模板及滚动索引创建及注意事项
https://blog.csdn.net/a924382407/article/details/115082265

2.ElasticSearch的IK分词器
https://blog.csdn.net/a924382407/article/details/117255506

3.ElasticSearch核心概念:倒排索引
https://blog.csdn.net/a924382407/article/details/117255449

4.springboot集成ElasticSearch使用completion实现补全功能
https://blog.csdn.net/a924382407/article/details/115868167

5.ES Restful API讲解使用
https://blog.csdn.net/a924382407/article/details/115085022

6.ES API,使用Kibana的开发工具用例说明
https://blog.csdn.net/a924382407/article/details/115084549

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

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

相关文章

[BUUCTF]Mysterious

[BUUCTF]Mysterious 分析 下载得到可执行文件,随便输入一些,没得反应解题PE..L..说明其为32位的exe文件,放入IDA PE…d…是64位程序 shift+F12检索字符串,看到well done,点进去 原因:大佬说这可能表示该程序输入成功后的情况类似于答案的文字左侧401090进去,接着F5进行反…

福气满满——项目原型设计+概要设计

博客文档这个作业属于哪个课程 https://edu.cnblogs.com/campus/fzu/SE2024作业要求 https://edu.cnblogs.com/campus/fzu/SE2024/homework/13297作业的目标 完成小福同学的原型设计以及概要设计团队名称 福气满满团队成员学号-名字 052203132童潇剑,102201226陈潇健,1022012…

ElasticSearch核心概念:倒排索引

ElasticSearch核心概念:倒排索引摘要 集群、节点、索引、类型、文档、分片、映射是什么? 分片(一个分片就是一个Lucene索引,Lucene索引采用倒排索引结构 ) 物理设计:节点和分片如何工作倒排索引ES使用的是一种称为倒排索引的结构,采用Lucene倒排索作为底层。这种结构适用…

2024秋软件工程现场编程作业

作业所属课程 班级链接作业要求 作业要求链接作业的目标 开发一个个人记账本应用团队名称 爱码单车队成员1 102201542--曾庆徽成员2 102201211--池家益成员3 102201302--毛震成员4 102201420--林传昊成员5 102201425--郑礼鑫成员6 102201538--黄志梁成员7 102201630--岳俊杰成员…

毕棚沟一日游玩

路线:成都(自驾)--> 毕棚沟(景区大巴)-->龙王海-->上海子-->磐羊湖(月亮湾)-->燕子岩成都--毕棚沟:自驾。5点出发。 毕棚沟--龙王海:景区大巴,顺时针绕湖走半圈,有黄杉、红枫,然后继续坐大巴 龙王海--上海子:景区大巴 上海子--磐羊湖:自费。上山单程…

基于STM32的激光通信系统设计

备忘编者按:激光通信具有保密性强、通信容量大、重量轻、功耗和体积小、制造和维护费用低等特点。为满足民用领域对于激光通信的需求,设计了一种基于STM32的激光通信系统。该系统由激光发射模块、激光接收模块,STM32F407主控模块组成,采用双音多频方式进行调制。在实验室环…

Respiratory Physiology Neurobiology

Respiratory Physiology & Neurobiology@目录一、征稿简介二、重要信息三、服务简述四、投稿须知 一、征稿简介二、重要信息期刊官网:https://ais.cn/u/3eEJNv三、服务简述 四、投稿须知 1.在线投稿:由艾思科蓝支持在线投稿,请将文章全文投稿至艾思科蓝投稿系统; 2.文章…

Applied Artificial Intelligence

Applied Artificial Intelligence@目录一、征稿简介二、重要信息三、服务简述四、投稿须知 一、征稿简介二、重要信息期刊官网:https://ais.cn/u/3eEJNv三、服务简述 四、投稿须知 1.在线投稿:由艾思科蓝支持在线投稿,请将文章全文投稿至艾思科蓝投稿系统; 2.文章应具有学术…

十光年团队_项目原型设计+概要设计

作业所属的课程 软件工程2024作业要求 2024秋软工实践团队作业-第二次作业的目标 完成原型设计和概要设计团队名称 十光年团队成员学号-姓名 施靖杰-102201327邓才慧-102201102陈宇尧-102201119陆旭东-102201118黄宇舟-102201331邱予-102202121高鑫源-102201635黄森福-10220163…

【文件系统】嵌入式文件系统Fatfs简介

Fatfs 1.Fatfs简介 FatFs(File Allocation Table File System)是一个专为小型嵌入式系统设计的通用FAT文件系统模块。它完全由ANSI C语言编写,独立于硬件平台,因此具有很好的可移植性。FatFs支持FAT12、FAT16和FAT32文件系统,可以用于各种嵌入式平台,包括但不限于8051、P…

CTF学习(17)MISC(后门查杀)

1.解压获得html文件夹(入门用的工具题吗)--->使用D盾以文本文件格式打开include.php文件后发现pass处有段md5数据(实为flag)(还是不太明白md5加密的特征,只是长度一样吗) FLAG: flag{6ac45fb83b3bc355c024f5034b947dd3}

一些AI数学基础

众所周知,在当前机器学习看待数据的很重要一个方式是概率,例如分类问题是建模一个P(Y=C|X)。 在面对离散变量的时候,例如人名这种离散变量。 假设有问题:给一个名字,判断该人是中国哪里人。(或许在现实生活中,该问题是不合理的,一般情况下无法根据人名判断是哪里人) 假…