# Help with Autocomplete (Search Suggestion)

**URL:** https://discuss.elastic.co/t/help-with-autocomplete-search-suggestion/9983
**Category:** Elasticsearch
**Created:** [December 7, 2012, 2:11am UTC](https://discuss.elastic.co/t/help-with-autocomplete-search-suggestion/9983 "2012-12-07T02:11:16Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Robert\_Abnous](https://avatars.discourse-cdn.com/v4/letter/r/9f8e36/32.png) [@Robert\_Abnous](https://discuss.elastic.co/u/Robert_Abnous)
#### Post date: [December 7, 2012, 2:11am UTC](https://discuss.elastic.co/t/help-with-autocomplete-search-suggestion/9983/1 "2012-12-07T02:11:16Z")

</div>

Hi everyone,

I have put together the following mapping for a Catalog of Items (using  
nGrams):

"mappings":{  
"item":{  
"properties":{  
"productName":{  
"fields" : {  
"partial":{ "search\_analyzer":"full\_name",  
"index\_analyzer":"partial\_name", "type":"string" },  
"partial\_back":{ "search\_analyzer":"full\_name",  
"index\_analyzer":"partial\_name\_back", "type":"string" },  
"partial\_middle":{ "search\_analyzer":"full\_name",  
"index\_analyzer":"partial\_middle\_name", "type":"string" },  
"productName":{ "type":"string", "analyzer":"full\_name" }  
},  
"type":"multi\_field"  
},  
"productID":{ "type":"string", "analyzer":"simple" },  
"vendor":{ "type":"string", "analyzer":"simple" },  
"productDescription":{ "type":"string", "analyzer":"full\_name"}  
}  
}  
},  
"settings":{  
"analysis":{  
"filter":{  
"name\_ngrams":{ "side":"front", "max\_gram":50, "min\_gram":2,  
"type":"edgeNGram" },  
"name\_ngrams\_back":{ "side":"back", "max\_gram":50,  
"min\_gram":2, "type":"edgeNGram" },  
"name\_middle\_ngrams":{ "type":"nGram", "max\_gram":50,  
"min\_gram":2 }  
},  
"analyzer":{  
"full\_name":{  
"filter":["standard", "lowercase", "asciifolding"],  
"type":"custom",  
"tokenizer":"standard"  
},  
"partial\_name":{  
"filter":[ "standard", "lowercase", "asciifolding",  
"name\_ngrams" ],  
"type":"custom",  
"tokenizer":"standard"  
},  
"partial\_name\_back":{  
"filter":[ "standard", "lowercase", "asciifolding",  
"name\_ngrams\_back" ],  
"type":"custom",  
"tokenizer":"standard"  
},  
"partial\_middle\_name":{  
"filter":[ "standard", "lowercase", "asciifolding",  
"name\_middle\_ngrams" ],  
"type":"custom",  
"tokenizer":"standard"  
}  
}  
}  
}

and then populated some data in it (like the following:)

================= Data Population

curl -XPUT '[http://localhost:9200/store/item/1?pretty=true](http://localhost:9200/store/item/1?pretty=true)' -d '  
{  
"productName":"XBox 360 2Gig",  
"productID":"1",  
"vendor":"Amazon",  
"productDescription": "XBox uncofingure"  
}  
'

curl -XPUT '[http://localhost:9200/store/item/2?pretty=true](http://localhost:9200/store/item/2?pretty=true)' -d '  
{  
"productName":"XBox 360 2Gig",  
"productID":"2",  
"vendor":"Buy",  
"productDescription": "XBox configured from Buy"  
}  
'

curl -XPUT '[http://localhost:9200/store/item/3?pretty=true](http://localhost:9200/store/item/3?pretty=true)' -d '  
{  
"productName":"XBox 360 2Gig",  
"productID":"3",  
"vendor":"BestBuy",  
"productDescription": "XBox from Best Buy"  
}  
'

curl -XPUT '[http://localhost:9200/store/item/4?pretty=true](http://localhost:9200/store/item/4?pretty=true)' -d '  
{  
"productName":"XBox 4Gig",  
"productID":"4",  
"vendor":"Amazon",  
"productDescription": "XBox from Amazon"  
}  
'

curl -XPUT '[http://localhost:9200/store/item/5?pretty=true](http://localhost:9200/store/item/5?pretty=true)' -d '  
{  
"productName":"ipad 32Gig",  
"productID":"5",  
"vendor":"BestBuy",  
"productDescription": "ipad from Amazon"  
}  
'

As you notice I have duplicates in the productName.

I am struggling to put together a query that I can use for Autocomplete  
(Search Suggestion) correctly (on the productName).

I will appreciate any suggestions.

Thanks,  
Robert

--

---

<div class="post-metadata">

### Author: ![A\_Zed](https://avatars.discourse-cdn.com/v4/letter/a/eb8c5e/32.png) [@A\_Zed](https://discuss.elastic.co/u/A_Zed)
#### Post date: [December 7, 2012, 5:24am UTC](https://discuss.elastic.co/t/help-with-autocomplete-search-suggestion/9983/2 "2012-12-07T05:24:17Z")

</div>

Try this:  
(replace "YOUR\_QUERY" with, say, "ipa" or some such substring)

curl -XGET '[http://localhost:9200/store/item/\_search?pretty=1](http://localhost:9200/store/item/_search?pretty=1)' -d '  
{  
"fields":["productName","vendor","productDescription"],  
"query":{  
"bool":{  
"should":[  
{  
"text":{  
"productName":{

"query":"YOUR\_QUERY",  
"boost":1  
}  
}  
},  
{  
"text":{

"productName.partial":"YOUR\_QUERY"  
}  
}  
]  
}  
}  
}'

--

---

<div class="post-metadata">

### Author: ![Robert\_Abnous](https://avatars.discourse-cdn.com/v4/letter/r/9f8e36/32.png) [@Robert\_Abnous](https://discuss.elastic.co/u/Robert_Abnous)
#### Post date: [December 7, 2012, 5:53pm UTC](https://discuss.elastic.co/t/help-with-autocomplete-search-suggestion/9983/3 "2012-12-07T17:53:31Z")

</div>

Thanks for reply. Since, I have duplicate names in the productName, I think  
I should use a Facet search.

I have duplicates in the productName, and for Autocomplete/Search  
Suggestion, I do not want to show duplicates.

On Thursday, December 6, 2012 9:24:17 PM UTC-8, A Zed wrote:

> Try this:  
> (replace "YOUR\_QUERY" with, say, "ipa" or some such substring)
> 
> curl -XGET '[http://localhost:9200/store/item/\_search?pretty=1](http://localhost:9200/store/item/_search?pretty=1)' -d '  
> {  
> "fields":["productName","vendor","productDescription"],  
> "query":{  
> "bool":{  
> "should":[  
> {  
> "text":{  
> "productName":{
> 
> "query":"YOUR\_QUERY",  
> "boost":1  
> }  
> }  
> },  
> {  
> "text":{
> 
> "productName.partial":"YOUR\_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: [July 6, 2017, 3:00am UTC](https://discuss.elastic.co/t/help-with-autocomplete-search-suggestion/9983/4 "2017-07-06T03:00:50Z")

</div>


