# ElasticSearch Auto Completion and Term/Phrase Suggesters

**URL:** <https://discuss.elastic.co/t/elasticsearch-auto-completion-and-term-phrase-suggesters/15027>\
**Category:** Elasticsearch\
**Created:** [December 26, 2013, 10:11pm UTC](https://discuss.elastic.co/t/elasticsearch-auto-completion-and-term-phrase-suggesters/15027 "2013-12-26T22:11:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ramdev\_Wudali](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ramdev_wudali/32/7894_2.png) [@Ramdev\_Wudali](https://discuss.elastic.co/u/Ramdev_Wudali)\
**Post date:** [December 26, 2013, 10:11pm UTC](https://discuss.elastic.co/t/elasticsearch-auto-completion-and-term-phrase-suggesters/15027/1 "2013-12-26T22:11:34Z")

</div>

Hi All:  
I am looking into providing a phrase suggester/ inline completion  
feature for search. I have been looking at the features as provided in  
Elasticsearch. However, I have not been able to understand the following :

1. Term/Phrase suggesters - Are these features search time features and  
How does mapping of content play into this feature ?
2. Auto Completion - I think I a working example :

(sample mapping provided :

```
"Titles": {
    "properties": {
        "DOC_ID": {
            "type":"string"
        },
        "TITLE": {
            "type": "string"
        },
        "TITLE_suggest" : {
            "type" : "completion",
            "index_analyzer" : "simple",
            "search_analyzer" : "simple",
            "payloads" : true
        }
    }
}

```

)

For content indexed , I am able to execute requests like the following : :

curl -XPOST http:/host:920/index/\_suggest -d  
'{"Titles":{"text":"on","completion":{"field":"TITLE\_suggest"}}}'

and I get back suggestions. But only those that start with the phrase "on".

{"\_shards":{"total":5,"successful":5,"failed":0},"Titles":[{"text":"on","offset":0,"length":2,"options":[{"text":"'Once  
Upon A Time...' tells a torrid tale of forbidden love and eventual  
betrayal","score":1.0},{"text":"On Drill Program  
Progress\<DRK.V\>","score":1.0},{"text":"On Foreign  
Shores","score":1.0},{"text":"On golden pond: Back to back Olympic  
champs","score":1.0},{"text":"Once Upon A Time In Mumbaai Dobaara fails to  
impress critics","score":1.0}]}]}

However, What I want to get is, where the phrase" on" appears in the title,  
as part of a term or a phrase. How would I tweak my request or making to  
be able to get that ?

Thanks much

Ramdev

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/7edd9a45-3fa5-4dae-bfb0-19b0df700e62%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7edd9a45-3fa5-4dae-bfb0-19b0df700e62%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Sloan\_Ahrens](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sloan_ahrens/32/1828_2.png) [@Sloan\_Ahrens](https://discuss.elastic.co/u/Sloan_Ahrens)\
**Post date:** [December 29, 2013, 1:15am UTC](https://discuss.elastic.co/t/elasticsearch-auto-completion-and-term-phrase-suggesters/15027/2 "2013-12-29T01:15:20Z")

</div>

It sounds like completion suggest may not be exactly what you want. One way you could solve your problem is to use a shingle (ngram) analyzer in your mapping, combined with a prefix query. A description of the basic technique can be found here: [http://developer.rackspace.com/blog/qbox.html#.UZ0yEWRATQ4](http://developer.rackspace.com/blog/qbox.html#.UZ0yEWRATQ4)

Qbox has a demo (here: [http://be6c2e3260c3e2af000.qbox.io/\_plugin/demo/tablemap/index.html](http://be6c2e3260c3e2af000.qbox.io/_plugin/demo/tablemap/index.html)) that uses this technique for auto-complete. Queries are matched using a prefix query, against a field that has been analyzed using a shingle filter.

So, adapted to the information you've given, a solution might look like this:

Create the index with the following analyzer:

curl -XPUT http://[endpoint]/[index\_name]  
{  
"settings": {  
"analysis": {  
"filter": {  
"shingle\_filter": {  
"type": "shingle",  
"min\_shingle\_size": 2,  
"max\_shingle\_size": 5  
}  
},  
"analyzer": {  
"shingle\_analyzer": {  
"type": "custom",  
"tokenizer": "standard",  
"filter": [  
"lowercase",  
"shingle\_filter"  
]  
}  
}  
}  
}  
}

and then apply this mapping:

curl -XPUT http://[endpoint]/[index\_name]/titles/\_mapping  
{  
"titles": {  
"properties": {  
"DOC\_ID": {  
"type":"string"  
},  
"TITLE": {  
"type": "string",  
"index\_analyzer": "shingle\_analyzer"  
}  
}  
}  
}

once you have some documents indexed you can query with "on" using the following query structure

curl -XPOST http://[endpoint]/\_search  
{  
"query": {  
"prefix": {  
"TITLE": "on"  
}  
}  
}

This should give you the behavior you are looking for.

---

<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 6, 2017, 1:59am UTC](https://discuss.elastic.co/t/elasticsearch-auto-completion-and-term-phrase-suggesters/15027/3 "2017-07-06T01:59:10Z")

</div>


