当前位置: 首页 > 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;}

相关文章:

  • Swift 小技巧:用单边区间优雅处理模糊范围
  • 杨洋出席喜临门Ai净眠智能新品发布会 今夜无人失眠
  • 基于Java+Springboot的宠物健康咨询系统
  • tmux-copy mode相关配置文件
  • 小米路由器 AX3000T自定义子网掩码
  • rollupOptions 详细讲解,如何优化性能
  • 07-Seq2Seq英译法案例
  • 模运算优化
  • 用R包mice进行多重插补
  • Git安装全攻略:避坑指南与最佳实践
  • Bugku——WEB篇(持续更新ing)
  • 代理模式 - Flutter中的智能替身,掌控对象访问的每一道关卡!
  • JavaScript中的回调函数详解
  • Springboot 集成 SpringBatch 批处理组件
  • 软件著作权人的权利
  • 【系统分析师】高分论文:论软件开发模型及应用
  • GitHub vs GitLab 全面对比报告(2025版)
  • 大模型小模型选型手册:开源闭源、国内国外全方位对比
  • Vulkan 学习(18)---- 使用 ValidationLayer
  • Function Calling与MCP的区别