Paul
Paul

Reputation: 20081

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

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?

Upvotes: 4

Views: 3298

Answers (1)

malthe
malthe

Reputation: 1449

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.

Upvotes: 4

Related Questions