# Having same order in aggregation

**URL:** https://discuss.elastic.co/t/having-same-order-in-aggregation/160978
**Category:** Elasticsearch
**Created:** [December 14, 2018, 10:53pm UTC](https://discuss.elastic.co/t/having-same-order-in-aggregation/160978 "2018-12-14T22:53:43Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![3ms1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/3ms1/32/54472_2.png) [@3ms1](https://discuss.elastic.co/u/3ms1)
#### Post date: [December 14, 2018, 10:53pm UTC](https://discuss.elastic.co/t/having-same-order-in-aggregation/160978/1 "2018-12-14T22:53:43Z")

</div>

Hi,

I have a function score query sorted by score in desc.

In aggregation, however, the order is not the same even though it is also sorted by max score in desc.

How will it be possible to do this?

Cheers,

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [December 17, 2018, 10:24am UTC](https://discuss.elastic.co/t/having-same-order-in-aggregation/160978/2 "2018-12-17T10:24:51Z")

</div>

> [@3ms1](#):
>
> In aggregation, however, the order is not the same

Not sure what you mean. An aggregation is typically a summary of all docs - both low and high scoring.  
Can you share the JSON of your problem request?

---

<div class="post-metadata">

### Author: ![3ms1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/3ms1/32/54472_2.png) [@3ms1](https://discuss.elastic.co/u/3ms1)
#### Post date: [December 19, 2018, 4:35pm UTC](https://discuss.elastic.co/t/having-same-order-in-aggregation/160978/3 "2018-12-19T16:35:49Z")

</div>

Hi Mark,

Thanks for your reply. Imagine that you have parents sorted by \_score in desc.

I need parents and their children's ids so I do not need whole children.

In aggregation, I can actually get them but size must be 1000! Because even though it is also sorted by \_score, parents are ordered different in aggregation.

What could it be the best practice?

```
  "aggs": {
    "parents": {
      "terms": {
        "field": "id",
        "size": 1000,
        "order": {
          "max_score": "desc"
        }
      },
      "aggs": {
        "items": {
          "children": {
            "type": "items"
          },
          "aggs": {
            "result": {
              "filter": {
                "term": {
                  "active": true
                }
              },
              "aggs": {
                "platform": {
                  "top_hits": {
                    "_source": "id",
                    "size": 100
                  }
                }
              }
            }
          }
        },
        "max_score": {
          "max": {
            "script": "_score"
          }
        },
        "bucket_truncate": {
          "bucket_sort": {
            "from": 0,
            "size": 1000,
            "sort": {
              "max_score": {
                "order": "desc"
              }
            }
          }
        }
      }
    }
  }
```

---

<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 16, 2019, 4:36pm UTC](https://discuss.elastic.co/t/having-same-order-in-aggregation/160978/4 "2019-01-16T16:36:01Z")

</div>

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