# Aggregation only over the search results

**URL:** <https://discuss.elastic.co/t/aggregation-only-over-the-search-results/319012>\
**Category:** Elasticsearch\
**Created:** [November 16, 2022, 2:02am UTC](https://discuss.elastic.co/t/aggregation-only-over-the-search-results/319012 "2022-11-16T02:02:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![namespace-Pt](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/namespace-pt/32/105443_2.png) [@namespace-Pt](https://discuss.elastic.co/u/namespace-Pt)\
**Post date:** [November 16, 2022, 2:02am UTC](https://discuss.elastic.co/t/aggregation-only-over-the-search-results/319012/1 "2022-11-16T02:02:45Z")

</div>

I found that the terms aggregations always inspect the entire collection, for example I use

```auto
{
    "query": {
        "match": {
            "text field": "some text"
        }
    },
    "aggs": {
        "agg-terms": {
            "terms": {
                "field": "my term field"
            }
        }
    }
  },

```

The system returns `total=10000` while the document count in each bucket is about `200k` (significantly greater than the total results). Can I limit the aggregations to only perform over returned results?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood1/32/101255_2.png) [@Mark\_Harwood1](https://discuss.elastic.co/u/Mark_Harwood1)\
**Post date:** [November 16, 2022, 6:32pm UTC](https://discuss.elastic.co/t/aggregation-only-over-the-search-results/319012/2 "2022-11-16T18:32:57Z")

</div>

Aggregations do run across search results only.

> [@namespace-Pt](#):
>
> The system returns `total=10000`

10k is the max number of hits counted when [track\_total\_hits](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/search-your-data.html#track-total-hits) is set to false (the default) and the figure is not accurate but “greater than equal”.

---

<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:** [December 14, 2022, 6:33pm UTC](https://discuss.elastic.co/t/aggregation-only-over-the-search-results/319012/3 "2022-12-14T18:33:18Z")

</div>

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