Elasticsearch completion suggest search with multiple-word inputs

Neman picture Neman · Apr 20, 2015 · Viewed 10k times · Source

Using the Elasticsearch completion suggester I have problems returning multi-word input suggestions matching a one-word query.

Example structure:

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"
      ]
   }
}

Working query:

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

with result

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

Failing query:

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

with result

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

I would expect the same result as the working query, matching 'cat dog'. Any suggestions what the problem is and how to make the failing query working? I get the same results when using the standard analyzer instead of the whitespace analyzer. I would like to use multiple words per input string as showed in the example above.

Answer

Sloan Ahrens picture Sloan Ahrens · Apr 20, 2015

The completion suggester is a prefix suggester, meaning it tries to match your query to the first few characters of the inputs that it's been given. If you want the document you posted to match the text "dog", then you'll need to specify "dog" as an input.

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

In my experience, the limitation of having to specify inputs to match makes completion suggesters less useful that other ways to implement prefix matching. I like edge ngrams for this purpose. I recently wrote a blog post about using ngrams that you might find helpful: http://blog.qbox.io/an-introduction-to-ngrams-in-elasticsearch

As a quick example, here is a mapping you could 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 like this:

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 all together:

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