# Need advice for my term completion feature

**URL:** <https://discuss.elastic.co/t/need-advice-for-my-term-completion-feature/43683>\
**Category:** Elasticsearch\
**Created:** [March 7, 2016, 5:16pm UTC](https://discuss.elastic.co/t/need-advice-for-my-term-completion-feature/43683 "2016-03-07T17:16:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![basiclaser](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/basiclaser/32/23304_2.png) [@basiclaser](https://discuss.elastic.co/u/basiclaser)\
**Post date:** [March 7, 2016, 5:16pm UTC](https://discuss.elastic.co/t/need-advice-for-my-term-completion-feature/43683/1 "2016-03-07T17:16:12Z")

</div>

I have a website with a searchbar where users can search for documents.  
Currently I am using a term suggester ( [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-term.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-term.html) ) to provide word suggestions to people as they type their queries.  
This approach is 99% perfect, except that I want a greater edit distance / Levenstein distance for the suggestions.  
It seems the maximum `max_edits` is set to 2, which AFAIK means that if a user is typing 'subspace', they have to type 'subspa' before they get a matching suggestion.  
I would prefer it is the matching suggestion was available after only 3-4 characters were typed eg.'subs'. It seems this is a limitation within term suggester.

Is there any way of circumventing this and increasing the edit distance, or switching the suggester to prefix functionality?

I am considering other methods such as prefix-filters ( [https://www.elastic.co/guide/en/elasticsearch/reference/1.4/query-dsl-prefix-filter.html](https://www.elastic.co/guide/en/elasticsearch/reference/1.4/query-dsl-prefix-filter.html) ) . Is it possible to use prefix-filters and extract and return only the matching terms, not the whole matching documents?

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![msimos](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@msimos](https://discuss.elastic.co/u/msimos)\
**Post date:** [March 7, 2016, 10:30pm UTC](https://discuss.elastic.co/t/need-advice-for-my-term-completion-feature/43683/2 "2016-03-07T22:30:21Z")

</div>

Hi,

Have you seen these two examples in the definitive guide:

[https://www.elastic.co/guide/en/elasticsearch/guide/current/\_query\_time\_search\_as\_you\_type.html](https://www.elastic.co/guide/en/elasticsearch/guide/current/_query_time_search_as_you_type.html)

[https://www.elastic.co/guide/en/elasticsearch/guide/current/\_index\_time\_search\_as\_you\_type.html](https://www.elastic.co/guide/en/elasticsearch/guide/current/_index_time_search_as_you_type.html)

Take a look at these as they may help. Probably the second example using edge\_ngram will work best.

---

<div class="post-metadata">

**Author:** ![basiclaser](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/basiclaser/32/23304_2.png) [@basiclaser](https://discuss.elastic.co/u/basiclaser)\
**Post date:** [March 8, 2016, 10:26am UTC](https://discuss.elastic.co/t/need-advice-for-my-term-completion-feature/43683/3 "2016-03-08T10:26:16Z")

</div>

Msimos thanks for helping.  
My issue with this is that I just want the **_matching terms_** to be returned, not the **_documents_** containing the matching terms.

So if i query 'sub', i want 'subspace', 'substring', 'subzero' to be returned. Is there a way to do this with queries?

---

<div class="post-metadata">

**Author:** ![basiclaser](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/basiclaser/32/23304_2.png) [@basiclaser](https://discuss.elastic.co/u/basiclaser)\
**Post date:** [March 8, 2016, 12:24pm UTC](https://discuss.elastic.co/t/need-advice-for-my-term-completion-feature/43683/4 "2016-03-08T12:24:20Z")

</div>

My issue is that _term suggester_ has a limit of **2 edit distance**. Could I solve this problem by adding edge Ngrams to the field which i want to perform autocompletions on?

---

<div class="post-metadata">

**Author:** ![msimos](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@msimos](https://discuss.elastic.co/u/msimos)\
**Post date:** [March 8, 2016, 5:33pm UTC](https://discuss.elastic.co/t/need-advice-for-my-term-completion-feature/43683/5 "2016-03-08T17:33:50Z")

</div>

Hi,

If there is a specific field you are using autocomplete for, then you can use the "fields" directive to just return that field and not the whole document. Like:

[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-fields.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-fields.html)

```auto
GET /my_index/my_type/_search
{
  "fields": ["name"], 
    "query": {
        "match": {
            "name": "yel"
        }
    }
}

```

Which will just return:

```auto
            "fields": {
               "name": [
                  "Yellow furballs"
               ]
            }

```

So at index time, you can add a list of terms into your documents which you can use for autocomplete. Or you can use any existing fields in your documents like title, city or whatever makes sense.

Here's a useful comparison of the different autosuggest methods:

[http://techblog.realestate.com.au/implementing-autosuggest-in-elasticsearch/](http://techblog.realestate.com.au/implementing-autosuggest-in-elasticsearch/)

---

<div class="post-metadata">

**Author:** ![msimos](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@msimos](https://discuss.elastic.co/u/msimos)\
**Post date:** [March 8, 2016, 5:50pm UTC](https://discuss.elastic.co/t/need-advice-for-my-term-completion-feature/43683/6 "2016-03-08T17:50:37Z")

</div>

One last thing, you can also use the highlighter to get the term like this:

```auto
GET /my_index/my_type/_search
{
  "fields": ["name"], 
    "query": {
        "match": {
            "name": "yel"
        }
    },
    "highlight": {
      "fields": {
        "name": {}
      }
    }
}

```

```auto
      "hits": [
         {
            "_index": "my_index",
            "_type": "my_type",
            "_id": "2",
            "_score": 0.625,
            "fields": {
               "name": [
                  "Yellow furballs"
               ]
            },
            "highlight": {
               "name": [
                  "<em>Yellow</em> furballs"
               ]
            }
         }

```

Then you need to parse out the term(s) which have been highlighted.

---

<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:** [July 5, 2017, 11:10pm UTC](https://discuss.elastic.co/t/need-advice-for-my-term-completion-feature/43683/7 "2017-07-05T23:10:15Z")

</div>


