# Elasticsearch phrase suggester

**URL:** <https://discuss.elastic.co/t/elasticsearch-phrase-suggester/16902>\
**Category:** Elasticsearch\
**Created:** [April 9, 2014, 3:57pm UTC](https://discuss.elastic.co/t/elasticsearch-phrase-suggester/16902 "2014-04-09T15:57:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mstrasser](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mstrasser/32/1656_2.png) [@mstrasser](https://discuss.elastic.co/u/mstrasser)\
**Post date:** [April 9, 2014, 3:57pm UTC](https://discuss.elastic.co/t/elasticsearch-phrase-suggester/16902/1 "2014-04-09T15:57:35Z")

</div>

```
    <p>I would like to use "<a href="http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-suggesters-phrase.html" rel="nofollow">Phrase Suggester</a>". I've got a problem. When typing "johni depp",

```

it returns several results in this order:

1. john depp
2. johnny depp
3. joann depp
4. johnn depp

How can I sort the suggestions using json so that the first result is "johnny depp"? I've tried doing this with a **phonetic indexer** , but without success.

This is my configuration:

Query :

```
{
  "query": {
    "multi_match": {
      "query": "johni depp",
      "fields": [
        "fullName.word"
      ],
      "operator": "and"
    }
  },
  "suggest": {
    "text": "johni depp",
    "film": {
      "phrase": {
        "analyzer": "whitespace-fullName",
        "field": "fullName.whitespace",
        "size": 5,
        "real_word_error_likelihood": 0.95,
        "max_errors": 0.5,
        "gram_size": 2
      }
    }
  },
  "from": 0,
  "size": 1,
  "sort": [],
  "facets": []
}

```

Indexer (I use Elastica, but it's same thing):

```
$elasticaIndex->create(
              array(
                  'number_of_shards' => 4,
                  'number_of_replicas' => 1,
                  'analysis' => array(
                      'analyzer' => array(
                          'autocomplete-index-fullName' => array(
                              'tokenizer' => 'standard',
                              'filter' => 'asciifolding, lowercase, edgeNGram'
                          ),
                          'autocomplete-search-fullName' => array(
                              'tokenizer' => 'standard',
                              'filter' => 'asciifolding, lowercase'
                          ),
                          'word-fullName' => array(
                              'tokenizer' => 'keyword',
                              'filter' => 'lowercase'
                          ),
                          'whitespace-fullName' => array(
                              'tokenizer' => 'whitespace',
                              'filter' => 'lowercase'
                          ),
                      ),
                      'filter' => array(
                          'edgeNGram' => array(
                              'type' => 'edgeNGram',
                              'min_gram' => 1,
                              'max_gram' => 15
                          )
                      )
                  )
              ),
              false
);

```

Mapping :

```
$mapping->setProperties(
        array(
            'fullName' => array('type' => 'string',
                                'fields' => array(
                                    'autocomplete' => array(
                                        'type' => 'string',
                                        'index_analyzer' => 'autocomplete-index-fullName',
                                        'search_analyzer' => 'autocomplete-search-fullName'
                                    ),
                                    'word' => array(
                                        'type' => 'string',
                                        'analyzer' => 'word-fullName'
                                    ),
                                    'whitespace' => array(
                                        'type' => 'string',
                                        'analyzer' => 'whitespace-fullName'
                                    ),
                                )),
        )
);

```

Examples of referenced values:

- John Cleese
- John Gemberling
- Johnny Hallyday
- Johnny Depp
- Joann Sfar
- Joanna Rytel
- Samuel Johnson
- Johnson Traoré

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [April 10, 2014, 11:46pm UTC](https://discuss.elastic.co/t/elasticsearch-phrase-suggester/16902/2 "2014-04-10T23:46:42Z")

</div>

Hi,

You would get better results with the phrase suggester by using an analyzer  
that has a shingle filter[1]. This filter will help Elasticsearch compute  
frequencies of pairs of terms. Without it, it falls back to single-term  
frequencies, so in your case it would only consider the frequencies of  
'Johnny' and 'Depp' instead of the frequency of 'Johnny Depp' as a whole.

[1]

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

On Wed, Apr 9, 2014 at 5:57 PM, mstrasser [contact@mathiasstrasser.com](mailto:contact@mathiasstrasser.com)wrote:

> I would like to use "Phrase Suggester[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-suggesters-phrase.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-suggesters-phrase.html)".  
> I've got a problem. When typing "johni depp", it returns several results in  
> this order:
> 
> 1. john depp
> 2. johnny depp
> 3. joann depp
> 4. johnn depp
> 
> How can I sort the suggestions using json so that the first result is  
> "johnny depp"? I've tried doing this with a _phonetic indexer_, but  
> without success.
> 
> This is my configuration:
> 
> Query :
> 
> {  
> "query": {  
> "multi\_match": {  
> "query": "johni depp",  
> "fields": [  
> "fullName.word"  
> ],  
> "operator": "and"  
> }  
> },  
> "suggest": {  
> "text": "johni depp",  
> "film": {  
> "phrase": {  
> "analyzer": "whitespace-fullName",  
> "field": "fullName.whitespace",  
> "size": 5,  
> "real\_word\_error\_likelihood": 0.95,  
> "max\_errors": 0.5,  
> "gram\_size": 2  
> }  
> }  
> },  
> "from": 0,  
> "size": 1,  
> "sort": ,  
> "facets":   
> }
> 
> Indexer (I use Elastica, but it's same thing):
> 
> $elasticaIndex-\>create(  
> array(  
> 'number\_of\_shards' =\> 4,  
> 'number\_of\_replicas' =\> 1,  
> 'analysis' =\> array(  
> 'analyzer' =\> array(  
> 'autocomplete-index-fullName' =\> array(  
> 'tokenizer' =\> 'standard',  
> 'filter' =\> 'asciifolding, lowercase, edgeNGram'  
> ),  
> 'autocomplete-search-fullName' =\> array(  
> 'tokenizer' =\> 'standard',  
> 'filter' =\> 'asciifolding, lowercase'  
> ),  
> 'word-fullName' =\> array(  
> 'tokenizer' =\> 'keyword',  
> 'filter' =\> 'lowercase'  
> ),  
> 'whitespace-fullName' =\> array(  
> 'tokenizer' =\> 'whitespace',  
> 'filter' =\> 'lowercase'  
> ),  
> ),  
> 'filter' =\> array(  
> 'edgeNGram' =\> array(  
> 'type' =\> 'edgeNGram',  
> 'min\_gram' =\> 1,  
> 'max\_gram' =\> 15  
> )  
> )  
> )  
> ),  
> false  
> );
> 
> Mapping :
> 
> $mapping-\>setProperties(  
> array(  
> 'fullName' =\> array('type' =\> 'string',  
> 'fields' =\> array(  
> 'autocomplete' =\> array(  
> 'type' =\> 'string',  
> 'index\_analyzer' =\> 'autocomplete-index-fullName',  
> 'search\_analyzer' =\> 'autocomplete-search-fullName'  
> ),  
> 'word' =\> array(  
> 'type' =\> 'string',  
> 'analyzer' =\> 'word-fullName'  
> ),  
> 'whitespace' =\> array(  
> 'type' =\> 'string',  
> 'analyzer' =\> 'whitespace-fullName'  
> ),  
> )),  
> )  
> );
> 
> Examples of referenced values:
> 
> - John Cleese
> - John Gemberling
> - Johnny Hallyday
> - Johnny Depp
> - Joann Sfar
> - Joanna Rytel
> - Samuel Johnson
> - Johnson Traoré
> 
> Thanks in advance.
> 
> * * *
> 
> View this message in context: Elasticsearch phrase suggester[http://elasticsearch-users.115913.n3.nabble.com/Elasticsearch-phrase-suggester-tp4053844.html](http://elasticsearch-users.115913.n3.nabble.com/Elasticsearch-phrase-suggester-tp4053844.html)  
> Sent from the Elasticsearch Users mailing list archive[http://elasticsearch-users.115913.n3.nabble.com/](http://elasticsearch-users.115913.n3.nabble.com/)at [Nabble.com](http://Nabble.com).
> 
> --  
> 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/1397059055130-4053844.post%40n3.nabble.com](https://groups.google.com/d/msgid/elasticsearch/1397059055130-4053844.post%40n3.nabble.com)[https://groups.google.com/d/msgid/elasticsearch/1397059055130-4053844.post%40n3.nabble.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/1397059055130-4053844.post%40n3.nabble.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
Adrien Grand

--  
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/CAL6Z4j4Hew1g%2BYR5%2BEh1kC%3DwUdvQGg-FGPsvbA8C942mtpJtoA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j4Hew1g%2BYR5%2BEh1kC%3DwUdvQGg-FGPsvbA8C942mtpJtoA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [April 11, 2014, 12:23am UTC](https://discuss.elastic.co/t/elasticsearch-phrase-suggester/16902/3 "2014-04-11T00:23:11Z")

</div>

Set the min to 2 and max to either 2 or 3 and emit unigrams. That'll get it working. Also you probably want to set max errors to a number \> 1 because .5 means 50% which can get quite large if someone types 20 terms.

That's been my experience with it.

Nik

Sent from my iPhone

> On Apr 10, 2014, at 7:46 PM, Adrien Grand [adrien.grand@elasticsearch.com](mailto:adrien.grand@elasticsearch.com) wrote:
> 
> Hi,
> 
> You would get better results with the phrase suggester by using an analyzer that has a shingle filter[1]. This filter will help Elasticsearch compute frequencies of pairs of terms. Without it, it falls back to single-term frequencies, so in your case it would only consider the frequencies of 'Johnny' and 'Depp' instead of the frequency of 'Johnny Depp' as a whole.
> 
> [1] [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-shingle-tokenfilter.html)
> 
> > On Wed, Apr 9, 2014 at 5:57 PM, mstrasser [contact@mathiasstrasser.com](mailto:contact@mathiasstrasser.com) wrote:  
> > I would like to use "Phrase Suggester". I've got a problem. When typing "johni depp", it returns several results in this order:
> > 
> > john depp  
> > johnny depp  
> > joann depp  
> > johnn depp  
> > How can I sort the suggestions using json so that the first result is "johnny depp"? I've tried doing this with a phonetic indexer, but without success.
> > 
> > This is my configuration:
> > 
> > Query :
> > 
> > {  
> > "query": {  
> > "multi\_match": {  
> > "query": "johni depp",  
> > "fields": [  
> > "fullName.word"  
> > ],  
> > "operator": "and"  
> > }  
> > },  
> > "suggest": {  
> > "text": "johni depp",  
> > "film": {  
> > "phrase": {  
> > "analyzer": "whitespace-fullName",  
> > "field": "fullName.whitespace",  
> > "size": 5,  
> > "real\_word\_error\_likelihood": 0.95,  
> > "max\_errors": 0.5,  
> > "gram\_size": 2  
> > }  
> > }  
> > },  
> > "from": 0,  
> > "size": 1,  
> > "sort": ,  
> > "facets":   
> > }  
> > Indexer (I use Elastica, but it's same thing):
> > 
> > $elasticaIndex-\>create(  
> > array(  
> > 'number\_of\_shards' =\> 4,  
> > 'number\_of\_replicas' =\> 1,  
> > 'analysis' =\> array(  
> > 'analyzer' =\> array(  
> > 'autocomplete-index-fullName' =\> array(  
> > 'tokenizer' =\> 'standard',  
> > 'filter' =\> 'asciifolding, lowercase, edgeNGram'  
> > ),  
> > 'autocomplete-search-fullName' =\> array(  
> > 'tokenizer' =\> 'standard',  
> > 'filter' =\> 'asciifolding, lowercase'  
> > ),  
> > 'word-fullName' =\> array(  
> > 'tokenizer' =\> 'keyword',  
> > 'filter' =\> 'lowercase'  
> > ),  
> > 'whitespace-fullName' =\> array(  
> > 'tokenizer' =\> 'whitespace',  
> > 'filter' =\> 'lowercase'  
> > ),  
> > ),  
> > 'filter' =\> array(  
> > 'edgeNGram' =\> array(  
> > 'type' =\> 'edgeNGram',  
> > 'min\_gram' =\> 1,  
> > 'max\_gram' =\> 15  
> > )  
> > )  
> > )  
> > ),  
> > false  
> > );  
> > Mapping :
> > 
> > $mapping-\>setProperties(  
> > array(  
> > 'fullName' =\> array('type' =\> 'string',  
> > 'fields' =\> array(  
> > 'autocomplete' =\> array(  
> > 'type' =\> 'string',  
> > 'index\_analyzer' =\> 'autocomplete-index-fullName',  
> > 'search\_analyzer' =\> 'autocomplete-search-fullName'  
> > ),  
> > 'word' =\> array(  
> > 'type' =\> 'string',  
> > 'analyzer' =\> 'word-fullName'  
> > ),  
> > 'whitespace' =\> array(  
> > 'type' =\> 'string',  
> > 'analyzer' =\> 'whitespace-fullName'  
> > ),  
> > )),  
> > )  
> > );  
> > Examples of referenced values:
> > 
> > John Cleese  
> > John Gemberling  
> > Johnny Hallyday  
> > Johnny Depp  
> > Joann Sfar  
> > Joanna Rytel  
> > Samuel Johnson  
> > Johnson Traoré  
> > Thanks in advance.
> > 
> > ## View this message in context: Elasticsearch phrase suggester Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).
> > 
> > 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/1397059055130-4053844.post%40n3.nabble.com](https://groups.google.com/d/msgid/elasticsearch/1397059055130-4053844.post%40n3.nabble.com).  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> 
> ## -- Adrien Grand
> 
> 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/CAL6Z4j4Hew1g%2BYR5%2BEh1kC%3DwUdvQGg-FGPsvbA8C942mtpJtoA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j4Hew1g%2BYR5%2BEh1kC%3DwUdvQGg-FGPsvbA8C942mtpJtoA%40mail.gmail.com).  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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/3A3B737F-528D-4DDC-A674-256F5231E349%40gmail.com](https://groups.google.com/d/msgid/elasticsearch/3A3B737F-528D-4DDC-A674-256F5231E349%40gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:36am UTC](https://discuss.elastic.co/t/elasticsearch-phrase-suggester/16902/4 "2017-07-06T01:36:39Z")

</div>


