# Autocompletion

**URL:** https://discuss.elastic.co/t/autocompletion/7921
**Category:** Elasticsearch
**Created:** [May 30, 2012, 8:10pm UTC](https://discuss.elastic.co/t/autocompletion/7921 "2012-05-30T20:10:03Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![phoenix](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/phoenix/32/1319_2.png) [@phoenix](https://discuss.elastic.co/u/phoenix)
#### Post date: [May 30, 2012, 8:10pm UTC](https://discuss.elastic.co/t/autocompletion/7921/1 "2012-05-30T20:10:03Z")

</div>

Hi everyone,

I'm looking for a way to implement an autocomplete feature using elasticsearch.  
Does someone have tips about the way to achieve this ?  
Kimchy, the search bar on elasticsearch sitenis exactly what i'd like to do. Can you tell us how you did it?  
What kind of mapping? Hownyou used the api to implement it ?

Thanks,  
Frederic

---

<div class="post-metadata">

### Author: ![zhouxiang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zhouxiang/32/2775_2.png) [@zhouxiang](https://discuss.elastic.co/u/zhouxiang)
#### Post date: [May 31, 2012, 5:48am UTC](https://discuss.elastic.co/t/autocompletion/7921/2 "2012-05-31T05:48:35Z")

</div>

use facet

在 2012年5月31日星期四UTC+8上午4时10分03秒，Frederic Esnault写道：

> Hi everyone,
> 
> I'm looking for a way to implement an autocomplete feature using  
> elasticsearch.  
> Does someone have tips about the way to achieve this ?  
> Kimchy, the search bar on elasticsearch sitenis exactly what i'd like to  
> do. Can you tell us how you did it?  
> What kind of mapping? Hownyou used the api to implement it ?
> 
> Thanks,  
> Frederic

---

<div class="post-metadata">

### Author: ![Nick\_Dunn](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nick_dunn/32/2315_2.png) [@Nick\_Dunn](https://discuss.elastic.co/u/Nick_Dunn)
#### Post date: [May 31, 2012, 8:33am UTC](https://discuss.elastic.co/t/autocompletion/7921/3 "2012-05-31T08:33:07Z")

</div>

I usually implement autocomplete with multi field types. Let's say for  
example you have two document types: Articles and Comments (for a blog) and  
you want autocomplete on the article title and commenter name. When you map  
these types, set these fields up as multifields  
([Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/multi-field-type.html)),  
with one of the sub-fields named "autocomplete". To query these you can use  
a simple query\_string search, and use the wildcard syntax to search only  
the fields that are mapped with the autocomplete sub-field. In your query  
set the "fields" param to "\*.autocomplete", which will run the search on  
any field with an autocomplete sub-field.

This way, I write my autocomplete query once, and then define what fields  
are searchable using my type mappings.

On Wednesday, May 30, 2012 9:10:03 PM UTC+1, Frederic Esnault wrote:

> Hi everyone,
> 
> I'm looking for a way to implement an autocomplete feature using  
> elasticsearch.  
> Does someone have tips about the way to achieve this ?  
> Kimchy, the search bar on elasticsearch sitenis exactly what i'd like to  
> do. Can you tell us how you did it?  
> What kind of mapping? Hownyou used the api to implement it ?
> 
> Thanks,  
> Frederic

---

<div class="post-metadata">

### Author: ![David\_G\_Ortega](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/david_g_ortega/32/46359_2.png) [@David\_G\_Ortega](https://discuss.elastic.co/u/David_G_Ortega)
#### Post date: [May 31, 2012, 11:54am UTC](https://discuss.elastic.co/t/autocompletion/7921/4 "2012-05-31T11:54:17Z")

</div>

"...use the wildcard syntax to search only the fields that are mapped  
with the autocomplete sub-field"

Is your index big? What about your index workload, is updated  
frecuently?  
I'm really interested to see how this performs in big indices with  
high updates ratings.

---

<div class="post-metadata">

### Author: ![slushi](https://avatars.discourse-cdn.com/v4/letter/s/3e96dc/32.png) [@slushi](https://discuss.elastic.co/u/slushi)
#### Post date: [May 31, 2012, 7:55pm UTC](https://discuss.elastic.co/t/autocompletion/7921/5 "2012-05-31T19:55:48Z")

</div>

Interesting approach. I actually need to do a slightly different  
auto-complete - I have a bunch of article text and would like to  
autocomplete by topic. So in the example below, if there were titles  
"phillips screwdrivers" and "flathead screwdrivers", if the user types  
"screw", I want to autocomplete to "screwdrivers" in the search box. Then  
the user can submit a "screwdrivers" query and both titles should match. I  
tried a match all query with a terms facet that did a pattern match  
(screw.\*) but I got an OutOfMemoryException. Is there a better way to do  
this or some way to estimate how much memory I need to do the facet query?

On Thursday, May 31, 2012 4:33:07 AM UTC-4, Nick Dunn wrote:

> I usually implement autocomplete with multi field types. Let's say for  
> example you have two document types: Articles and Comments (for a blog) and  
> you want autocomplete on the article title and commenter name. When you map  
> these types, set these fields up as multifields (  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/multi-field-type.html)),  
> with one of the sub-fields named "autocomplete". To query these you can use  
> a simple query\_string search, and use the wildcard syntax to search only  
> the fields that are mapped with the autocomplete sub-field. In your query  
> set the "fields" param to "\*.autocomplete", which will run the search on  
> any field with an autocomplete sub-field.
> 
> This way, I write my autocomplete query once, and then define what fields  
> are searchable using my type mappings.
> 
> On Wednesday, May 30, 2012 9:10:03 PM UTC+1, Frederic Esnault wrote:
> 
> > Hi everyone,
> > 
> > I'm looking for a way to implement an autocomplete feature using  
> > elasticsearch.  
> > Does someone have tips about the way to achieve this ?  
> > Kimchy, the search bar on elasticsearch sitenis exactly what i'd like to  
> > do. Can you tell us how you did it?  
> > What kind of mapping? Hownyou used the api to implement it ?
> > 
> > Thanks,  
> > Frederic

