1

I have added a sort conditions field to my ElasticSearch query. I'm using ES 7.14 / Kibana 7.10, with a "fallback" to the document's score. According to the docs I have to use the reserved key _score:

My sort array field look like

[
                    
    { "update_date": { "order": "desc", "missing" : "_last", "unmapped_type" : "long" } },
    { "release_date": { "order": "desc", "missing" : "_last", "unmapped_type" : "long" } },
    "_score"
]

This works ok, but I get a null value for score when not using the _score special field. Why? Is the score not calculated when using the sort conditions at all?

loretoparisi
  • 15,724
  • 11
  • 102
  • 146

1 Answers1

4

Yes, your understanding is correct that sort will not calculate when sorting applied to another field. ES Documentation is quoted below for same:

When sorting on a field, scores are not computed. By setting track_scores to true, scores will still be computed and tracked.

So If you want to calculate score then you can enable by providing "track_scores": true parameter.

{
  "track_scores": true,
  "sort" : [
    { "post_date" : {"order" : "desc"} },
    { "name" : "desc" },
    { "age" : "desc" }
  ],
  "query" : {
    "term" : { "user" : "kimchy" }
  }
}
Sagar Patel
  • 4,993
  • 1
  • 8
  • 19
  • Thanks. So setting `track_scores=true` is alternative to using `_score` in the `sort` field or the score will be calculated differently in those two cases? – loretoparisi Aug 20 '21 at 12:45
  • 3
    _score will be calculated same in both the scenario just difference is that, it will not applied sort on _score field, so when you add _score in sort then it will calculate score as well as it will apply sorting. – Sagar Patel Aug 20 '21 at 12:51
  • 1
    @loretoparisi Just want to share that, Score is only required when you want to search data based on relevancy, if you not need a score while applying sorting on another field then it is good practice to ignore it. Because score calculation with the sorting is a costly operation and it will impact performance as well. – Sagar Patel Aug 20 '21 at 13:09