欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 金融 > ES推荐搜索、自动补全,并且springBoot集成

ES推荐搜索、自动补全,并且springBoot集成

2024/10/23 9:23:51 来源:https://blog.csdn.net/m0_73363097/article/details/143022067  浏览:    关键词:ES推荐搜索、自动补全,并且springBoot集成

推荐搜索

在搜索过程中,因为单词的拼写错误,没有得到任何的结果,希望ES能够给我们一个推荐搜索。

GET movies/_search
{"suggest": {# title_suggestion为我们自定义的名字"title_suggestion": {"text": "drema","term": {"field": "title","suggest_mode": "popular"}}}
}

suggest_mode,有三个值:popular、missing、always

1. popular 是推荐词频更高的一些搜索。

2. missing 是当没有要搜索的结果的时候才推荐。

3. always无论什么情况下都进行推荐。

自动补全

自动补全应该是我们在日常的开发过程中最常见的搜索方式了,如百度搜索和京东商品搜索。

自动补全的功能对性能的要求极高,用户每发送输入一个字符就要发送一个请求去查找匹配项。

ES采取了不同的数据结构来实现,并不是通过倒排索引来实现的;需要将对应的数据类型设置为

completion ; 所以在将数据索引进ES之前需要先定义 mapping 信息。

1.定义mapping

PUT /test-completion
{"mappings": {"properties": {"name": {"type": "completion","analyzer": "ik_smart"}}}
}

添加数据

POST test-completion/_bulk
{"index": {}}
{"name": "刘德华"}
{"index": {}}
{"name": "张学友"}
{"index": {}}
{"name": "柳岩"}
{"index": {}}
{"name": "柳菲菲"}
{"index": {}}
{"name": "刘备"}

2.前缀搜索

GET test-completion/_search
{"suggest": {"prefix_suggestion": {"prefix": "刘","completion": {"field": "name","skip_duplicates": true,"size": 10}}}
}


GET test-completion/_search
{"suggest": {"prefix_suggestion": {"prefix": "刘德","completion": {"field": "name","skip_duplicates": true,"size": 10}}}
}

3.同一个字段可能既是搜索字段又是补全,则可以建立子字段

PUT /test-completion1
{"mappings": {"properties": {"name": {"type": "text","analyzer": "ik_smart","fields": { "nameCompletion": {"type": "completion"}}}}}
}

查询

GET test-completion1/_search
{"suggest": {"prefix_suggestion": {"prefix": "刘","completion": {"field": "name.nameCompletion","skip_duplicates": true,"size": 10}}}
}

结果

springboot集成实现自动补全

 @Autowiredprivate ElasticsearchClient client;public List<String> suggest(String title) throws IOException {List<String> result = new ArrayList<>();//构建FiledSuggestMapMap<String,FieldSuggester> filedSuggestMap = new HashMap<>();FieldSuggester nameFiledSuggest = FieldSuggester.of(f -> f.prefix(title).completion(c -> c.field("name.nameCompletion").skipDuplicates(true).size(10)));filedSuggestMap.put("pre_suggest",nameFiledSuggest);//构建SearchRequestSearchRequest searchRequest = SearchRequest.of(s -> s.index("suggest").suggest(s1 -> s1.suggesters(filedSuggestMap)));SearchResponse<Object> search = client.search(searchRequest, Object.class);Map<String, List<Suggestion<Object>>> suggest = search.suggest();if (CollectionUtils.isEmpty(suggest)){return result;}List<CompletionSuggestOption<Object>> preSuggest = search.suggest().get("pre_suggest").get(0).completion().options();for (CompletionSuggestOption<Object> suggestOption : preSuggest) {result.add(suggestOption.text());}return result;}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com