# Referencing query field values in global aggregation

**URL:** <https://discuss.elastic.co/t/referencing-query-field-values-in-global-aggregation/89797>\
**Category:** Elasticsearch\
**Created:** [June 17, 2017, 7:37am UTC](https://discuss.elastic.co/t/referencing-query-field-values-in-global-aggregation/89797 "2017-06-17T07:37:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![srd](https://avatars.discourse-cdn.com/v4/letter/s/ba9def/32.png) [@srd](https://discuss.elastic.co/u/srd)\
**Post date:** [June 17, 2017, 7:37am UTC](https://discuss.elastic.co/t/referencing-query-field-values-in-global-aggregation/89797/1 "2017-06-17T07:37:44Z")

</div>

I have documents that contain a (multi-value) "category\_ids" field. During a search, I want to find out how many total (unfiltered) documents are in each of the results category\_ids.

I've been playing around with a global aggregation which contains a terms aggregation on the category\_ids field. However I haven't been able to figure out how to filter the category ids so that only category ids in the searches result set are returned. Is it possible to reference the search result field values in an aggregation? Is this kind of mixing of global/non-global aggregation at all possible, or do I need to do two separate queries?

---

<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 15, 2017, 7:37am UTC](https://discuss.elastic.co/t/referencing-query-field-values-in-global-aggregation/89797/2 "2017-07-15T07:37:58Z")

</div>

Closed on July 15, 2017, 7:37am UTC

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