# Type Ahead Suggestions using Elastic Search

**URL:** <https://discuss.elastic.co/t/type-ahead-suggestions-using-elastic-search/84279>\
**Category:** Elasticsearch\
**Created:** [May 2, 2017, 2:02pm UTC](https://discuss.elastic.co/t/type-ahead-suggestions-using-elastic-search/84279 "2017-05-02T14:02:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Madhup\_Narain](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@Madhup\_Narain](https://discuss.elastic.co/u/Madhup_Narain)\
**Post date:** [May 2, 2017, 2:02pm UTC](https://discuss.elastic.co/t/type-ahead-suggestions-using-elastic-search/84279/1 "2017-05-02T14:02:55Z")

</div>

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

> <https://stackoverflow.com/questions/41744712/word-oriented-completion-suggester-elasticsearch-5-x>

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

---

<div class="post-metadata">

**Author:** ![Abhilash\_Bolla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abhilash_bolla/32/26840_2.png) [@Abhilash\_Bolla](https://discuss.elastic.co/u/Abhilash_Bolla)\
**Post date:** [May 4, 2017, 10:57am UTC](https://discuss.elastic.co/t/type-ahead-suggestions-using-elastic-search/84279/2 "2017-05-04T10:57:20Z")

</div>

You may try using a **match\_phrase** query with slop 0 instead of a **term** query.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 1, 2017, 11:01am UTC](https://discuss.elastic.co/t/type-ahead-suggestions-using-elastic-search/84279/3 "2017-06-01T11:01:58Z")

</div>

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