# Elasticsearch-5: Aggregation based on query without per-doc results?

**URL:** <https://discuss.elastic.co/t/elasticsearch-5-aggregation-based-on-query-without-per-doc-results/66708>\
**Category:** Elasticsearch\
**Created:** [November 21, 2016, 11:44am UTC](https://discuss.elastic.co/t/elasticsearch-5-aggregation-based-on-query-without-per-doc-results/66708 "2016-11-21T11:44:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmkgreen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jmkgreen/32/471_2.png) [@jmkgreen](https://discuss.elastic.co/u/jmkgreen)\
**Post date:** [November 21, 2016, 11:44am UTC](https://discuss.elastic.co/t/elasticsearch-5-aggregation-based-on-query-without-per-doc-results/66708/1 "2016-11-21T11:44:45Z")

</div>

We have an application that is essentially a reporting tool. It is interested in aggregate data, not individual documents.

The queries involved are along the lines of:

`{body: { query: { bool (...) }, size: 1, aggregations: { name: { (...), size: 2000}}}}`

Two questions:

1. I've been unable to tell the server that I don't want any non-aggregate documents to come back so I have to accept one back (hence `size:1`). As I am only interested in the aggregation results, this feels like a hack - is there a better way of limiting the scope of my aggregation search and thus avoiding having matched documents returned?

2. My application expects the aggregation results to be returned in full. In our case it's unlikely to result in more than 2,000 documents so I've set this as the `size` however this too feels like a hack. Is there a better way?

Thanks,  
James

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [November 21, 2016, 1:23pm UTC](https://discuss.elastic.co/t/elasticsearch-5-aggregation-based-on-query-without-per-doc-results/66708/2 "2016-11-21T13:23:42Z")

</div>

> [@jmkgreen](#):
>
> I've been unable to tell the server that I don't want any non-aggregate documents to come back so I have to accept one back (hence size:1).

This is odd, whats wrong with ommiting the "query" part and setting the size to 0 if you don't want any search hits? I might be misunderstanding the question though.

> [@jmkgreen](#):
>
> My application expects the aggregation results to be returned in full.

What kind of aggregation result is this? As far as I know the size parameters in aggregations are specific to the kind of aggregation you are using (e.g. terms)

---

<div class="post-metadata">

**Author:** ![jmkgreen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jmkgreen/32/471_2.png) [@jmkgreen](https://discuss.elastic.co/u/jmkgreen)\
**Post date:** [November 21, 2016, 1:36pm UTC](https://discuss.elastic.co/t/elasticsearch-5-aggregation-based-on-query-without-per-doc-results/66708/3 "2016-11-21T13:36:52Z")

</div>

> [@cbuescher](#):
>
> > [@jmkgreen](#):
> >
> > I've been unable to tell the server that I don't want any non-aggregate documents to come back so I have to accept one back (hence size:1).
> 
> This is odd, whats wrong with ommiting the "query" part and setting the size to 0 if you don't want any search hits? I might be misunderstanding the question though.

The `query` limits the aggregation to the documents of interest (a `where` clause, if you will). For instance, those within a particular time range.

Using `size: 0` results in an error that the value must be a positive integer.

> [@cbuescher](#):
>
> > [@jmkgreen](#):
> >
> > My application expects the aggregation results to be returned in full.
> 
> What kind of aggregation result is this? As far as I know the size parameters in aggregations are specific to the kind of aggregation you are using (e.g. terms)

I have a `date_histogram` and aggregations including `terms`, `cardinality` and `sum`. The `terms` ones are bounded with a `size` after we discovered that without this parameter only the first ten results would be returned.

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [November 21, 2016, 1:44pm UTC](https://discuss.elastic.co/t/elasticsearch-5-aggregation-based-on-query-without-per-doc-results/66708/4 "2016-11-21T13:44:22Z")

</div>

> [@jmkgreen](#):
>
> Using size: 0 results in an error

Thats the odd part. It shouldn't do that. You are essentially scoping your aggregation as described [here](https://www.elastic.co/guide/en/elasticsearch/guide/master/_scoping_aggregations.html). Setting a size of 0 to omitt the seach results while still having a query to scope the aggregation is the normal thing to do here. Please check those examples, maybe you can spot the difference.

> [@jmkgreen](#):
>
> The terms ones are bounded with a size

Yes, you explicitely need to set the size for the terms aggregation since it will also affect the way the aggregation is computed as explained [here](https://www.elastic.co/guide/en/elasticsearch/reference/5.0/search-aggregations-bucket-terms-aggregation.html#_size).

---

<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 19, 2016, 1:44pm UTC](https://discuss.elastic.co/t/elasticsearch-5-aggregation-based-on-query-without-per-doc-results/66708/5 "2016-12-19T13:44:29Z")

</div>

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