---

<div class="post-metadata">

### Author: ![phoenix](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/phoenix/32/1319_2.png) [@phoenix](https://discuss.elastic.co/u/phoenix)
#### Post date: [May 31, 2012, 7:56pm UTC](https://discuss.elastic.co/t/autocompletion/7921/6 "2012-05-31T19:56:23Z")

</div>

Thx Nick for this tip.  
Actually i'm going to search on a FAQ questions and answers.  
The autocomplete method is not going to be a bit too much on answers (say 5 to 10 sentences in each answer)?

My index is going to have a maximum of 15 categories of questions, each holding an unlimited amount of question/answer pairs).  
I think we won't go up to 50 questions in each category.  
this is not such a big index, sorry 🙂

About facets i'm not sure how it would help. Facets can help to make stats on, for example, how many question/answer pairs contain a certain word.  
As an example, (the FAQ is about cars), i could display the number of questions dealing with gas, with security, and so on. Or do i misunderstand facets ?

I'm actually quite intested in the multifields method. I'll check this out.

Any more opinions ?

---

<div class="post-metadata">

### Author: ![phoenix](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/phoenix/32/1319_2.png) [@phoenix](https://discuss.elastic.co/u/phoenix)
#### Post date: [May 31, 2012, 7:56pm UTC](https://discuss.elastic.co/t/autocompletion/7921/7 "2012-05-31T19:56:23Z")

</div>

Thx Nick for this tip.  
Actually i'm going to search on a FAQ questions and answers.  
The autocomplete method is not going to be a bit too much on answers (say 5 to 10 sentences in each answer)?

My index is going to have a maximum of 15 categories of questions, each holding an unlimited amount of question/answer pairs).  
I think we won't go up to 50 questions in each category.  
this is not such a big index, sorry 🙂

About facets i'm not sure how it would help. Facets can help to make stats on, for example, how many question/answer pairs contain a certain word.  
As an example, (the FAQ is about cars), i could display the number of questions dealing with gas, with security, and so on. Or do i misunderstand facets ?

I'm actually quite intested in the multifields method. I'll check this out.

Any more opinions ?

---

<div class="post-metadata">

### Author: ![phoenix](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/phoenix/32/1319_2.png) [@phoenix](https://discuss.elastic.co/u/phoenix)
#### Post date: [May 31, 2012, 7:59pm UTC](https://discuss.elastic.co/t/autocompletion/7921/8 "2012-05-31T19:59:55Z")

</div>

Just a dumb question after reading the doc on multi fields. Does it mean the value is indexed twice ? I mean it takes two times the space on the disk/memory ?

---

<div class="post-metadata">

### Author: ![Tanguy1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tanguy1/32/1749_2.png) [@Tanguy1](https://discuss.elastic.co/u/Tanguy1)
#### Post date: [June 1, 2012, 7:24am UTC](https://discuss.elastic.co/t/autocompletion/7921/9 "2012-06-01T07:24:54Z")

</div>

Yes, your field will be mapped n times and more space will be required,  
depending on the mapping.

-- Tanguy

Le jeudi 31 mai 2012 21:59:55 UTC+2, Frederic Esnault a écrit :

> Just a dumb question after reading the doc on multi fields. Does it mean  
> the value is indexed twice ? I mean it takes two times the space on the  
> disk/memory ?

---

<div class="post-metadata">

### Author: ![phoenix](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/phoenix/32/1319_2.png) [@phoenix](https://discuss.elastic.co/u/phoenix)
#### Post date: [June 1, 2012, 8:14am UTC](https://discuss.elastic.co/t/autocompletion/7921/10 "2012-06-01T08:14:53Z")

</div>

Ok thanx, i'll see how it can fit, but the approach seems really  
interesting.  
And by the way i saw an article about autocomplete with solar, and they  
created also an autocomplete field.

