首页 > 其他分享 >springboot集成ElasticSearch使用completion实现补全功能

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

时间:2024-11-02 21:31:42浏览次数:1  
标签:completion index 补全 springboot file org elasticsearch type name

@

目录

摘要

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

说明:使用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

标签:completion,index,补全,springboot,file,org,elasticsearch,type,name
From: https://www.cnblogs.com/bigcat26/p/18522486

相关文章

  • springboot智能停车场管理系统的设计与实现 84e3y
    目录项目技术路线系统实现截图技术栈说明本课题软硬件环境核心代码部分展示源码获取-详细视频演示项目技术路线本课题使用了SpringBoot、Vue和MySQL作为技术栈,体现了其技术可行性。SpringBoot作为轻量级Java开发框架,能提高开发效率和降低系统复杂度;Vue作为流行的前......
  • springboot社区饮水机自动售水系统的设计和实现
    目录项目技术路线系统实现截图技术栈说明本课题软硬件环境核心代码部分展示源码获取-详细视频演示项目技术路线本课题使用了SpringBoot、Vue和MySQL作为技术栈,体现了其技术可行性。SpringBoot作为轻量级Java开发框架,能提高开发效率和降低系统复杂度;Vue作为流行的前......
  • java开发基于springboot框架生鲜海鲜商城交易系统设计与实现
    目录项目技术路线系统实现截图技术栈说明本课题软硬件环境核心代码部分展示源码获取-详细视频演示项目技术路线本课题使用了SpringBoot、Vue和MySQL作为技术栈,体现了其技术可行性。SpringBoot作为轻量级Java开发框架,能提高开发效率和降低系统复杂度;Vue作为流行的前......
  • 基于springboot的网络海鲜市场系统,附源码+数据库+论文,包安装调试
    1、项目介绍网络海鲜市场系统把操作该系统的用户群分为两类,即管理员,用户。管理员对于网络海鲜市场系统操作的功能包括对商品资讯,用户信息,商品信息等进行增删改查,同时可以回复用户对商品的评价,以及管理商品订单和论坛帖子。其用例图如图3.4所示:用户对于网络海鲜市场系统操作......
  • springboot+vue超市仓库管理系统【开题+程序+论文】
    系统程序文件列表开题报告内容研究背景随着现代商业的快速发展,超市作为零售业的重要组成部分,其运营效率和管理水平直接关系到企业的竞争力。超市仓库作为商品存储和流转的关键环节,其管理效率直接影响到超市的运营成本和顾客满意度。然而,传统的人工管理方式存在诸多不足,如信......
  • 【JAVA毕业设计】基于Vue和SpringBoot的网上订餐系统
    本文项目编号T039,文末自助获取源码\color{red}{T039,文末自助获取源码}......
  • 【JAVA毕业设计】基于Vue和SpringBoot的房屋租赁系统
    本文项目编号T040,文末自助获取源码\color{red}{T040,文末自助获取源码}......
  • 基于Springboot+Vue的人事管理系统 (含源码数据库)
    1.开发环境开发系统:Windows10/11架构模式:MVC/前后端分离JDK版本:JavaJDK1.8开发工具:IDEA数据库版本:mysql5.7或8.0数据库可视化工具:navicat服务器:SpringBoot自带apachetomcat主要技术:Java,Springboot,mybatis,mysql,vue2.视频演示地址3.功能这个系......
  • 【含文档】基于Springboot+Vue的工商局商家管理系统 (含源码数据库+LW)
    1.开发环境开发系统:Windows10/11架构模式:MVC/前后端分离JDK版本:JavaJDK1.8开发工具:IDEA数据库版本:mysql5.7或8.0数据库可视化工具:navicat服务器:SpringBoot自带apachetomcat主要技术:Java,Springboot,mybatis,mysql,vue2.视频演示地址3.功能系统定......
  • java毕业设计基于springboot+vue的奶茶店线下点餐管理系统
    一、项目介绍  本系统旨在通过Web技术,实现奶茶店线下点餐流程的自动化与智能化。主要功能包括品信息、餐品信息、桌台信息、开台信息、点餐信息、桌台更换、餐具更换、商品库存、商品采购等;此外,系统还能对销售数据进行统计分析,为奶茶店的经营决策提供有力支持。本系统......