Type Ahead Suggestions using Elastic Search

Firstly would like to post my requirement

  1. I should be able to provide type ahead suggestions to users.
  2. The type ahead should be based on a field contains an Array of suggestions
  3. The suggestions being returned to user should not contain duplicates

For instance, i have the following settings and mappings

PUT test-index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "completion_analyzer": {
          "type": "custom",
          "filter": [
            "lowercase",
            "completion_filter"
          ],
          "tokenizer": "keyword"
        }
      },
      "filter": {
        "completion_filter": {
          "type": "edge_ngram",
          "min_gram": 1,
          "max_gram": 24
        }
      }
    }
  },
  "mappings": {
    "users": {
      "properties": {
        "autocomplete": {
          "type": "text",
          "fields": {
            "raw": {
              "type": "keyword"
            },
            "completion": {
              "type": "text",
              "analyzer": "completion_analyzer",
              "search_analyzer": "standard"
            }
          }
        },
        "firstName": {
          "type": "text"
        },
        "lastName": {
          "type": "text"
        }
      }
    }
  }
}

POST test-index/users/_bulk
{"index":{"_id" : "4"}}
{ "firstName": "John", "lastName": "Doe", "autocomplete": "John Doe"}
{"index":{"_id" : "5"}}
{ "firstName": "John", "lastName": "Deere", "autocomplete": "John Deere" }
{"index":{"_id" : "6"}}
{ "firstName": "Johnny", "lastName": "Cash", "autocomplete": ["Johnny Cash","John Rambo"] }

On searching for john, i get desired results

POST test-index/_search
{
  "size":0,
  "query": {
    "term": {
      "autocomplete.completion": "john"
    }
  },
"_source" : true ,
  "aggs": {
    "suggestions": {
      "terms": {
        "field": "autocomplete.raw"
      }
    }
  }
}

When Searching for .... john r , i get the results as
John Rambo
Johnny Cash
i had expected to get John Rambo

I tried the example provided under the following read

The above does not return results correcly when suggestions are part of the array, and seems to return entire array even if there was one ngram match

Also I can not use the completion suggester as it is document oriented and returns duplicates

You may try using a match_phrase query with slop 0 instead of a term query.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.