Access to max_score result property in elasticsearch result in script sorting

I have es query for search, like this

{
  "track_scores": true,
  "query": {
    "bool": {
      "must": [
         <fulltext_search_predicate_1>,
         <fulltext_search_predicate_2>,
      ]
  }

and I got result like this:

{
  "took" : 95,
  "timed_out" : false,
  "_shards" : {
    "total" : 6,
    "successful" : 6,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 151,
    "max_score" : 249,
    "hits" : [
      {
        "_index" : "indexname",
        "_type" : "doctype",
        "_id" : "100500",
        "_score" : 249,
        "_source" : {
          "name" : "first",
          "cost": 100,
        },
      },
      {
        "_index" : "indexname",
        "_type" : "doctype",
        "_id" : "3140",
        "_score" : 240,
        "_source" : {
          "name" : "second",
          "cost": 105,
        },
      },
      {
        "_index" : "indexname",
        "_type" : "doctype",
        "_id" : "7654",
        "_score" : 170,
        "_source" : {
          "name" : "third",
          "cost": 205,
        },
      },

In my project, small difference between scores of documents doesn`t make much impact. I want to group my result for 5 group (by score) and sort docs in each groups by cost(desc), like this:

# first group, 250 > score > 200
1. second (cost=105)
2. first (cost=100)

# second group, 199 > score > 150
3. third (cost=205, but low grouped_score_value)

# third group, 149 > score > 100
... etc 

I can do that with script like this (groups qty is constant, always 5):

...
  "script": "(int)( (max_score - _score) / (int)(max_score / 5) )"
...

But how I can access to max_score of query result in each document sort-script?

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