Another question, are NGrams useful for this usage? And if yes, how ?

On Friday, June 1, 2012 9:24:54 AM UTC+2, Tanguy wrote:

> Yes, your field will be mapped n times and more space will be required,  
> depending on the mapping.
> 
> -- Tanguy
> 
> Le jeudi 31 mai 2012 21:59:55 UTC+2, Frederic Esnault a écrit :
> 
> > Just a dumb question after reading the doc on multi fields. Does it mean  
> > the value is indexed twice ? I mean it takes two times the space on the  
> > disk/memory ?

---

<div class="post-metadata">

### Author: ![Iftekharul\_Haque](https://avatars.discourse-cdn.com/v4/letter/i/e19adc/32.png) [@Iftekharul\_Haque](https://discuss.elastic.co/u/Iftekharul_Haque)
#### Post date: [June 1, 2012, 8:41am UTC](https://discuss.elastic.co/t/autocompletion/7921/11 "2012-06-01T08:41:55Z")

</div>

On Fri, Jun 1, 2012 at 10:14 AM, Frederic Esnault  
[esnault.frederic@gmail.com](mailto:esnault.frederic@gmail.com) wrote:

> Another question, are NGrams useful for this usage? And if yes, how ?

Ngrams are useful to do matches with partial strings (particularly  
useful for autocomplete since you're matching partially input words)  
and matches with typographical errors (generally useful). The ngram  
features on ES are quite powerful, but do note that if you have lots  
of terms to be analyzed by a wide range of ngrams, it does take up a  
lot of memory.

- Ifty.

---

<div class="post-metadata">

### Author: ![David\_G\_Ortega](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/david_g_ortega/32/46359_2.png) [@David\_G\_Ortega](https://discuss.elastic.co/u/David_G_Ortega)
#### Post date: [June 1, 2012, 10:45am UTC](https://discuss.elastic.co/t/autocompletion/7921/12 "2012-06-01T10:45:39Z")

</div>

"...are NGrams useful for this usage? And if yes, how ?..."

ngrams are the way I have gone to do autocompletion and probably the  
way almost everyone here uses.  
Thats why I'm asking how was performing something that IMHO is a  
beast.  
Wildcard queries over a large index is not the best idea unless you  
dont mind the response time or has sharded with lots of machines.

To autocomplete with ngram just create a custom analyzer that do what  
you want plus ngram, thats making the index bigger but  
you have the possibility of search by ngram which are essentially  
parts of a word.

Does this makes sense to U?

---

<div class="post-metadata">

### Author: ![phoenix](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/phoenix/32/1319_2.png) [@phoenix](https://discuss.elastic.co/u/phoenix)
#### Post date: [June 1, 2012, 12:56pm UTC](https://discuss.elastic.co/t/autocompletion/7921/13 "2012-06-01T12:56:53Z")

</div>

Well i think i get the point, i just need to get up to the speed with  
ngrams, and how to implement an analyzer.  
I guess the best source to look for this is lucene, right ?

On Friday, June 1, 2012 12:45:39 PM UTC+2, David G Ortega wrote:

> "...are NGrams useful for this usage? And if yes, how ?..."
> 
> ngrams are the way I have gone to do autocompletion and probably the  
> way almost everyone here uses.  
> Thats why I'm asking how was performing something that IMHO is a  
> beast.  
> Wildcard queries over a large index is not the best idea unless you  
> dont mind the response time or has sharded with lots of machines.
> 
> To autocomplete with ngram just create a custom analyzer that do what  
> you want plus ngram, thats making the index bigger but  
> you have the possibility of search by ngram which are essentially  
> parts of a word.
> 
> Does this makes sense to U?

---

<div class="post-metadata">

### Author: ![phoenix](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/phoenix/32/1319_2.png) [@phoenix](https://discuss.elastic.co/u/phoenix)
#### Post date: [June 1, 2012, 2:49pm UTC](https://discuss.elastic.co/t/autocompletion/7921/14 "2012-06-01T14:49:35Z")

</div>

I tried creating an index with a mapping, specifying a tokenizer and  
analyzers.

XContentBuilder settings = jsonBuilder()  
.startObject()  
.startObject("analysis")  
.startObject("analyzer")  
.startObject("response\_search\_analyzer")  
.field("tokenizer", "responseTokenizer")  
.field("filter", "lowercase")  
.endObject()  
.startObject("response\_index\_analyzer")  
.field("tokenizer", "responseTokenizer")  
.field("filter", "lowercase", "nGram")  
.endObject()  
.endObject()  
.startObject("tokenizer")  
.startObject("responseTokenizer")  
.field("type", "whitespace")  
.endObject()  
.endObject()  
.startObject("filter")  
.startObject("nGram")  
.field("type", "nGram")  
.field("min\_ngram", 3)  
.field("max\_ngram", 6)  
.endObject()  
.endObject()  
.endObject()  
.endObject();

XContentBuilder mapping = jsonBuilder()  
.startObject()  
.startObject("question")  
.startObject("properties")  
.startObject("responseDescription")  
.field("type", "string")  
.field("search\_analyzer",  
"response\_search\_analyzer")  
.field("index\_analyzer",  
"response\_index\_analyzer")  
.endObject()  
.endObject()  
.endObject()  
.endObject();

Then i create the index this way :  
CreateIndexResponse response =  
client.admin().indices().prepareCreate("faq-ze").setSettings(createSettings())  
.addMapping("question",  
createMapping()).execute().actionGet();

Resulting settings are these :  
curl -XGET  
'[http://192.168.6.159:9202/faq-ze/\_settings?pretty=1](http://192.168.6.159:9202/faq-ze/_settings?pretty=1)'  
{  
"faq-ze" : {  
"settings" : {  
"index.analysis.analyzer.response\_index\_analyzer.filter.0" :  
"lowercase",  
"index.analysis.analyzer.response\_index\_analyzer.filter.1" : "nGram",  
"index.analysis.tokenizer.responseTokenizer.type" : "whitespace",  
"index.analysis.analyzer.response\_index\_analyzer.tokenizer" :  
"responseTokenizer",  
"index.analysis.analyzer.response\_search\_analyzer.filter" :  
"lowercase",  
"index.analysis.filter.nGram.min\_ngram" : "3",  
"index.analysis.filter.nGram.type" : "nGram",  
"index.analysis.filter.nGram.max\_ngram" : "6",  
"index.analysis.analyzer.response\_search\_analyzer.tokenizer" :  
"responseTokenizer",  
"index.number\_of\_shards" : "2",  
"index.number\_of\_replicas" : "1",  
"index.version.created" : "190499"  
}  
}  
}

And the mapping :

curl -XGET  
'[http://192.168.6.159:9202/faq-ze/\_mapping?pretty=1](http://192.168.6.159:9202/faq-ze/_mapping?pretty=1)'

{  
"faq-ze" : {  
"category" : {  
"properties" : {  
"id" : {  
"type" : "long"  
},  
"name" : {  
"type" : "string"  
}  
}  
},  
"question" : {  
"properties" : {  
"categoryTitle" : {  
"type" : "string"  
},  
"id" : {  
"type" : "long"  
},  
"questionDisplay" : {  
"type" : "string"  
},  
"questionPopularity" : {  
"type" : "long"  
},  
"questionTitle" : {  
"type" : "string"  
},  
"responseDescription" : {  
"type" : "string",  
"index\_analyzer" : "index\_analyzer",  
"search\_analyzer" : "search\_analyzer"  
},  
"responseMedia" : {  
"type" : "string"  
},  
"responseMediaGlimpse" : {  
"type" : "string"  
},  
"responsePdf" : {  
"type" : "string"  
},  
"responsePlusLabel" : {  
"type" : "string"  
},  
"responsePlusUrl" : {  
"type" : "string"  
},  
"responseTitle" : {  
"type" : "string"  
}  
}  
}  
}  
}

But an analysis try gives nothing :  
curl -XGET '[http://192.168.6.159:9202/faq-ze/\_analyze?pretty=1&text=](http://192.168.6.159:9202/faq-ze/_analyze?pretty=1&text=)"In the  
future, the HCCI diesel engine (Homogenous Charge Compression Ignition) and  
CAI gasoline engine "&analyzer=response\_index\_analyzer'  
curl: (52) Empty reply from server  
[1] 9614 exit 52 curl -XGET

And of course, searching returns nothing :  
{  
"query" : {  
"field" : {  
"responseDescription" : "ren"  
}  
}  
}

Gives :  
Pretty  
Result Transformer?  
Repeat Request  
Display Options?  
{

- took: 1
- timed\_out: false
- \_shards: {
  - total: 2
  - successful: 2
  - failed: 0  
}

- hits: {
  - total: 0
  - max\_score: null
  - hits:   
}

}

(given that my document's reponseDecription field values contains 'Renault'  
in the middle of a text.

Any idea ?

On Friday, June 1, 2012 2:56:53 PM UTC+2, Frederic Esnault wrote:

> Well i think i get the point, i just need to get up to the speed with  
> ngrams, and how to implement an analyzer.  
> I guess the best source to look for this is lucene, right ?
> 
> On Friday, June 1, 2012 12:45:39 PM UTC+2, David G Ortega wrote:
> 
> > "...are NGrams useful for this usage? And if yes, how ?..."
> > 
> > ngrams are the way I have gone to do autocompletion and probably the  
> > way almost everyone here uses.  
> > Thats why I'm asking how was performing something that IMHO is a  
> > beast.  
> > Wildcard queries over a large index is not the best idea unless you  
> > dont mind the response time or has sharded with lots of machines.
> > 
> > To autocomplete with ngram just create a custom analyzer that do what  
> > you want plus ngram, thats making the index bigger but  
> > you have the possibility of search by ngram which are essentially  
> > parts of a word.
> > 
> > Does this makes sense to U?

---

<div class="post-metadata">

### Author: ![phoenix](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/phoenix/32/1319_2.png) [@phoenix](https://discuss.elastic.co/u/phoenix)
#### Post date: [June 1, 2012, 2:58pm UTC](https://discuss.elastic.co/t/autocompletion/7921/15 "2012-06-01T14:58:21Z")

</div>

And if i remove the index/search analyzers from the field, i get responses  
with my query using 'Renault' (but not 'ren')

On Friday, June 1, 2012 4:49:35 PM UTC+2, Frederic Esnault wrote:

> I tried creating an index with a mapping, specifying a tokenizer and  
> analyzers.
> 
> XContentBuilder settings = jsonBuilder()  
> .startObject()  
> .startObject("analysis")  
> .startObject("analyzer")  
> .startObject("response\_search\_analyzer")  
> .field("tokenizer", "responseTokenizer")  
> .field("filter", "lowercase")  
> .endObject()  
> .startObject("response\_index\_analyzer")  
> .field("tokenizer", "responseTokenizer")  
> .field("filter", "lowercase", "nGram")  
> .endObject()  
> .endObject()  
> .startObject("tokenizer")  
> .startObject("responseTokenizer")  
> .field("type", "whitespace")  
> .endObject()  
> .endObject()  
> .startObject("filter")  
> .startObject("nGram")  
> .field("type", "nGram")  
> .field("min\_ngram", 3)  
> .field("max\_ngram", 6)  
> .endObject()  
> .endObject()  
> .endObject()  
> .endObject();
> 
> XContentBuilder mapping = jsonBuilder()  
> .startObject()  
> .startObject("question")  
> .startObject("properties")  
> .startObject("responseDescription")  
> .field("type", "string")  
> .field("search\_analyzer",  
> "response\_search\_analyzer")  
> .field("index\_analyzer",  
> "response\_index\_analyzer")  
> .endObject()  
> .endObject()  
> .endObject()  
> .endObject();
> 
> Then i create the index this way :  
> CreateIndexResponse response =  
> client.admin().indices().prepareCreate("faq-ze").setSettings(createSettings())  
> .addMapping("question",  
> createMapping()).execute().actionGet();
> 
> Resulting settings are these :  
> curl -XGET '[http://192.168.6.159:9202/faq-ze/\_settings?pretty=1](http://192.168.6.159:9202/faq-ze/_settings?pretty=1)'
> 
> {  
> "faq-ze" : {  
> "settings" : {  
> "index.analysis.analyzer.response\_index\_analyzer.filter.0" :  
> "lowercase",  
> "index.analysis.analyzer.response\_index\_analyzer.filter.1" : "nGram",  
> "index.analysis.tokenizer.responseTokenizer.type" : "whitespace",  
> "index.analysis.analyzer.response\_index\_analyzer.tokenizer" :  
> "responseTokenizer",  
> "index.analysis.analyzer.response\_search\_analyzer.filter" :  
> "lowercase",  
> "index.analysis.filter.nGram.min\_ngram" : "3",  
> "index.analysis.filter.nGram.type" : "nGram",  
> "index.analysis.filter.nGram.max\_ngram" : "6",  
> "index.analysis.analyzer.response\_search\_analyzer.tokenizer" :  
> "responseTokenizer",  
> "index.number\_of\_shards" : "2",  
> "index.number\_of\_replicas" : "1",  
> "index.version.created" : "190499"  
> }  
> }  
> }
> 
> And the mapping :
> 
> curl -XGET '[http://192.168.6.159:9202/faq-ze/\_mapping?pretty=1](http://192.168.6.159:9202/faq-ze/_mapping?pretty=1)'
> 
> {  
> "faq-ze" : {  
> "category" : {  
> "properties" : {  
> "id" : {  
> "type" : "long"  
> },  
> "name" : {  
> "type" : "string"  
> }  
> }  
> },  
> "question" : {  
> "properties" : {  
> "categoryTitle" : {  
> "type" : "string"  
> },  
> "id" : {  
> "type" : "long"  
> },  
> "questionDisplay" : {  
> "type" : "string"  
> },  
> "questionPopularity" : {  
> "type" : "long"  
> },  
> "questionTitle" : {  
> "type" : "string"  
> },  
> "responseDescription" : {  
> "type" : "string",  
> "index\_analyzer" : "index\_analyzer",  
> "search\_analyzer" : "search\_analyzer"  
> },  
> "responseMedia" : {  
> "type" : "string"  
> },  
> "responseMediaGlimpse" : {  
> "type" : "string"  
> },  
> "responsePdf" : {  
> "type" : "string"  
> },  
> "responsePlusLabel" : {  
> "type" : "string"  
> },  
> "responsePlusUrl" : {  
> "type" : "string"  
> },  
> "responseTitle" : {  
> "type" : "string"  
> }  
> }  
> }  
> }  
> }
> 
> But an analysis try gives nothing :  
> curl -XGET '[http://192.168.6.159:9202/faq-ze/\_analyze?pretty=1&text=](http://192.168.6.159:9202/faq-ze/_analyze?pretty=1&text=)"In  
> the future, the HCCI diesel engine (Homogenous Charge Compression Ignition)  
> and CAI gasoline engine "&analyzer=response\_index\_analyzer'  
> curl: (52) Empty reply from server  
> [1] 9614 exit 52 curl -XGET
> 
> And of course, searching returns nothing :  
> {  
> "query" : {  
> "field" : {  
> "responseDescription" : "ren"  
> }  
> }  
> }
> 
> Gives :  
> Pretty  
> Result Transformer?  
> Repeat Request  
> Display Options?  
> {
> 
> - took: 1
> - timed\_out: false
> - \_shards: {
> - total: 2
> - successful: 2
> - failed: 0  
> }
> 
> - hits: {
> - total: 0
> - max\_score: null
> - hits:   
> }
> 
> }
> 
> (given that my document's reponseDecription field values contains  
> 'Renault' in the middle of a text.
> 
> Any idea ?
> 
> On Friday, June 1, 2012 2:56:53 PM UTC+2, Frederic Esnault wrote:
> 
> > Well i think i get the point, i just need to get up to the speed with  
> > ngrams, and how to implement an analyzer.  
> > I guess the best source to look for this is lucene, right ?
> > 
> > On Friday, June 1, 2012 12:45:39 PM UTC+2, David G Ortega wrote:
> > 
> > > "...are NGrams useful for this usage? And if yes, how ?..."
> > > 
> > > ngrams are the way I have gone to do autocompletion and probably the  
> > > way almost everyone here uses.  
> > > Thats why I'm asking how was performing something that IMHO is a  
> > > beast.  
> > > Wildcard queries over a large index is not the best idea unless you  
> > > dont mind the response time or has sharded with lots of machines.
> > > 
> > > To autocomplete with ngram just create a custom analyzer that do what  
> > > you want plus ngram, thats making the index bigger but  
> > > you have the possibility of search by ngram which are essentially  
> > > parts of a word.
> > > 
> > > Does this makes sense to U?

---

<div class="post-metadata">

### Author: ![phoenix](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/phoenix/32/1319_2.png) [@phoenix](https://discuss.elastic.co/u/phoenix)
#### Post date: [June 1, 2012, 5:19pm UTC](https://discuss.elastic.co/t/autocompletion/7921/16 "2012-06-01T17:19:53Z")

</div>

Got it working !

First actually my analysis query returned something, but not displayed with  
curl 😕  
I entered the request in my browser and saw a list of tokens, but very  
wrong (ngrams with defaults boundaries (min : 1 and max : 2)  
I had a wrong ngram filter with wrong fields.  
Here is my new settings :

XContentBuilder settings = jsonBuilder()  
.startObject()  
.startObject("analysis")  
.startObject("analyzer")  
.startObject("response\_search\_analyzer")  
.field("type","custom")  
.field("tokenizer", "responseTokenizer")  
.field("filter", "lowercase")  
.endObject()  
.startObject("response\_index\_analyzer")  
.field("type","custom")  
.field("tokenizer", "standard")  
.field("filter", "lowercase", "edgeNGram")  
.endObject()  
.endObject()  
.startObject("tokenizer")  
.startObject("responseTokenizer")  
.field("type", "lowercase")  
.endObject()  
.endObject()  
.startObject("tokenizer")  
.startObject("responseNGRamTokenizer")  
.field("type", "edgeNGram")  
.field("min\_ngram", 3)  
.field("max\_ngram", 6)  
.endObject()  
.endObject()  
.startObject("filter")  
.startObject("edgeNGram")  
.field("type", "edgeNGram")  
.field("side", "front")  
.field("min\_gram", 3)  
.field("max\_gram", 6)  
.endObject()  
.endObject()  
.endObject()  
.endObject();

The analysis works perfectly and my request also.

{"query" : {  
"text" : {  
"responseDescription" : "red"  
}  
}  
}

Gives me ( i'm looking for a text containing 'reduce', so an autocompletion  
request with 'red' gives me the text i'm looking for ) :  
{

- took: 2
- timed\_out: false
- \_shards: {
  - total: 2
  - successful: 2
  - failed: 0  
}

- hits: {
  - total: 1
  - max\_score: 0.033902764
  - hits: [
    - {
      - \_index: faq-ze
      - \_type: question
      - \_id: 2
      - \_score: 0.033902764
      - \_source: {
        - id: 2
        - responseDescription: We created the low pressure EGR  
(Exhaust Gas Recirculation) system to\* reduce\* nitrogen  
oxide (NOx) emissions from combustion. Using several injectors in the  
diesel burning process also optimizes combustion. This multi-injection  
diesel process reduces pollutant emissions and engine noise.  
}  
}  
]  
}

}

Yipeee !

Frederic

On Friday, June 1, 2012 4:58:21 PM UTC+2, Frederic Esnault wrote:

> And if i remove the index/search analyzers from the field, i get responses  
> with my query using 'Renault' (but not 'ren')
> 
> On Friday, June 1, 2012 4:49:35 PM UTC+2, Frederic Esnault wrote:
> 
> > I tried creating an index with a mapping, specifying a tokenizer and  
> > analyzers.
> > 
> > XContentBuilder settings = jsonBuilder()  
> > .startObject()  
> > .startObject("analysis")  
> > .startObject("analyzer")  
> > .startObject("response\_search\_analyzer")  
> > .field("tokenizer", "responseTokenizer")  
> > .field("filter", "lowercase")  
> > .endObject()  
> > .startObject("response\_index\_analyzer")  
> > .field("tokenizer", "responseTokenizer")  
> > .field("filter", "lowercase", "nGram")  
> > .endObject()  
> > .endObject()  
> > .startObject("tokenizer")  
> > .startObject("responseTokenizer")  
> > .field("type", "whitespace")  
> > .endObject()  
> > .endObject()  
> > .startObject("filter")  
> > .startObject("nGram")  
> > .field("type", "nGram")  
> > .field("min\_ngram", 3)  
> > .field("max\_ngram", 6)  
> > .endObject()  
> > .endObject()  
> > .endObject()  
> > .endObject();
> > 
> > XContentBuilder mapping = jsonBuilder()  
> > .startObject()  
> > .startObject("question")  
> > .startObject("properties")  
> > .startObject("responseDescription")  
> > .field("type", "string")  
> > .field("search\_analyzer",  
> > "response\_search\_analyzer")  
> > .field("index\_analyzer",  
> > "response\_index\_analyzer")  
> > .endObject()  
> > .endObject()  
> > .endObject()  
> > .endObject();
> > 
> > Then i create the index this way :  
> > CreateIndexResponse response =  
> > client.admin().indices().prepareCreate("faq-ze").setSettings(createSettings())  
> > .addMapping("question",  
> > createMapping()).execute().actionGet();
> > 
> > Resulting settings are these :  
> > curl -XGET '[http://192.168.6.159:9202/faq-ze/\_settings?pretty=1](http://192.168.6.159:9202/faq-ze/_settings?pretty=1)'
> > 
> > {  
> > "faq-ze" : {  
> > "settings" : {  
> > "index.analysis.analyzer.response\_index\_analyzer.filter.0" :  
> > "lowercase",  
> > "index.analysis.analyzer.response\_index\_analyzer.filter.1" :  
> > "nGram",  
> > "index.analysis.tokenizer.responseTokenizer.type" : "whitespace",  
> > "index.analysis.analyzer.response\_index\_analyzer.tokenizer" :  
> > "responseTokenizer",  
> > "index.analysis.analyzer.response\_search\_analyzer.filter" :  
> > "lowercase",  
> > "index.analysis.filter.nGram.min\_ngram" : "3",  
> > "index.analysis.filter.nGram.type" : "nGram",  
> > "index.analysis.filter.nGram.max\_ngram" : "6",  
> > "index.analysis.analyzer.response\_search\_analyzer.tokenizer" :  
> > "responseTokenizer",  
> > "index.number\_of\_shards" : "2",  
> > "index.number\_of\_replicas" : "1",  
> > "index.version.created" : "190499"  
> > }  
> > }  
> > }
> > 
> > And the mapping :
> > 
> > curl -XGET '[http://192.168.6.159:9202/faq-ze/\_mapping?pretty=1](http://192.168.6.159:9202/faq-ze/_mapping?pretty=1)'
> > 
> > {  
> > "faq-ze" : {  
> > "category" : {  
> > "properties" : {  
> > "id" : {  
> > "type" : "long"  
> > },  
> > "name" : {  
> > "type" : "string"  
> > }  
> > }  
> > },  
> > "question" : {  
> > "properties" : {  
> > "categoryTitle" : {  
> > "type" : "string"  
> > },  
> > "id" : {  
> > "type" : "long"  
> > },  
> > "questionDisplay" : {  
> > "type" : "string"  
> > },  
> > "questionPopularity" : {  
> > "type" : "long"  
> > },  
> > "questionTitle" : {  
> > "type" : "string"  
> > },  
> > "responseDescription" : {  
> > "type" : "string",  
> > "index\_analyzer" : "index\_analyzer",  
> > "search\_analyzer" : "search\_analyzer"  
> > },  
> > "responseMedia" : {  
> > "type" : "string"  
> > },  
> > "responseMediaGlimpse" : {  
> > "type" : "string"  
> > },  
> > "responsePdf" : {  
> > "type" : "string"  
> > },  
> > "responsePlusLabel" : {  
> > "type" : "string"  
> > },  
> > "responsePlusUrl" : {  
> > "type" : "string"  
> > },  
> > "responseTitle" : {  
> > "type" : "string"  
> > }  
> > }  
> > }  
> > }  
> > }
> > 
> > But an analysis try gives nothing :  
> > curl -XGET '[http://192.168.6.159:9202/faq-ze/\_analyze?pretty=1&text=](http://192.168.6.159:9202/faq-ze/_analyze?pretty=1&text=)"In  
> > the future, the HCCI diesel engine (Homogenous Charge Compression Ignition)  
> > and CAI gasoline engine "&analyzer=response\_index\_analyzer'  
> > curl: (52) Empty reply from server  
> > [1] 9614 exit 52 curl -XGET
> > 
> > And of course, searching returns nothing :  
> > {  
> > "query" : {  
> > "field" : {  
> > "responseDescription" : "ren"  
> > }  
> > }  
> > }
> > 
> > Gives :  
> > Pretty  
> > Result Transformer?  
> > Repeat Request  
> > Display Options?  
> > {
> > 
> > - took: 1
> > - timed\_out: false
> > - \_shards: {
> > - total: 2
> > - successful: 2
> > - failed: 0  
> > }
> > 
> > - hits: {
> > - total: 0
> > - max\_score: null
> > - hits:   
> > }
> > 
> > }
> > 
> > (given that my document's reponseDecription field values contains  
> > 'Renault' in the middle of a text.
> > 
> > Any idea ?
> > 
> > On Friday, June 1, 2012 2:56:53 PM UTC+2, Frederic Esnault wrote:
> > 
> > > Well i think i get the point, i just need to get up to the speed with  
> > > ngrams, and how to implement an analyzer.  
> > > I guess the best source to look for this is lucene, right ?
> > > 
> > > On Friday, June 1, 2012 12:45:39 PM UTC+2, David G Ortega wrote:
> > > 
> > > > "...are NGrams useful for this usage? And if yes, how ?..."
> > > > 
> > > > ngrams are the way I have gone to do autocompletion and probably the  
> > > > way almost everyone here uses.  
> > > > Thats why I'm asking how was performing something that IMHO is a  
> > > > beast.  
> > > > Wildcard queries over a large index is not the best idea unless you  
> > > > dont mind the response time or has sharded with lots of machines.
> > > > 
> > > > To autocomplete with ngram just create a custom analyzer that do what  
> > > > you want plus ngram, thats making the index bigger but  
> > > > you have the possibility of search by ngram which are essentially  
> > > > parts of a word.
> > > > 
> > > > Does this makes sense to U?

---

<div class="post-metadata">

### Author: ![Roly\_Vicaria](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/roly_vicaria/32/506_2.png) [@Roly\_Vicaria](https://discuss.elastic.co/u/Roly_Vicaria)
#### Post date: [June 1, 2012, 7:27pm UTC](https://discuss.elastic.co/t/autocompletion/7921/17 "2012-06-01T19:27:41Z")

</div>

I recommend this  
thread: [http://elasticsearch-users.115913.n3.nabble.com/help-needed-with-the-query-tt3177477.html#a3178856](http://elasticsearch-users.115913.n3.nabble.com/help-needed-with-the-query-tt3177477.html#a3178856)

On Wednesday, May 30, 2012 4:10:03 PM UTC-4, Frederic Esnault wrote:

> Hi everyone,
> 
> I'm looking for a way to implement an autocomplete feature using  
> elasticsearch.  
> Does someone have tips about the way to achieve this ?  
> Kimchy, the search bar on elasticsearch sitenis exactly what i'd like to  
> do. Can you tell us how you did it?  
> What kind of mapping? Hownyou used the api to implement it ?
> 
> Thanks,  
> Frederic

---

<div class="post-metadata">

### Author: ![phoenix](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/phoenix/32/1319_2.png) [@phoenix](https://discuss.elastic.co/u/phoenix)
#### Post date: [June 1, 2012, 8:04pm UTC](https://discuss.elastic.co/t/autocompletion/7921/18 "2012-06-01T20:04:11Z")

</div>

Nice pointer, seems quite informative. I'll check this out later, but i'm  
interested in the different filters used, allowing less strict search. And  
the multifields are back in there 🙂  
Thx Roly !

Frederic

Le vendredi 1 juin 2012 21:27:41 UTC+2, Roly Vicaria a écrit :

> I recommend this thread:  
> [http://elasticsearch-users.115913.n3.nabble.com/help-needed-with-the-query-tt3177477.html#a3178856](http://elasticsearch-users.115913.n3.nabble.com/help-needed-with-the-query-tt3177477.html#a3178856)
> 
> On Wednesday, May 30, 2012 4:10:03 PM UTC-4, Frederic Esnault wrote:
> 
> > Hi everyone,
> > 
> > I'm looking for a way to implement an autocomplete feature using  
> > elasticsearch.  
> > Does someone have tips about the way to achieve this ?  
> > Kimchy, the search bar on elasticsearch sitenis exactly what i'd like to  
> > do. Can you tell us how you did it?  
> > What kind of mapping? Hownyou used the api to implement it ?
> > 
> > Thanks,  
> > Frederic

---

<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:25am UTC](https://discuss.elastic.co/t/autocompletion/7921/19 "2017-07-06T03:25:58Z")

</div>


