当前位置: 首页 > news >正文

es自动补全(仅供自己参考)

elasticssearch提供了CompletionSuggester查询来实现自动补全功能。这个查询会匹配以用户输入内容开头的词条并返回。为了提高补全查询效率,对于文档中字段的类型有一些约束:

+ 查询类型必须是:completion

+ 字段内容是多个补全词条形成的数组

PUT /test2
{"mappings": {"properties": {"title":{"type": "completion"   #创建字段类型的时候,只能是completion}}}
}POST /test2/_doc/1
{"title":["Sony", "WH-1000XM3"]    #字段的内容是多词条形成的数组
}POST test2/_doc/2
{"title": ["SK-II", "PITERA"]
}
POST test2/_doc/3
{"title": ["Nintendo", "switch"]
}# 查询语法,自动补全
GET /test2/_search
{"suggest": {"titleSuggest": {    #查询的名称"text": "So",        #查询的内容"completion": {    #查询补全的类型"field": "title",    #字段类型"skip_duplicates": true,    #跳过重复的词条"size": 10         #查询的大小}}}
}

完成一个hotel酒店的es库创建:(创建了两个自定义的分词器)

PUT /hotel
{"settings": {"analysis": {"analyzer": {"text_anlyzer": {"tokenizer": "ik_max_word","filter": "py"},"completion_analyzer": {"tokenizer": "keyword","filter": "py"}},"filter": {"py": {"type": "pinyin","keep_full_pinyin": false,"keep_joined_full_pinyin": true,"keep_original": true,"limit_first_letter_length": 16,"remove_duplicated_term": true,"none_chinese_pinyin_tokenize": false}}}},"mappings": {"properties": {"id":{"type": "keyword"},"name":{"type": "text","analyzer": "text_anlyzer","search_analyzer": "ik_smart","copy_to": "all"},"address":{"type": "keyword","index": false},"price":{"type": "integer"},"score":{"type": "integer"},"brand":{"type": "keyword","copy_to": "all"},"city":{"type": "keyword"},"starName":{"type": "keyword"},"business":{"type": "keyword","copy_to": "all"},"location":{"type": "geo_point"},"pic":{"type": "keyword","index": false},"all":{"type": "text","analyzer": "text_anlyzer","search_analyzer": "ik_smart"},"suggestion":{"type": "completion","analyzer": "completion_analyzer","search_analyzer": "ik_smart"  # 使用这个为了拼音和汉字都可以使用,而不只是拼音}}}
}

java代码查询:

 @Testpublic void completionTest() throws IOException {SearchRequest request = new SearchRequest("hotel");request.source().suggest(new SuggestBuilder().addSuggestion("suggestions",SuggestBuilders.completionSuggestion("suggestion").prefix("火").size(10).skipDuplicates(true)));SearchResponse response = client.search(request, RequestOptions.DEFAULT);Suggest suggest = response.getSuggest();CompletionSuggestion suggestions = suggest.getSuggestion("suggestions");List<CompletionSuggestion.Entry.Option> options = suggestions.getOptions();for (CompletionSuggestion.Entry.Option option : options) {String string = option.getText().string();System.out.println(string);}}

http://www.lryc.cn/news/478265.html

相关文章:

  • 13-综合排序:Function Score Query 优化算分
  • 鸿蒙应用App测试-专项测试(DevEco Testing)
  • RabbitMQ设置消息过期时间
  • 大数据-209 数据挖掘 机器学习理论 - 梯度下降 梯度下降算法调优
  • 粒子群优化双向深度学习!PSO-BiTCN-BiGRU-Attention多输入单输出回归预测
  • 排序算法简介
  • (没有跳过联网激活)导致使用微软账号激活电脑---修改为本地账户和英文名字
  • [论文粗读][REALM: Retrieval-Augmented Language Model Pre-Training
  • flink 内存配置(五):网络缓存调优
  • set和map的使用
  • LCL三相并网逆变器simulink仿真+说明文档
  • 从0开始深度学习(24)——填充和步幅
  • CPU Study - Instructions Fetch
  • GJ Round (2024.9) Round 1~7
  • 【CMCL】多模态情感识别的跨模态对比学习
  • 输入/输出系统
  • asp.net+uniapp养老助餐管理系统 微信小程序
  • 部署istio应用未能产生Envoy sidecar代理
  • 使用YOLO 模型进行线程安全推理
  • ABAP 增强
  • vue使用方法创建组件
  • HTML 基础标签——链接标签 <a> 和 <iframe>
  • @SpringBootApplication源码解析
  • 【实战篇】requests库 - 有道云翻译爬虫 【附:代理IP的使用】
  • 法语动词变位
  • Excel:vba实现批量插入图片
  • Vue3的router和Vuex的学习笔记整理
  • 设置JAVA以适配华为2288HV2服务器的KVM控制台
  • 掌握Qt调试技术
  • 使用NVM自由切换nodejs版本