# Is it possible to reference query/filer data in your aggregations

**URL:** <https://discuss.elastic.co/t/is-it-possible-to-reference-query-filer-data-in-your-aggregations/19715>\
**Category:** Elasticsearch\
**Created:** [September 10, 2014, 1:34pm UTC](https://discuss.elastic.co/t/is-it-possible-to-reference-query-filer-data-in-your-aggregations/19715 "2014-09-10T13:34:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mindaugas\_Verdingova](https://avatars.discourse-cdn.com/v4/letter/m/977dab/32.png) [@Mindaugas\_Verdingova](https://discuss.elastic.co/u/Mindaugas_Verdingova)\
**Post date:** [September 10, 2014, 1:34pm UTC](https://discuss.elastic.co/t/is-it-possible-to-reference-query-filer-data-in-your-aggregations/19715/1 "2014-09-10T13:34:13Z")

</div>

In other words can you create a bucket for each document in your query and  
then add some further aggregations using values of that document.

I also asked for the same question in detail on stack overflow here is a  
link if you need more details to the  
question [http://stackoverflow.com/questions/25720027/elasticsearch-aggregations-is-it-possible-to-reference-filter-query-data-in-agg](http://stackoverflow.com/questions/25720027/elasticsearch-aggregations-is-it-possible-to-reference-filter-query-data-in-agg)

if you don't understand something please ask me I'll try my best to explain.

if it's not possible could someone just tell me that, because I've spend a  
lot of hours trying to find a way of achieving this without any luck.

or maybe you have some other ideas on how I should represent my data to  
meet my demands?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/43c193e9-0ebe-48b9-85b5-24e29f145ef0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/43c193e9-0ebe-48b9-85b5-24e29f145ef0%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Benoit\_Gagnon](https://avatars.discourse-cdn.com/v4/letter/b/dbc845/32.png) [@Benoit\_Gagnon](https://discuss.elastic.co/u/Benoit_Gagnon)\
**Post date:** [September 14, 2014, 5:37pm UTC](https://discuss.elastic.co/t/is-it-possible-to-reference-query-filer-data-in-your-aggregations/19715/2 "2014-09-14T17:37:46Z")

</div>

I suppose you can make a bucket for each value of \_id that matches your  
query; you should then have one bucket per document, on which you can  
define sub-aggregations (only metrics at this granularity) -- but really at  
that point, script\_fields would be equivalent.

It sounds like you want to get aggregations at the global scope based on  
values from documents that match a certain query.  
This is not possible -- you will have to do multiple queries.

On Wednesday, September 10, 2014 9:34:13 AM UTC-4, Mindaugas Verdingovas  
wrote:

> In other words can you create a bucket for each document in your query and  
> then add some further aggregations using values of that document.
> 
> I also asked for the same question in detail on stack overflow here is a  
> link if you need more details to the question  
> [Elasticsearch aggregations: is it possible to reference filter/query data in aggregation - Stack Overflow](http://stackoverflow.com/questions/25720027/elasticsearch-aggregations-is-it-possible-to-reference-filter-query-data-in-agg)  
> [http://www.google.com/url?q=http%3A%2F%2Fstackoverflow.com%2Fquestions%2F25720027%2Felasticsearch-aggregations-is-it-possible-to-reference-filter-query-data-in-agg&sa=D&sntz=1&usg=AFQjCNFEo1k9QKsXZnIGCjBJUx01aHkbeA](http://www.google.com/url?q=http%3A%2F%2Fstackoverflow.com%2Fquestions%2F25720027%2Felasticsearch-aggregations-is-it-possible-to-reference-filter-query-data-in-agg&sa=D&sntz=1&usg=AFQjCNFEo1k9QKsXZnIGCjBJUx01aHkbeA)
> 
> if you don't understand something please ask me I'll try my best to  
> explain.
> 
> if it's not possible could someone just tell me that, because I've spend a  
> lot of hours trying to find a way of achieving this without any luck.
> 
> or maybe you have some other ideas on how I should represent my data to  
> meet my demands?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/28ca3e77-cbc0-4f37-9d6b-16e1cf776309%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/28ca3e77-cbc0-4f37-9d6b-16e1cf776309%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 1:02am UTC](https://discuss.elastic.co/t/is-it-possible-to-reference-query-filer-data-in-your-aggregations/19715/3 "2017-07-06T01:02:27Z")

</div>


