# How to give shorter hit a higher score?

**URL:** <https://discuss.elastic.co/t/how-to-give-shorter-hit-a-higher-score/322245>\
**Category:** Elasticsearch\
**Created:** [December 31, 2022, 2:07pm UTC](https://discuss.elastic.co/t/how-to-give-shorter-hit-a-higher-score/322245 "2022-12-31T14:07:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![JohnDope](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/johndope/32/115425_2.png) [@JohnDope](https://discuss.elastic.co/u/JohnDope)\
**Post date:** [December 31, 2022, 2:07pm UTC](https://discuss.elastic.co/t/how-to-give-shorter-hit-a-higher-score/322245/1 "2022-12-31T14:07:10Z")

</div>

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](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-script-score-query.html), but don't know exactly how after browsing a bunch of seemingly related questions on StackOverflow.

---

<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:** [January 28, 2023, 2:07pm UTC](https://discuss.elastic.co/t/how-to-give-shorter-hit-a-higher-score/322245/2 "2023-01-28T14:07:12Z")

</div>

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