Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch Completion Suggester - Sort suggestions

Is it possible to have the Elasticsearch completion suggester return the results in sorted order? I'm following the example in this Elasticsearch blog post and added additional hotels:

{"name": "Mercure Hotel", "city": "Munich", "name_suggest": "Mercure Hotel"}
{"name": "Mercure Motel", "city": "Munich", "name_suggest": "Mercure Motel"}
{"name": "Mercure1", "city": "Munich", "name_suggest": "Mercure1"}
{"name": "Mercure2", "city": "Munich", "name_suggest": "Mercure2"}
{"name": "Mercure3", "city": "Munich", "name_suggest": "Mercure3"}

When I post the following to the _suggest endpoint...

{
  "hotels":
  {
    "text": "Me", "completion": {"field":"name_suggest", "size":10}
  }
}

...my results are ordered like so (all with score: 1):

  • Mercure Motel
  • Mercure Hotel Munich
  • Mercure1
  • Mercure2
  • Mercure Hotel
  • Mercure3

I'd like them to be sorted lexically, so "Mercure Hotel" comes before "Mercure Motel" at the very least.

The results are ordered the same even if I post the text "Mercure2" to _suggest. My expectation is that Mercure2 would be the first suggestion, if not the only one.

When I don't find that others are having the same problem it usually means I'm doing something wrong or completely misunderstand the topic. Am I expecting too much from the completion suggester or do I need to make a configuration change to achieve what I'm trying to do?

like image 775
Paul Avatar asked Oct 18 '25 00:10

Paul


1 Answers

Add a weight to each document – the higher the number, the higher it's prioritized.

That is, sort your output strings lexically and then enumerate them. Finally, subtract their index from 2 ** 31 - 1 (the highest priority) to get suggestions in lexically ascending order.

like image 113
malthe Avatar answered Oct 19 '25 14:10

malthe