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

​19.自动补全功能

 参与自动补全查询的字段必须是completion类型

字段内容一般为用来补全的多个词条形成的数组

开始给酒店索引库实现自动补全功能

重新定义hotel的mapping

{"settings": {"analysis": {"analyzer": {"text_analyzer": {"tokenizer": "ik_max_word","filter": "py"},"completion_analyzer": {"tokenizer": "keyword","filter": "py"}},"filter": {"py": {"type": "pinyin","keep_full_pinyin": false,"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_analyzer","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_analyzer","search_analyzer": "ik_smart"},"suggestion": {"type": "completion","analyzer": "completion_analyzer"}}}
}

插入数据后,自动补全查询:

首字母匹配:

 改造HotelDoc类,添加suggest字段List<String>类型:

package com.xkj.org.entity;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import java.util.Arrays;
import java.util.List;/*** @description: es的索引库hotel实体类* @author: xiankejin* @time: 2025-06-21**/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class HotelDoc {private Integer id;private String name;private String address;private Integer price;private Integer score;private String brand;private String city;private String starName;private String business;private String location;private String pic;/*** 自动补全的字段,数组*/private List<String> suggestion;public List<String> getSuggestion() {return suggestion;}public void setSuggestion(List<String> suggestion) {this.suggestion = suggestion;}/*** 距离值*/private Object distance;/*** 是否为广告*/private Boolean isAD;public HotelDoc(Hotel hotel) {this.id = hotel.getId();this.address = hotel.getAddress();this.brand = hotel.getBrand();this.business = hotel.getBusiness();this.city = hotel.getCity();//纬度, 经度(引文逗号) 顺序不能颠倒this.location = hotel.getLatitude()+", "+hotel.getLongitude();this.name = hotel.getName();this.pic = hotel.getPic();this.price = hotel.getPrice();this.score = hotel.getScore();this.starName = hotel.getStarName();this.suggestion = Arrays.asList(this.getBrand(), this.getBusiness());}}

@Overridepublic List<String> autoCompletion(String input) {List<String> suggestList = new ArrayList<>();try {SearchRequest searchRequest = new SearchRequest("hotel");searchRequest.source().suggest(new SuggestBuilder().addSuggestion("mySuggestion",SuggestBuilders.completionSuggestion("suggestion")//前缀匹配.prefix(input)//跳过重复的数据.skipDuplicates(true).size(10)));//发送请求SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);Suggest suggest = response.getSuggest();CompletionSuggestion suggestion = suggest.getSuggestion("mySuggestion");for(CompletionSuggestion.Entry.Option option: suggestion.getOptions()) {String text = option.getText().string();suggestList.add(text);}}catch (Exception e) {e.printStackTrace();}return suggestList;}
http://www.lqws.cn/news/570619.html

相关文章:

  • 机器学习7——神经网络上
  • SpringCloud系列(40)--SpringCloud Gateway的Filter的简介及使用
  • 基于YOLO的目标检测图形界面应用(适配于YOLOv5、YOLOv6、YOLOv8、YOLOv9、YOLOv10、YOLOv11、YOLOv12)
  • Spring Cloud 服务追踪实战:使用 Zipkin 构建分布式链路追踪
  • NLP文本增强——随机删除
  • ASP.Net依赖注入!使用Microsoft.Extensions.DependencyInjection配置依赖注入
  • Vue中的v-if与emit事件传递:一个常见陷阱分析
  • documents4j导出pdf
  • Spark Web UI从0到1详解
  • 野生动物检测数据集介绍-5,138张图片 野生动物保护监测 智能狩猎相机系统 生态研究与调查
  • 【大模型学习 | CLIP 原理代码实现】
  • Matlab自学笔记六十一:快速上手解方程
  • Vue 与react 生命周期对比
  • 什么是DID(Decentralized Identifier,去中心化身份)
  • 如何优化RK3588集群的性能?支持12个RK3588云手机阵列
  • C++ 设计模式—简略版
  • MySQL的调控按钮
  • 【linux】权限深入解析
  • C/C++数据结构之动态数组
  • Vulkan 学习(18)---- 使用 ValidationLayer
  • 洛谷日常刷题3
  • 通过交互式可视化探索波动方程-AI云计算数值分析和代码验证
  • Xcode 中的 Compilation Mode 是管什么的
  • 模拟与可视化复杂非线性偏微分方程:从KdV到云端几何问题-AI云计算数值分析和代码验证
  • 现代 JavaScript (ES6+) 入门到实战(一):告别 var!拥抱 let 与 const,彻底搞懂作用域
  • 80%的知识库场景选择FastGPT,20%的复杂场景选择Dify
  • 概率论符号和公式整理
  • Dify私有化知识库搭建并通过ChatFlow智能机器人使用知识库的详细操作步骤
  • C# 合并两个byte数组的几种方法
  • linux运维学习第10周