# Elasticsearch Ngram sort by the nearest results

**URL:** <https://discuss.elastic.co/t/elasticsearch-ngram-sort-by-the-nearest-results/142777>\
**Category:** Elasticsearch\
**Created:** [August 2, 2018, 2:48pm UTC](https://discuss.elastic.co/t/elasticsearch-ngram-sort-by-the-nearest-results/142777 "2018-08-02T14:48:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aymenkn](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@aymenkn](https://discuss.elastic.co/u/aymenkn)\
**Post date:** [August 2, 2018, 2:48pm UTC](https://discuss.elastic.co/t/elasticsearch-ngram-sort-by-the-nearest-results/142777/1 "2018-08-02T14:48:19Z")

</div>

I have the following settings and analyzer:

```
{
   "settings":{
      "analysis":{
         "filter":{
            "nGram_filter":{
               "type":"nGram",
               "min_gram":1,
               "max_gram":200,
               "token_chars":["letter", "digit"]
            }
         },
         "analyzer":{
            "edge_ngram_analyzer":{
               "type":"custom",
               "tokenizer":"whitespace",
               "filter":["lowercase", "asciifolding", "nGram_filter"]
            },
            "edge_ngram_search_analyzer":{
               "type":"custom",
               "tokenizer":"whitespace",
               "filter":["lowercase", "asciifolding"]
            }
         }
      }
   },
   "mappings":{
      "local_organization":{
         "properties":{
            "autocomplete":{
               "type":"text",
               "analyzer":"edge_ngram_analyzer",
               "search_analyzer":"edge_ngram_search_analyzer"
            }
         }
      }
   }
}

```

And I posted the following data into it:

```
   {
        "autocomplete": "Service Provider organisation 3 01254 25742"
    }
   {
        "autocomplete": "Service Provider test 2 132455 132455"
    }
    {
        "autocomplete": "Service Provider organisation 2 78900 6521"
    }
    {
        "autocomplete": "VOYAGES ANSELMINO 30876168300025 382510022"
    }
    {
        "autocomplete": "Service Provider test 1 32722 21211"
    }
    {
        "autocomplete": "SP recherche autocomplete 7897788 7897788"
    }

```

And here is my query:

```
{
  "query": {
    "match": {
      "autocomplete": "pro or 7"
    }
  }
}

```

The result that I get is sorted by \_score.

can someone explain to me how can i diplay the nearest result first.  
that means, when i enter 'pro or 7', i have to get the result in that order

- Service Provider organisation 2 78900 6521 ==\> ('Provider' contains 'pro', 'organisation' contains 'or', '78900' contains '7')
- Service Provider organisation 3 01254 25742 ==\> ('Provider' contains 'pro', 'organisation' contains 'or', '25742' contains '7')
- Service Provider test 1 32722 21211 ==\> ('Provider' contains 'pro', '32722' contains '7')
- Service Provider test 2 132455 132455 ==\> ('Provider' contains 'pro')
- SP recherche autocomplete 7897788 7897788 ==\> ('7897788' contains '7')
- VOYAGES ANSELMINO 30876168300025 382510022 ==\> ('30876168300025' contains '7')

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [August 2, 2018, 3:47pm UTC](https://discuss.elastic.co/t/elasticsearch-ngram-sort-by-the-nearest-results/142777/2 "2018-08-02T15:47:08Z")

</div>

You should use multi fields to generate a sub field named `autocomplete.keyword` with `keyword` type. Then sort on this field.

---

<div class="post-metadata">

**Author:** ![aymenkn](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@aymenkn](https://discuss.elastic.co/u/aymenkn)\
**Post date:** [August 16, 2018, 12:57pm UTC](https://discuss.elastic.co/t/elasticsearch-ngram-sort-by-the-nearest-results/142777/3 "2018-08-16T12:57:48Z")

</div>

i don't think that this approach can show me the nearest result first

---

<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:** [September 13, 2018, 12:57pm UTC](https://discuss.elastic.co/t/elasticsearch-ngram-sort-by-the-nearest-results/142777/4 "2018-09-13T12:57:48Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
