Elasticsearch Ngram sort by the nearest results

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')

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

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

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