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;}