當(dāng)用戶在搜索框輸入字符時,我們應(yīng)該提示出與該字符有關(guān)的搜索項(xiàng),如圖
image.png
elasticsearch提供了Completion Suggesttion查詢實(shí)現(xiàn)自動補(bǔ)全浅乔。為了提高補(bǔ)全查詢的效率鲤妥,對于文檔字段的類型有限制
- 參與補(bǔ)全查詢的字段必須是completetion類型
- 字段的內(nèi)容一般是用來補(bǔ)全的多個字條組成的數(shù)組
PUT test
{
"mappings": {
" properties": {
"title":{
"type": "completion"
}
}
}
}
插入下面數(shù)據(jù)
// 示例數(shù)據(jù)
POST test/_doc
{
"title": ["Sony", "WH-1000XM3"]
}
POST test/_doc
{
"title": ["SK-II", "PITERA"]
}
POST test/_doc
{
"title": ["Nintendo", "switch"]
}
查詢的DSL語句如下
// 自動補(bǔ)全查詢
GET /test/_search
{
"suggest": {
"title_suggest": {
"text": "s", // 關(guān)鍵字
"completion": {
"field": "title", // 補(bǔ)全查詢的字段
"skip_duplicates": true, // 跳過重復(fù)的
"size": 10 // 獲取前10條結(jié)果
}
}
}
}
搜索補(bǔ)全結(jié)果
實(shí)現(xiàn)酒店搜索框的自動補(bǔ)全
1修改hotel索引的索引庫結(jié)構(gòu),設(shè)置自定義拼音分詞器
2修改索引庫需要被自定義分詞字段那么/all
3索引庫添加一個新字段suggestion夷恍,類型為completetion類型魔眨,使用我們自己自定義分詞器
4給HotelDoc類添加suggestestion字段,內(nèi)容包含brand酿雪、business
5重新導(dǎo)入數(shù)據(jù)到hotel庫
酒店索引映射結(jié)構(gòu)
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"
}
}
}
}
JAVA類
@Data
@NoArgsConstructor
public class HotelDoc {
private Long 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 Object distance;
private Boolean isAD;
private List<String> suggestion;
public HotelDoc(Hotel hotel) {
this.id = hotel.getId();
this.name = hotel.getName();
this.address = hotel.getAddress();
this.price = hotel.getPrice();
this.score = hotel.getScore();
this.brand = hotel.getBrand();
this.city = hotel.getCity();
this.starName = hotel.getStarName();
this.business = hotel.getBusiness();
this.location = hotel.getLatitude() + ", " + hotel.getLongitude();
this.pic = hotel.getPic();
// 判斷商圈是否包含/
if (this.business.contains("/")) {
// 需要切割
String[] arr = this.business.split("/");
this.suggestion = new ArrayList<>();
this.suggestion.add(this.brand);
Collections.addAll(this.suggestion, arr);
} else {
this.suggestion = Arrays.asList(this.brand, this.business);
}
}
}
重新導(dǎo)入數(shù)據(jù),編寫DSL語句
image.png
測試補(bǔ)全
參考圖片
解析語句