1. Concept
1.1 The completion API is mainly divided into four categories
- Term Suggester (error correction completion, complete the correct word in case of input error)
- Phrase Suggester (automatically complete phrases, enter a word to complete the entire phrase)
- Completion Suggester (Complete the word, output the first half, complete the whole word)
- Context Suggester
The overall effect is similar to Baidu search, as shown in the figure:
2. Completion suggester automatic completion
A suggester designed for auto-completion scenarios. In this scenario, every time the user inputs a character, a query request needs to be immediately sent to the backend to find the matching item. When the user input speed is high, the backend response speed is more stringent. Therefore, in terms of implementation, it uses a different data structure from the previous two Suggesters. The index is not completed through inversion, but the analyzed data is encoded into FST and stored together with the index. For an index in the open state, the FST will be completely loaded into the memory by ES, and the prefix search speed is extremely fast. However, FST can only be used for prefix search, which is also the limitation of Completion Suggester .
2.1 Create index
put /book
{
"mappings": {
"music" : {
"properties" : {
"suggest" : {
"type" : "completion"
},
"title" : {
"type": "keyword"
}
}
}
}
}
注意,最终写入数据需要 /book/music
2.2 Insert data
put /book/music/_doc/1
{
"suggest":"爱一个人好难"
}
put /book/music/_doc/2
{
"suggest":"爱一个人好难"
}
put /book/music/_doc/3
{
"suggest":"爱真的需要勇气"
}
2.3 Auto-complete query
Example 1: Query suggestions based on prefix query
POST book/music/_search
{
"suggest": {
"song-suggest" : {
"prefix" : "爱",
"completion" : {
"field" : "suggest"
}
}
}
}
Example 1: Query suggestions query results based on prefix
{
"took": ,
"timed_out": false,
"_shards": {
"total": ,
"successful": ,
"skipped": ,
"failed":
},
"hits": {
"total": ,
"max_score": ,
"hits": []
},
"suggest": {
"song-suggest": [
{
"text": "te",
"offset": ,
"length": ,
"options": [
{
"text": "爱一个人好难",
"_index": "book",
"_type": "music",
"_id": "6Xu6mmUBYLvVFwGWpXeL",
"_score": ,
"_source": {
"suggest": "爱一个人好难"
}
},
{
"text": "爱一个人好难",
"_index": "book",
"_type": "music",
"_id": "6nu8mmUBYLvVFwGWSndC",
"_score": ,
"_source": {
"suggest": "爱一个人好难"
}
},
{
"text": "爱真的需要勇气",
"_index": "book",
"_type": "music",
"_id": "63u8mmUBYLvVFwGWZHdC",
"_score": ,
"_source": {
"suggest": "爱真的需要勇气"
}
}
]
}
]
}
}
Example 2: Deduplication of suggested query results
{
"suggest": {
"song-suggest" : {
"prefix" : "爱",
"completion" : {
"field" : "suggest" ,
"skip_duplicates": true
}
}
}
}
Part of the reference in this article: " ES Series 13, Elasticsearch Suggester API (Automatic Completion) " There are four completion methods in it.