Elasticsearch study notes (five) detailed search supplement Suggest- query suggestions

table of Contents

Inquiry suggestion introduction

What is the query suggestion?

Query suggested API in ES

Suggester introduction

Term suggester

phrase suggester

Completion suggester automatic completion


Inquiry suggestion introduction

What is the query suggestion?

Query suggestions to provide users with a good experience. Mainly include: spell check; automatically suggest query words (automatic completion)

Query suggested API in ES

The query suggestion also uses the _search endpoint address. Use the suggest node in the DSL to define the required suggestion query.

POST twitter/_search
{
  "query" : {
    "match": {
      "message": "tring out Elasticsearch"
    }
  },
  "suggest" : {                           //定义建议查询
                                          //一个建议查询名
    "my-suggestion" : {
      "text" : "tring out Elasticsearch",  //查询文本
      "term" : {                          //使用词项建议器
        "field" : "message"               //指定在哪个字段上获取建议词
      }
    }
  }
}
POST _search
{
  "suggest": {
    "my-suggest-1" : {
      "text" : "tring out Elasticsearch",
      "term" : {
        "field" : "message"
      }
    },
    "my-suggest-2" : {
      "text" : "kmichy",
      "term" : {
        "field" : "user"
      }
    }
  }
}

Multiple suggested queries can use global query text

POST _search
{
  "suggest": {
    "text" : "tring out Elasticsearch",
    "my-suggest-1" : {
      "term" : {
        "field" : "message"
      }
    },
    "my-suggest-2" : {
       "term" : {
        "field" : "user"
       }
    }
  }
}

Suggester introduction

Term suggester

The term term suggestion implements word segmentation of the input text, and provides term suggestions for fuzzy query of each word. Suggested words are not provided by default for the words that exist in the index, and a certain number of suggested words are selected after sorting according to the fuzzy query results for the non-existent words.

Commonly recommended options:

 

text 

Specify search text

field 

Get the search field for suggested words

analyzer 

Designated tokenizer

size 

Maximum number of suggested words returned per word 

sort 

How to sort the suggested words, available options:

Score: sort by score first, then sort by document frequency and term order;

frequency: Sort by document frequency first, and then sort by score and term.

suggest_mode 

Suggestion mode, which controls the way of suggesting words:

missing: Suggested words are provided only when the search term does not exist in the index, the default value;

popular: Only suggest words with a higher document frequency than the search term. 

always: Always provide matching suggestions.

phrase suggester

Phrase suggestion, based on the term, will consider the relationship between multiple terms, such as whether they appear in the original text of the index at the same time, the degree of adjacency, and word frequency, etc.

POST /ftq/_search
{
  "query": {
    "match_all": {}
  },
  
  "suggest" : {
    "myss":{
      "text": "java sprin boot",
      "phrase": {
        "field": "title"
      }
    }
  }
}

Completion suggester automatic completion

A suggestion device designed for automatic completion of scenes. In this scenario, every time the user enters a character, he needs to send a query request to the back-end to find a matching item immediately. When the user input speed is high, the back-end response speed is more demanding. Therefore, in terms of implementation, it uses a different data structure from the previous two Suggesters. The index is not completed by inverting, 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 loaded into the memory by the ES, and the prefix search is extremely fast. But FST can only be used for prefix search, which is also the limitation of Completion Suggester.

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html

In order to use automatic completion, the fields in the index used to provide suggestions for completion need to be specially designed, and the field type is completion.

PUT music
{
    "mappings": {
        "_doc" : {
            "properties" : {
                "suggest" : {  //用于自动补全的字段
                    "type" : "completion"
                },
                "title" : {
                    "type": "keyword"
                }
            }
        }
    }
}
PUT music/_doc/1?refresh
{
    "suggest" : {
        "input": [ "Nevermind", "Nirvana" ],
        "weight" : 34
    }
}
//Input 指定输入词Weight 指定排序值(可选)
PUT music/_doc/1?refresh
{
    "suggest" : [
        {
            "input": "Nevermind",
            "weight" : 10
        },
        {
            "input": "Nirvana",
            "weight" : 3
        }
    ]}
PUT music/_doc/1?refresh
{
  "suggest" : [ "Nevermind", "Nirvana" ]
}
PUT music/_doc/2?refresh
{
    "suggest" : {
        "input": [ "Nevermind", "Nirvana" ],
        "weight" : 20
    }
}
POST music/_search?pretty
{
    "suggest": {
        "song-suggest" : {
            "prefix" : "nir", 
            "completion" : { 
                "field" : "suggest" 
            }
        }
    }
}
POST music/_search?pretty
{
    "suggest": {
        "song-suggest" : {
            "prefix" : "nir", 
            "completion" : { 
                "field" : "suggest",
                "skip_duplicates": true   //去重
            }
        }    }}
PUT music/_doc/3?refresh
{
    "suggest" : {
        "input": [ "lucene solr", "lucene so cool","lucene elasticsearch" ],
        "weight" : 20
    }
}
//存的是短语

PUT music/_doc/4?refresh
{
    "suggest" : {
        "input": ["lucene solr cool","lucene elasticsearch" ],
        "weight" : 10
    }
}

POST music/_search?pretty
{
    "suggest": {
        "song-suggest" : {
            "prefix" : "lucene s", 
            "completion" : { 
                "field" : "suggest" ,
                "skip_duplicates": true
            }
        }
    }
}

 

Guess you like

Origin blog.csdn.net/qq_34050399/article/details/113111805