# Aggregation over a paginated query

**URL:** <https://discuss.elastic.co/t/aggregation-over-a-paginated-query/258792>\
**Category:** Elasticsearch\
**Created:** [December 16, 2020, 12:11am UTC](https://discuss.elastic.co/t/aggregation-over-a-paginated-query/258792 "2020-12-16T00:11:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![solenom85](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/solenom85/32/68154_2.png) [@solenom85](https://discuss.elastic.co/u/solenom85)\
**Post date:** [December 16, 2020, 12:11am UTC](https://discuss.elastic.co/t/aggregation-over-a-paginated-query/258792/1 "2020-12-16T00:11:49Z")

</div>

Hi.

I have a question regarding to running an aggregation on a paginated query.

For example, this query:

```auto
    {
        "size": 0,
        "from": 100,
        "query": {
            "match_all": {}
        },
        "aggs": {
            "agg_top_hits": {
                "top_hits": {
                    "size": 1,
                    "_source": {
                        "includes": [
                            "username"
                        ]
                    }
                }
            }
        }
    }

```

In this case the store has only 2 documents.

My question is, shouldn't the aggregation run on the set of matched documents? If my store has only 2 documents, shouldn't the aggregation be empty once the from is over 2?

---

<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 16, 2020, 9:37am UTC](https://discuss.elastic.co/t/aggregation-over-a-paginated-query/258792/2 "2020-12-16T09:37:17Z")

</div>

Aggregations are the full set of docs that matched a query - independent of hit pagination.

If you think back to where aggs/facets came from, e-commerce websites show how many shoe sizes/colours etc are available in a side-bar regardless of what page in the results you're currently on.

---

<div class="post-metadata">

**Author:** ![solenom85](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/solenom85/32/68154_2.png) [@solenom85](https://discuss.elastic.co/u/solenom85)\
**Post date:** [December 16, 2020, 3:41pm UTC](https://discuss.elastic.co/t/aggregation-over-a-paginated-query/258792/3 "2020-12-16T15:41:47Z")

</div>

Thanks @Mark_Harwood sounds fair.

---

<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 13, 2021, 3:42pm UTC](https://discuss.elastic.co/t/aggregation-over-a-paginated-query/258792/4 "2021-01-13T15:42:11Z")

</div>

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