# Why i got different result of aggregation?

**URL:** <https://discuss.elastic.co/t/why-i-got-different-result-of-aggregation/379289>\
**Category:** Elasticsearch\
**Created:** [June 18, 2025, 11:55am UTC](https://discuss.elastic.co/t/why-i-got-different-result-of-aggregation/379289 "2025-06-18T11:55:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zoree](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zoree/32/142863_2.png) [@Zoree](https://discuss.elastic.co/u/Zoree)\
**Post date:** [June 18, 2025, 11:56am UTC](https://discuss.elastic.co/t/why-i-got-different-result-of-aggregation/379289/1 "2025-06-18T11:56:00Z")

</div>

I have queries with aggregation, the query section is the same for both, but the aggregation section is a little different and I don't understand why the different results are coming.

agg1:

```json
{
    "aggs": {
        "Metrics": {
            "filter": {
                "match_all": {}
            },
            "aggs": {
                "double": {
                    "filter": {
                        "term": {
                            "is_double": true
                        }
                    }
                },
                "pub": {
                    "filter": {
                        "match_all": {}
                    }
                },
                "theme": {
                    "cardinality": {
                        "field": "tfidf_parent_id"
                    }
                },
                "site": {
                    "cardinality": {
                        "field": "site_id"
                    }
                }
            }
        }
    }
}

```

agg2:

```json
{
    "aggs": {
        "date": {
            "date_histogram": {
                "field": "created",
                "interval": "hour",
                "format": "yyyy-MM-dd HH:mm:ss.SSSSSS",
                "extended_bounds": {
                    "min": "2025-05-22 00:00:00.000000",
                    "max": "2025-05-22 23:59:59.999000"
                }
            },
            "aggs": {
                "Metrics": {
                    "filter": {
                        "match_all": {}
                    },
                    "aggs": {
                        "double": {
                            "filter": {
                                "term": {
                                    "is_double": true
                                }
                            }
                        },
                        "pub": {
                            "filter": {
                                "match_all": {}
                            }
                        },
                        "theme": {
                            "cardinality": {
                                "field": "tfidf_parent_id"
                            }
                        },
                        "site": {
                            "cardinality": {
                                "field": "site_id"
                            }
                        }
                    }
                }
            }
        }
    }
}

```

in query section this have equal time range, but aggregation result is different. For first case is 912 for next is 948, its a sum of "site" bucket

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [June 19, 2025, 4:56pm UTC](https://discuss.elastic.co/t/why-i-got-different-result-of-aggregation/379289/2 "2025-06-19T16:56:33Z")

</div>

Hi @Zoree,

Which version of Elasticsearch are you using? For agg2 I had to change the query to use `calendar_interval` instead of `interval` to make it work.

What is the range of dates that you run the original aggregation over? Looking at the [documentation for `extended_bounds`](https://www.elastic.co/docs/reference/aggregations/search-aggregations-bucket-histogram-aggregation#search-aggregations-bucket-histogram-aggregation-extended-bounds) I can see that option forces the min and max ranges for the buckets, which could lead to different results.

Have you tried comparing the output of the [Search Profiler](https://www.elastic.co/docs/explore-analyze/query-filter/tools/search-profiler) or the [\_profile API](https://www.elastic.co/docs/reference/elasticsearch/rest-apis/search-profile) to inspect the execution?

Giving it a go myself the difference is the date histogram layer:

agg1:

 ![Screenshot 2025-06-19 at 17.55.26](https://us1.discourse-cdn.com/elastic/original/3X/b/3/b352aa28642e9ed5f895503ae2a5478b9ac93f45.png)  
 ![Screenshot 2025-06-19 at 17.55.35](https://us1.discourse-cdn.com/elastic/original/3X/0/7/0725dfe699ef50eed3f53c539c5135181e447150.png)

agg2:

 ![Screenshot 2025-06-19 at 17.55.35](https://us1.discourse-cdn.com/elastic/original/3X/0/7/0725dfe699ef50eed3f53c539c5135181e447150.png)  
 ![Screenshot 2025-06-19 at 17.56.03](https://us1.discourse-cdn.com/elastic/original/3X/2/3/2382a3eea27174827ec80855875fdc58bb6a81c7.png)

Hope that helps!
