Upon completion of Elasticsearch, a multi-word search

With the Elasticsearch completion assistant, I am having trouble returning multiple word suggestions matching a single word query.

Structure example:

PUT /test_index/ { "mappings": { "item": { "properties": { "test_suggest": { "type": "completion", "index_analyzer": "whitespace", "search_analyzer": "whitespace", "payloads": false } } } } } PUT /test_index/item/1 { "test_suggest": { "input": [ "cat dog", "elephant" ] } } 

Work request:

 POST /test_index/_suggest { "test_suggest":{ "text":"cat", "completion": { "field" : "test_suggest" } } } 

with the result

 { "_shards": { "total": 5, "successful": 5, "failed": 0 }, "test_suggest": [ { "text": "cat", "offset": 0, "length": 3, "options": [ { "text": "cat dog", "score": 1 } ] } ] } 

The request failed:

 POST /test_index/_suggest { "test_suggest":{ "text":"dog", "completion": { "field" : "test_suggest" } } } 

with the result

 { "_shards": { "total": 5, "successful": 5, "failed": 0 }, "test_suggest": [ { "text": "dog", "offset": 0, "length": 3, "options": [] } ] } 

I would expect the same result as a working request matching "cat dog". Any suggestions, what is the problem, and how to make a failed request work? I get the same results when using a standard parser instead of a space parser. I would like to use a few words per input line, as shown in the example above.

+7
elasticsearch autosuggest search-suggestion
source share
1 answer

The completion assistant is an advester prefix , that is, it tries to match your request with the first few characters of the inputs it inputs. If you want the document that you posted to match the text "dog", you need to specify "dog" as input.

 PUT /test_index/item/1 { "test_suggest": { "input": [ "cat dog", "elephant", "dog" ] } } 

In my experience, limiting what you need to specify input to match makes output hints less useful than other ways to implement prefix matching. I like edge ngrams for this purpose. I recently wrote a blog post about using ngrams that can help you: http://blog.qbox.io/an-introduction-to-ngrams-in-elasticsearch

As a quick example, this is a mapping that you can use

 PUT /test_index { "settings": { "analysis": { "filter": { "edge_ngram_filter": { "type": "edge_ngram", "min_gram": 2, "max_gram": 20 } }, "analyzer": { "edge_ngram_analyzer": { "type": "custom", "tokenizer": "standard", "filter": [ "lowercase", "edge_ngram_filter" ] } } } }, "mappings": { "item": { "properties": { "text_field": { "type": "string", "index_analyzer": "edge_ngram_analyzer", "search_analyzer": "standard" } } } } } 

then index the doc as follows:

 PUT /test_index/item/1 { "text_field": [ "cat dog", "elephant" ] } 

and any of these queries will return it:

 POST /test_index/_search { "query": { "match": { "text_field": "dog" } } } POST /test_index/_search { "query": { "match": { "text_field": "ele" } } } POST /test_index/_search { "query": { "match": { "text_field": "ca" } } } 

Here's the code together:

http://sense.qbox.io/gist/4a08fbb6e42c34ff8904badfaaeecc01139f96cf

+10
source share

All Articles