# Access to max\_score result property in elasticsearch result in script sorting

**URL:** <https://discuss.elastic.co/t/access-to-max-score-result-property-in-elasticsearch-result-in-script-sorting/232806>\
**Category:** Elasticsearch\
**Created:** [May 15, 2020, 11:35am UTC](https://discuss.elastic.co/t/access-to-max-score-result-property-in-elasticsearch-result-in-script-sorting/232806 "2020-05-15T11:35:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![111338](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/111338/32/68432_2.png) [@111338](https://discuss.elastic.co/u/111338)\
**Post date:** [May 15, 2020, 11:35am UTC](https://discuss.elastic.co/t/access-to-max-score-result-property-in-elasticsearch-result-in-script-sorting/232806/1 "2020-05-15T11:35:05Z")

</div>

I have es query for search, like this

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

```

and I got result like this:

```auto
{
  "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:

```auto
# 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):

```auto
...
  "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?

---

<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:** [June 12, 2020, 11:35am UTC](https://discuss.elastic.co/t/access-to-max-score-result-property-in-elasticsearch-result-in-script-sorting/232806/2 "2020-06-12T11:35:19Z")

</div>

Closed on June 12, 2020, 11:35am UTC

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