# Aggregation query size?

**URL:** https://discuss.elastic.co/t/aggregation-query-size/28393
**Category:** Elasticsearch
**Created:** [August 31, 2015, 10:45pm UTC](https://discuss.elastic.co/t/aggregation-query-size/28393 "2015-08-31T22:45:12Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![linlma](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@linlma](https://discuss.elastic.co/u/linlma)
#### Post date: [August 31, 2015, 10:45pm UTC](https://discuss.elastic.co/t/aggregation-query-size/28393/1 "2015-08-31T22:45:12Z")

</div>

Hello ElasticSearch experts,

I have about 100M documents and want to run aggregation query on a random subset (500k) documents on their features/terms (for query to ElasticSearch, I can input the random 500k document as a filter), and wondering if there is a limit (or a limit we can configure) on how big the subset is for one aggregation query?

thanks in advance,  
Lin

---

<div class="post-metadata">

### Author: ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)
#### Post date: [September 1, 2015, 7:43pm UTC](https://discuss.elastic.co/t/aggregation-query-size/28393/2 "2015-09-01T19:43:19Z")

</div>

There's no technical limit to aggregation size, but you may run into practical limitations due to memory (depending on how you structure your aggregation, and if you are using fielddata vs docvalues).

As far as limiting the size, that is generally accomplished through various mechanisms to limit the "scope" the aggregation is run on. For example, you can filter the search to reduce the size, or you can limit the number of terms that a `terms` agg will calculate, or use the [Sampler aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/master/search-aggregations-bucket-sampler-aggregation.html) to sub-sample a context, etc.

If you can share a bit more details I may be able to help more specifically

---

<div class="post-metadata">

### Author: ![linlma](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@linlma](https://discuss.elastic.co/u/linlma)
#### Post date: [September 2, 2015, 4:42am UTC](https://discuss.elastic.co/t/aggregation-query-size/28393/3 "2015-09-02T04:42:16Z")

</div>

@polyfractal, if I need to use Sampler aggregation, it is applied on whole documents? Could I apply Sampler aggregation on a any subset of documents (and the subset of documents could be dynamically input from query)? Thanks.

---

<div class="post-metadata">

### Author: ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)
#### Post date: [September 2, 2015, 2:43pm UTC](https://discuss.elastic.co/t/aggregation-query-size/28393/4 "2015-09-02T14:43:23Z")

</div>

Yep! The Sampler agg is just an aggregation itself, so you could embed it inside of a [Filter](https://www.elastic.co/guide/en/elasticsearch/reference/master/search-aggregations-bucket-filter-aggregation.html) or [Filters](https://www.elastic.co/guide/en/elasticsearch/reference/master/search-aggregations-bucket-filters-aggregation.html) Aggregation for example. It would then "sample" all the docs that match the `filter` (which is itself filtering all the docs that match the `query`).

Or you could embed it inside of a `terms` aggregation, which means it would "sample" documents inside of each bucket of terms

> [@linlma](#):
>
> it is applied on whole documents?

Yes, it has access to the entire document. You specify the field you want to "sample". So you might filter/aggregate on `title` but sample on `category` for example.

---

<div class="post-metadata">

### Author: ![linlma](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@linlma](https://discuss.elastic.co/u/linlma)
#### Post date: [September 18, 2015, 1:31am UTC](https://discuss.elastic.co/t/aggregation-query-size/28393/5 "2015-09-18T01:31:24Z")

</div>

@polyfractal, thanks a lot! Sample aggregation seems powerful. 😄

Have a good day.

---

<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: [July 5, 2017, 11:49pm UTC](https://discuss.elastic.co/t/aggregation-query-size/28393/6 "2017-07-05T23:49:31Z")

</div>


