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

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
简介: springboot集成ElasticSearch使用completion实现补全功能

@[TOC]

摘要

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

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

相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
目录
相关文章
|
1月前
|
安全 定位技术 API
婚恋交友系统匹配功能 婚恋相亲软件实现定位 语音社交app红娘系统集成高德地图SDK
在婚恋交友系统中集成高德地图,可实现用户定位、导航及基于地理位置的匹配推荐等功能。具体步骤如下: 1. **注册账号**:访问高德开放平台,注册并创建应用。 2. **获取API Key**:记录API Key以备开发使用。 3. **集成SDK**:根据开发平台下载并集成高德地图SDK。 4. **配置功能**:实现定位、导航及基于位置的匹配推荐。 5. **注意事项**:保护用户隐私,确保API Key安全,定期更新地图数据,添加错误处理机制。 6. **测试优化**:完成集成后进行全面测试,并根据反馈优化功能。 通过以上步骤,提升用户体验,提供更便捷的服务。
|
2月前
|
Java 开发者 微服务
手写模拟Spring Boot自动配置功能
【11月更文挑战第19天】随着微服务架构的兴起,Spring Boot作为一种快速开发框架,因其简化了Spring应用的初始搭建和开发过程,受到了广大开发者的青睐。自动配置作为Spring Boot的核心特性之一,大大减少了手动配置的工作量,提高了开发效率。
75 0
|
15天前
|
监控 Java Nacos
使用Spring Boot集成Nacos
通过上述步骤,Spring Boot应用可以成功集成Nacos,利用Nacos的服务发现和配置管理功能来提升微服务架构的灵活性和可维护性。通过这种集成,开发者可以更高效地管理和部署微服务。
110 17
|
15天前
|
XML JavaScript Java
SpringBoot集成Shiro权限+Jwt认证
本文主要描述如何快速基于SpringBoot 2.5.X版本集成Shiro+JWT框架,让大家快速实现无状态登陆和接口权限认证主体框架,具体业务细节未实现,大家按照实际项目补充。
62 11
|
17天前
|
缓存 安全 Java
Spring Boot 3 集成 Spring Security + JWT
本文详细介绍了如何使用Spring Boot 3和Spring Security集成JWT,实现前后端分离的安全认证概述了从入门到引入数据库,再到使用JWT的完整流程。列举了项目中用到的关键依赖,如MyBatis-Plus、Hutool等。简要提及了系统配置表、部门表、字典表等表结构。使用Hutool-jwt工具类进行JWT校验。配置忽略路径、禁用CSRF、添加JWT校验过滤器等。实现登录接口,返回token等信息。
199 12
|
23天前
|
存储 安全 Java
Spring Boot 3 集成Spring AOP实现系统日志记录
本文介绍了如何在Spring Boot 3中集成Spring AOP实现系统日志记录功能。通过定义`SysLog`注解和配置相应的AOP切面,可以在方法执行前后自动记录日志信息,包括操作的开始时间、结束时间、请求参数、返回结果、异常信息等,并将这些信息保存到数据库中。此外,还使用了`ThreadLocal`变量来存储每个线程独立的日志数据,确保线程安全。文中还展示了项目实战中的部分代码片段,以及基于Spring Boot 3 + Vue 3构建的快速开发框架的简介与内置功能列表。此框架结合了当前主流技术栈,提供了用户管理、权限控制、接口文档自动生成等多项实用特性。
71 8
|
28天前
|
人工智能 数据处理 C#
AI Dev Gallery:微软开源 Windows AI 模型本地运行工具包和示例库,助理开发者快速集成 AI 功能
微软推出的AI Dev Gallery,为Windows开发者提供开源AI工具包和示例库,支持本地运行AI模型,提升开发效率。
78 13
|
1月前
|
人工智能 自然语言处理 搜索推荐
Open Notebook:开源 AI 笔记工具,支持多种文件格式,自动转播客和生成总结,集成搜索引擎等功能
Open Notebook 是一款开源的 AI 笔记工具,支持多格式笔记管理,并能自动将笔记转换为博客或播客,适用于学术研究、教育、企业知识管理等多个场景。
152 0
Open Notebook:开源 AI 笔记工具,支持多种文件格式,自动转播客和生成总结,集成搜索引擎等功能
|
1月前
|
XML Java API
Spring Boot集成MinIO
本文介绍了如何在Spring Boot项目中集成MinIO,一个高性能的分布式对象存储服务。主要步骤包括:引入MinIO依赖、配置MinIO属性、创建MinIO配置类和服务类、使用服务类实现文件上传和下载功能,以及运行应用进行测试。通过这些步骤,可以轻松地在项目中使用MinIO的对象存储功能。
110 5
|
2月前
|
消息中间件 Java Kafka
什么是Apache Kafka?如何将其与Spring Boot集成?
什么是Apache Kafka?如何将其与Spring Boot集成?
97 5

热门文章

最新文章