# Does order of aggregation in ElasticSearch query affect final search result

**URL:** <https://discuss.elastic.co/t/does-order-of-aggregation-in-elasticsearch-query-affect-final-search-result/373947>\
**Category:** Elastic Search\
**Created:** [January 31, 2025, 2:14pm UTC](https://discuss.elastic.co/t/does-order-of-aggregation-in-elasticsearch-query-affect-final-search-result/373947 "2025-01-31T14:14:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![giangasl8](https://avatars.discourse-cdn.com/v4/letter/g/848f3c/32.png) [@giangasl8](https://discuss.elastic.co/u/giangasl8)\
**Post date:** [January 31, 2025, 2:14pm UTC](https://discuss.elastic.co/t/does-order-of-aggregation-in-elasticsearch-query-affect-final-search-result/373947/1 "2025-01-31T14:14:13Z")

</div>

0

I have an Elasticsearch query like below, where I try to take the aggregations, filter with **bucket\_filter** then do pagination with **bucket\_paging**. If I execute this exact query, I get the correct output as expected. But if I switch the order of **bucket\_filter** and **bucket\_paging** in query, it returns less documents than expected. As guess that, with the later case, Elasticsearch executes the **bucket\_paging** paging first (that return max 50 items), then applies the **bucket\_filter** filter, that in turn filters out a few more items from previous 50 items. Is my assumption correct? And if yes, is there any way to customize the query to get the expected result without being affected by mentioned aggregations' order in query.

My problem is, I'm using elasticsearch-java client library to build the query, which put aggregations into a map instead of a list, as a result the order of aggregations are random in the final built query.

Note: I'm on Elasticsearch v8.7

```auto
{
"aggregations": {
    "by_planning_sum_id": {
        "aggregations": {
            "bad_count": {
                "filter": {
                    "bool": {
                        "must": [{ "term": { "review_score_class": { "value": "bad" } } }]
                    }
                }
            },
            "country_data": {
                "aggregations": {
                    "avg_score": { "avg": { "field": "review_score" } }
                },
                "filter": {
                    "bool": {
                        "must": [{ "term": { "region_code": { "value": "JP" } } }]
                    }
                }
            },
            "bad_ratio": {
                "bucket_script": {
                    "buckets_path": { "all": "_count", "bad": "bad_count>_count" },
                    "script": { "source": "params.bad/params.all" }
                }
            },
            "zero_flag": {
                "bucket_script": {
                    "buckets_path": { "count": "country_data>_count" },
                    "gap_policy": "insert_zeros",
                    "script": { "source": "return ((params.count == 0) ? 0 : 1)" }
                }
            },
            "quality_negative_count": {
                "filter": {
                    "bool": {
                        "must": [
                            { "term": { "quality_label_class": { "value": "negative" } } }
                        ]
                    }
                }
            },
            "avg_score": { "avg": { "field": "review_score" } },
            "bucket_filter": {
                "bucket_selector": {
                    "buckets_path": { "count": "_count" },
                    "script": { "source": "params.count>=30" }
                }
            },
            "bucket_paging": {
                "bucket_sort": {
                    "from": 0,
                    "size": 50,
                    "sort": [
                        { "zero_flag": { "order": "desc" } },
                        { "country_data>avg_score": { "order": "desc" } },
                        { "avg_score": { "order": "desc" } },
                        { "_key": { "order": "desc" } }
                    ]
                }
            }
        },
        "terms": { "field": "root_planning_sum_id", "size": 10000 }
    }
},
"query": "..."
}

```

---

<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 28, 2025, 2:15pm UTC](https://discuss.elastic.co/t/does-order-of-aggregation-in-elasticsearch-query-affect-final-search-result/373947/2 "2025-02-28T14:15:02Z")

</div>

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