当前位置: 首页 > 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.mrgr.cn/news/67006.html

相关文章:

  • Html编写发射粒子爱心
  • yolov8涨点系列之替换DW卷积
  • 入门必读:深度解析如何利用Coze构建企业级知识库的详尽指南
  • 江协科技STM32学习- P28 USART串口数据包
  • WCY的比赛题解
  • 初学Java基础---Day21---正则表达式,日期类,Math类,Random类,System类,Runtime类,大数值运算类,
  • ASRPRO 日历2
  • Python 装饰器 (面向切面编程,语法糖,AOP)
  • PySpark 本地开发环境搭建与实践
  • 对自动化测试的一些展望与理解
  • Linux(CentOS)安装 MySQL
  • 伊莱亚斯 M. 斯坦恩(Elias M. Stein)《复分析》与《实分析》教材
  • APP 后台广告位配置的关键要素与策略
  • 浏览器是如何渲染页面的? - 2024最新版前端秋招面试短期突击面试题
  • 编程语言越来越多,为什么C/C++还没有被现在的时代淘汰呢?
  • 智合同丨买卖合同纠纷中,起诉关联公司需要准备些什么?
  • JVM 内存结构中哪些区域可能发生 OOM
  • 红队-linux基础(1)
  • linux利用环境变量提权以及如何防范
  • 基本开关电源电路分析
  • Axure设计之三级联动选择器教程(中继器)
  • 大家知道输电线路微风振动在线监测有哪些先进技术?
  • docker 入门教程
  • 什么是广告联盟?
  • ABC378
  • 字段值为null就不返回的注解