How to give shorter hit a higher score?

I am building a ngram search example with ES. Is is possible to take account the shortest length of all the hits? Here's an example:

Documents:

  • {"aliases": ["ElonMuskTesla", "ElonTesla"]}
  • {"aliases": ["ElonMusk"]}

Default Result:
When searching for "Musk" against the field "aliases", the first document will have the highest score, because it has two hits matching "Musk".

What I want:
But I want the second document to appear on the top, because in my case, it's more relavant to the serach term (shortest means most similar).


I guess this might be achieved by the script score query, but don't know exactly how after browsing a bunch of seemingly related questions on StackOverflow.

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