ElasticSearch搜索提示实现

一、四种Suggester介绍

Suggesters基本的运作原理是将输入的文本分解为token,然后在索引的字典里查找相似的term并返回。 根据使用场景的不同,Elasticsearch里设计了4种类别的Suggester,分别是:

Term Suggester
Completion Suggester
Phrase Suggester
Context Suggester

二、四个Suggester比较[1]

Term Suggester——基于编辑距离算法实现。在提供建议之前,对输入的文本进行分析

Phrase suggester——在 term suggester之上添加额外的逻辑以选择整个经校正的短语,而不是基于 ngram-language 模型加权的单个 token

Completion Suggester——只能用于前缀查询,速度很快,性能要求高

•需求场景是:输入一个字符,即时发送一个请求查询匹配项•数据结构:并非是倒排索引实现的,而是将分词的数据编码成FST和索引一起存放;FST会被加载进内存,速度很快•限制:需要对查询字段指定为Completion

Context Suggester——可以通过筛选提供建议,context 支持两种类型,分别是category(任意字符串),geo(地理位置信息)

准确度completion > phrase > term

三、Completion Suggester Mapping的设置

因为Completion Suggester的搜索补全和搜索提示是要求查询的字段typeCompletion类型的。所以在定义Mapping时候需要将被查询的字段type定义为completion类型。查询的Mapping如下:

PUT document {
    "mappings": {
        "properties": {
            "id": {
                "type": "keyword"
            },
            "doc_name": {
                "type": "completion",
                "analyzer": "ik_max_word"
            },
            "doc_number": {
                "type": "text",
                "analyzer": "ik_max_word"
            },
            "doc_type": {
                "type": "text",
                "analyzer": "ik_max_word"
            },
            "keywords": {
                "type": "completion",
                "analyzer": "ik_max_word"
            },
            "pubdate": {
                "type": "date",
                "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
            },
            "attachment": {
                "properties": {
                    "content": {
                        "type": "text",
                        "analyzer": "ik_max_word"
                    }
                }
            }
        }
    }
}

因为需要进行全文检索添加了attachment的内容

四、TransportClientREST client的区别[2]

Elasticsearch计划在Elasticsearch 7.0中弃用TransportClient
在8.0中完全删除它。相反,您应该使用Java高级REST clientrest client执行HTTP请求来执行操作,无需再序列化的Java请求。

TransportClient 是ElasticSearch(java)客户端封装对象,使用transport模块远程连接到Elasticsearch集群,该transport node并不会加入集群,而是简单的向ElasticSearch集群上的节点发送请求。transport node使用轮询机制进行集群内的节点进行负载均衡,尽管大多数操作(请求)可能是“两跳操作”。(图片来源于Elasticsearch权威指南)

Java REST客户端有两种风格:

Java Low Level REST Clientelasticsearch client低级别客户端。它允许通过http请求与Elasticsearch集群进行通信。API本身不负责数据的编码解码,由用户去编码解码。它与所有的ElasticSearch版本兼容。
Java High Level REST ClientElasticsearch client官方高级客户端。基于低级客户端,它定义的API,已经对请求与响应数据包进行编码解码。

五、基于ElasticSearch Java REST Client API的自动补全

 /**
    * @param suggestField 查询搜索补全的字段
    * @param suggestValue 查询搜索补全的值
    * @return 返回搜索补全list
    * @throws IOException IO异常
  */
  public List<String> suggestSearchList(String suggestField, String suggestValue) throws IOException {

        /**
         * ElasticSearch 7.X版本以上 不在使用TransportClient进行客户端连接 所以使用client进行连接客户端无法进行使用
         * 7.X版本将搜索补全(completion)合并到SuggestBuilders中进行使用,在SuggestBuilders中构建completionSuggestion搜索参数
         */

        // 构建SearchRequest、SearchSourceBuilder 指定查询的库
        // SearchRequest searchRequest = new SearchRequest(ESConst.ES_INDEX);
        SearchRequest searchRequest = new SearchRequest("testdata");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        // 控制显示内容 (优化查询效率将所有无关查询提示字段都不显示)
        String[] excludeFields = new String[] {"doc_number","doc_type","attachment","doc_keywords","id","pubdate","doc_name"};
        String[] includeFields = new String[] {""};
        searchSourceBuilder.fetchSource(includeFields, excludeFields);

        // 构建completionSuggestionBuilder传入查询的参数
        CompletionSuggestionBuilder completionSuggestionBuilder = SuggestBuilders.completionSuggestion(suggestField).prefix(suggestValue).size(10);
        SuggestBuilder suggestBuilder = new SuggestBuilder();
        // 定义查询的suggest名称
        suggestBuilder.addSuggestion(suggestField+"_suggest", completionSuggestionBuilder);
        searchSourceBuilder.suggest(suggestBuilder);
        searchRequest.source(searchSourceBuilder);

        // 执行查询
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        // 获取查询的结果
        Suggest suggest = searchResponse.getSuggest();

        Set<String> suggestSet = new HashSet<>();
        int maxSuggest = 0;
        if (suggest != null) {
            // 获取Suggestion的结果
            Suggest.Suggestion result = suggest.getSuggestion(suggestField+"_suggest");
            // 遍历获得查询结果的Text
            for (Object term : result.getEntries()) {
                if (term instanceof CompletionSuggestion.Entry) {
                    CompletionSuggestion.Entry item = (CompletionSuggestion.Entry) term;
                    if (!item.getOptions().isEmpty()) {
                        // 若item的option不为空,循环遍历
                        for (CompletionSuggestion.Entry.Option option : item.getOptions()) {
                            String tip = option.getText().toString();
                            if (!suggestSet.contains(tip)) {
                                suggestSet.add(tip);
                                ++maxSuggest;
                            }
                        }
                    }
                }
                if (maxSuggest >= 10) {
                    break;
                }
            }
        }

        return Arrays.asList(suggestSet.toArray(new String[]{}));
    }

代码思路:

1、首先实例化构建SearchRequestSearchSourceBuilder,查询document文档;

2、控制查询显示的内容,使用searchSourceBuilder.fetchSource控制excludeFieldsincludeFields(无关的要素不进行查询);

3、构建completionSuggestionBuilder,以参数形式传入suggestFieldsuggestValue,默认设置size为10;

4、定义查询的suggest_name,通过suggestBuilder.addSuggestion进行添加;

5、执行查询,searchResponse.getSuggest获得查询的结果;

6、遍历获得Suggest中的text,输出传入list返回给前端。

六、实现效果截图

 

References

[1] 四个Suggester比较: https://www.jianshu.com/p/34db35d13cd3
[2] TransportClientREST client的区别: https://blog.csdn.net/prestigeding/article/details/83188043

posted @ 2021-06-25 19:09  宥_XWX  阅读(31)  评论(0编辑  收藏  举报  来源