# Need Help With AutoSuggest Urgently

**URL:** <https://discuss.elastic.co/t/need-help-with-autosuggest-urgently/9898>\
**Category:** Elasticsearch\
**Created:** [November 30, 2012, 5:01am UTC](https://discuss.elastic.co/t/need-help-with-autosuggest-urgently/9898 "2012-11-30T05:01:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zkidkid](https://avatars.discourse-cdn.com/v4/letter/z/fbc32d/32.png) [@zkidkid](https://discuss.elastic.co/u/zkidkid)\
**Post date:** [November 30, 2012, 5:01am UTC](https://discuss.elastic.co/t/need-help-with-autosuggest-urgently/9898/1 "2012-11-30T05:01:15Z")

</div>

Hi All,

My _purpose_ is to make a autosuggest.  
example: use type\* Heal Th\*  
then suggest: _I heal the world, heal the world, heal them ...._

So with ES, I use edgeNGram to index and default for search:  
Let see my config first:

index:  
analysis:  
filter:  
my\_gram\_filter:  
type: edgeNGram  
side: front  
min\_gram: 1  
max\_gram: 10  
tokenizer:  
my\_gram:  
type: edgeNGram  
side: front  
min\_gram: 1  
max\_gram: 10  
analyzer:  
default:  
tokenizer: standard  
filter: [asciifolding,lowercase]  
auto:  
type: custom  
tokenizer: my\_gram  
filter: [asciifolding,lowercase]  
auto2:  
type: custom  
tokenizer: standard  
filter: [standard,lowercase,asciifolding,my\_gram\_filter]

you could see that:

auto: use edgeNgram at tokenizer  
aut2: use edgeNgram at filter.

For example, with the test:\* "Hello World"\*

_Auto_ will : h, he, hel, hell, hello, hello , hello w, hello wo, hello  
wor, hello worl, hello world  
_Auto2_ will: h, he, hel, hell, hello, w, wo, wor, worl, world

_The problem is:_

_#1: Which is better for suggestion for my above purpose ?_  
_#2: Which type of search request should I do ?_

I have tried  
with: textPhrasePrefixQuery, prefixQuery, textQuery, textPhraseQuery, wildcardQuery, fieldQuery, termQuery, spanTermQuery  
with use default analyzer. and I found that:

only textPhrasePrefixQuery match to my purpose. But it cause the problem too  
many clauses failure (default 1024) ....

I have tried with wildcard, text Query, but it is strange work.

For other query (Excep textPhrasePrefixQuery) I have checked and found that  
it have several problems as bellow:

# Don't match more then two words. ex: search _hello_ will give "\*hello

world\*" but search _hello world_ will show nothing.

# Don't match partial at world. ex: search _"hello "_ will show nothing.

# Don't match exactly . ex: search _"hello w"_ will show "_hello abc_"

_Hope anyone could help me :_

1. which analyzer, filter for index & search should I use for my above  
purpose ?
2. how does ES compare between search-analyzer and index-analyzer to match  
result ?

_Thanks in advance._

Sang Dang.

--

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [December 1, 2012, 1:08am UTC](https://discuss.elastic.co/t/need-help-with-autosuggest-urgently/9898/2 "2012-12-01T01:08:56Z")

</div>

Just a few hints:

- please try to avoid min\_gram=1, I think you never will autosuggest words  
with just a length of 1. This is very expensive.

- "auto2" is what you want. Note, autocompletion with edge n-grams is a per  
word suggestion algorithm. There is no easy solution for suggesting phrases  
based on edge n-grams (you will prefer a custom phrase dictionary in an FSA  
for this purpose)

- for n-grams analyzers, use such analyzer only for indexing, not for  
search. Then you can use a simple "match" ("text") query.  
MatchPhrasePrefixQuery is very expensive for autocompletion

- If you are interested, have a look  
at [http://jprante.github.com/applications/2012/08/17/Autocompletion-with-jQuery-JAX-RS-and-Elasticsearch.html](http://jprante.github.com/applications/2012/08/17/Autocompletion-with-jQuery-JAX-RS-and-Elasticsearch.html)  
for my autocomplete solution

Hope this helps,

Cheers,

Jörg

--

---

<div class="post-metadata">

**Author:** ![zkidkid](https://avatars.discourse-cdn.com/v4/letter/z/fbc32d/32.png) [@zkidkid](https://discuss.elastic.co/u/zkidkid)\
**Post date:** [December 3, 2012, 4:04am UTC](https://discuss.elastic.co/t/need-help-with-autosuggest-urgently/9898/3 "2012-12-03T04:04:16Z")

</div>

Hi,

Thanks for your reply.  
As I said above, I have check with other search type and it has some  
problem with is not fix my purpose:

ex:  
Data: Nothing else matter, Nothing at all  
Query: Nothing e,

If I use text query, then it will show both of them nothing else matter &  
Nothing at all while I just want to show Nothing else matter.

Here is my code:

TextQueryBuilder tq = QueryBuilders.textQuery("field\_test", query);  
SearchRequestBuilder srb = cli.prepareSearch("index\_test")  
.setQuery(tq)  
.setTypes("type\_test");

Thanks.

On Saturday, December 1, 2012 8:08:56 AM UTC+7, Jörg Prante wrote:

> Just a few hints:
> 
> - please try to avoid min\_gram=1, I think you never will autosuggest words  
> with just a length of 1. This is very expensive.
> 
> - "auto2" is what you want. Note, autocompletion with edge n-grams is a  
> per word suggestion algorithm. There is no easy solution for suggesting  
> phrases based on edge n-grams (you will prefer a custom phrase dictionary  
> in an FSA for this purpose)
> 
> - for n-grams analyzers, use such analyzer only for indexing, not for  
> search. Then you can use a simple "match" ("text") query.  
> MatchPhrasePrefixQuery is very expensive for autocompletion
> 
> - If you are interested, have a look at  
> [http://jprante.github.com/applications/2012/08/17/Autocompletion-with-jQuery-JAX-RS-and-Elasticsearch.htmlfor](http://jprante.github.com/applications/2012/08/17/Autocompletion-with-jQuery-JAX-RS-and-Elasticsearch.htmlfor) my autocomplete solution
> 
> Hope this helps,
> 
> Cheers,
> 
> Jörg

--

---

<div class="post-metadata">

**Author:** ![Doug\_Fitzmaurice](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/doug_fitzmaurice/32/2592_2.png) [@Doug\_Fitzmaurice](https://discuss.elastic.co/u/Doug_Fitzmaurice)\
**Post date:** [December 5, 2012, 9:03am UTC](https://discuss.elastic.co/t/need-help-with-autosuggest-urgently/9898/4 "2012-12-05T09:03:18Z")

</div>

I've been grappling with the same problem:

- match\_phrase\_prefix has performance / exception problems with larger  
data sets, but provides perfect results
- nGrams provide good performance on the same data, but can't match  
across word boundaries

My current (but hacky) solution is to cap the number of expansions  
performed by the mach\_phrase\_prefix to 1023:

{  
"match\_phrase\_prefix" : {  
"message" : {  
"query" : "this is a test",  
"max\_expansions" : 1023  
}  
}  
}

This will stop the exception from being triggered, but will still have performance problems with some searches. You could work around this with caching of the query results?

An alternative I have been considering is to have my search tool send an nGram query if the search phrase is \<=3 characters, then switch to match\_phrase\_prefix after that.

Hope this is of some help.

Doug

On Monday, December 3, 2012 4:04:16 AM UTC, kidkid wrote:

> Hi,
> 
> Thanks for your reply.  
> As I said above, I have check with other search type and it has some  
> problem with is not fix my purpose:
> 
> ex:  
> Data: Nothing else matter, Nothing at all  
> Query: Nothing e,
> 
> If I use text query, then it will show both of them nothing else matter &  
> Nothing at all while I just want to show Nothing else matter.
> 
> Here is my code:
> 
> TextQueryBuilder tq = QueryBuilders.textQuery("field\_test", query);  
> SearchRequestBuilder srb = cli.prepareSearch("index\_test")  
> .setQuery(tq)  
> .setTypes("type\_test");
> 
> Thanks.
> 
> On Saturday, December 1, 2012 8:08:56 AM UTC+7, Jörg Prante wrote:
> 
> > Just a few hints:
> > 
> > - please try to avoid min\_gram=1, I think you never will autosuggest  
> > words with just a length of 1. This is very expensive.
> > 
> > - "auto2" is what you want. Note, autocompletion with edge n-grams is a  
> > per word suggestion algorithm. There is no easy solution for suggesting  
> > phrases based on edge n-grams (you will prefer a custom phrase dictionary  
> > in an FSA for this purpose)
> > 
> > - for n-grams analyzers, use such analyzer only for indexing, not for  
> > search. Then you can use a simple "match" ("text") query.  
> > MatchPhrasePrefixQuery is very expensive for autocompletion
> > 
> > - If you are interested, have a look at  
> > [http://jprante.github.com/applications/2012/08/17/Autocompletion-with-jQuery-JAX-RS-and-Elasticsearch.htmlfor](http://jprante.github.com/applications/2012/08/17/Autocompletion-with-jQuery-JAX-RS-and-Elasticsearch.htmlfor) my autocomplete solution
> > 
> > Hope this helps,
> > 
> > Cheers,
> > 
> > Jörg

--

---

<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:01am UTC](https://discuss.elastic.co/t/need-help-with-autosuggest-urgently/9898/5 "2017-07-06T03:01:24Z")

</div>


