# Is there any way to use one query aggregation result as the filter for other query

**URL:** <https://discuss.elastic.co/t/is-there-any-way-to-use-one-query-aggregation-result-as-the-filter-for-other-query/187095>\
**Category:** Elasticsearch\
**Created:** [June 24, 2019, 9:40am UTC](https://discuss.elastic.co/t/is-there-any-way-to-use-one-query-aggregation-result-as-the-filter-for-other-query/187095 "2019-06-24T09:40:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![BleedCode](https://avatars.discourse-cdn.com/v4/letter/b/a87d85/32.png) [@BleedCode](https://discuss.elastic.co/u/BleedCode)\
**Post date:** [June 24, 2019, 9:40am UTC](https://discuss.elastic.co/t/is-there-any-way-to-use-one-query-aggregation-result-as-the-filter-for-other-query/187095/1 "2019-06-24T09:40:38Z")

</div>

Is there any approach by using which i can pass the aggregation result as the input for other query. By aggregation result i mean the buckets key value. I want to use the "key" field value as the filter for other query. So, i can restrict the data to the key results. This i am trying to implement in java.

I am using two separate queries, if there is any better approach please suggest. Below is the response of my first query:

> "data\_aggregation": {  
> "doc\_count\_error\_upper\_bound": 0,  
> "sum\_other\_doc\_count": 0,  
> "buckets": [  
> {  
> "key": "1",  
> "doc\_count": 10  
> },  
> {  
> "key": "9",  
> "doc\_count": 1  
> }  
> ]  
> }  
> }

For this response i want to use key's value.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [June 24, 2019, 9:47am UTC](https://discuss.elastic.co/t/is-there-any-way-to-use-one-query-aggregation-result-as-the-filter-for-other-query/187095/2 "2019-06-24T09:47:04Z")

</div>

The Graph API effectively does this to walk the connections in data: [https://www.elastic.co/guide/en/elasticsearch/reference/current/graph-explore-api.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/graph-explore-api.html)

This is available as part of a Platinum subscription and has a graph visualization GUI to show connections. You can check it out for a trial period if you don't have a subscription

---

<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 22, 2019, 9:47am UTC](https://discuss.elastic.co/t/is-there-any-way-to-use-one-query-aggregation-result-as-the-filter-for-other-query/187095/3 "2019-07-22T09:47:12Z")

</div>

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