# Ordering bucket aggregations by rescore

**URL:** https://discuss.elastic.co/t/ordering-bucket-aggregations-by-rescore/117078
**Category:** Elasticsearch
**Created:** [January 25, 2018, 4:32pm UTC](https://discuss.elastic.co/t/ordering-bucket-aggregations-by-rescore/117078 "2018-01-25T16:32:57Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![bwsawyer](https://avatars.discourse-cdn.com/v4/letter/b/91b2a8/32.png) [@bwsawyer](https://discuss.elastic.co/u/bwsawyer)
#### Post date: [January 25, 2018, 4:32pm UTC](https://discuss.elastic.co/t/ordering-bucket-aggregations-by-rescore/117078/1 "2018-01-25T16:32:57Z")

</div>

Hi all,

I'm trying to retrieve the top-n buckets of documents that share the same value in a field (terms agg), where the score for each bucket is the maximum scoring document according to a rescore function (order by max agg). I also get all (or most) of the documents from each bucket as well using the top\_hits agg. This gives me everything I need from a single query.

Unfortunately the "\_score" script used inside of the max agg does not include the score after the rescore query. Is that a bug in Elasticsearch? Can somebody think of a better way for me to approach this problem?

Thanks

Example query and results follow:

Query:

```
POST test/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "general_field": "test"
          }
        },
        {
          "match": {
            "group_field": "a"
          }
        }
      ]
    }
  },
  "rescore": {
    "query": {
      "rescore_query": {
        "match": {
          "group_field": "b"
        }
      },
      "query_weight": 0.0
    }
  },
  "aggs": {
    "group_agg": {
      "terms": {
        "field": "group_field",
        "size": 10,
        "order": {
          "order_max_score": "desc"
        }
      },
      "aggs": {
        "order_max_score": {
          "max": {
            "script": "_score"
          }
        },
        "top_hits": {
          "top_hits": {
            "size": 10
          }
        }
      }
    }
  },
  "size": 0
}

```

Results - Notice in the aggregations that due to the rescore query the results should be in the opposite order:

```
{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "group_agg": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "a",
          "doc_count": 1,
          "order_max_score": {
            "value": 0.8754687309265137
          },
          "top_hits": {
            "hits": {
              "total": 1,
              "max_score": 1.4e-45,
              "hits": [
                {
                  "_index": "test",
                  "_type": "test",
                  "_id": "z1QnLmEB1I-r5xEoPg9n",
                  "_score": 0,
                  "_source": {
                    "group_field": "a",
                    "general_field": "test"
                  }
                }
              ]
            }
          }
        },
        {
          "key": "b",
          "doc_count": 1,
          "order_max_score": {
            "value": 0.18232156336307526
          },
          "top_hits": {
            "hits": {
              "total": 1,
              "max_score": 0.6931472,
              "hits": [
                {
                  "_index": "test",
                  "_type": "test",
                  "_id": "0FQnLmEB1I-r5xEoRg9r",
                  "_score": 0.6931472,
                  "_source": {
                    "group_field": "b",
                    "general_field": "test"
                  }
                }
              ]
            }
          }
        }
      ]
    }
  }
}
```

---

<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: [February 22, 2018, 4:32pm UTC](https://discuss.elastic.co/t/ordering-bucket-aggregations-by-rescore/117078/2 "2018-02-22T16:32:58Z")

</div>

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