# Reverse nested query for filter in aggregations required

**URL:** <https://discuss.elastic.co/t/reverse-nested-query-for-filter-in-aggregations-required/104263>\
**Category:** Elasticsearch\
**Created:** [October 17, 2017, 2:34pm UTC](https://discuss.elastic.co/t/reverse-nested-query-for-filter-in-aggregations-required/104263 "2017-10-17T14:34:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![david\_](https://avatars.discourse-cdn.com/v4/letter/d/c0e974/32.png) [@david\_](https://discuss.elastic.co/u/david_)\
**Post date:** [October 17, 2017, 2:34pm UTC](https://discuss.elastic.co/t/reverse-nested-query-for-filter-in-aggregations-required/104263/1 "2017-10-17T14:34:06Z")

</div>

Let's assume I have a document `A` with fields `a` and `b`, b is a list of nested documents `B` with fields `b1` and `b2`  
Now set up some documents for A:

> {a:1, b:[{b1:1, b2:1}, {b1:2, b2:2}] }  
> {a:2, b:[{b1:3, b2:1}, {b1:4, b2:2}] }

What I need: all different values for `b1` where `b2 == 1 OR a == 1`; i.e. 1,2,3

This has to be done in a nested aggregation; and we need a filter, and inside the filter a terms aggregation on `b1`.  
The filter will be a boolQuery with 2 should statements and a minimum match of 1. So far, it's still okay.  
My problem:

- If I create the filter on the outer level (as aggregation or query), both objects match, and the nested terms aggregation would return 1,2,3,4
- If I filter only for b2 in the nested aggregation, I would get 1 and 3
- I need a reverse nested query inside the bool query of the nested filter aggregation, but it seems as if this does not exist.

Any ideas? Is there a solution?  
As far as I am aware of, this would require a reverse nested query - which is currently not implemented. Are there plans to do so?

---

<div class="post-metadata">

**Author:** ![david\_](https://avatars.discourse-cdn.com/v4/letter/d/c0e974/32.png) [@david\_](https://discuss.elastic.co/u/david_)\
**Post date:** [November 3, 2017, 1:28pm UTC](https://discuss.elastic.co/t/reverse-nested-query-for-filter-in-aggregations-required/104263/2 "2017-11-03T13:28:29Z")

</div>

Is there a special reason why this is not supported?  
Are there plans to provide a solution for this? In 5.x? 6.x?

---

<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 1, 2017, 1:28pm UTC](https://discuss.elastic.co/t/reverse-nested-query-for-filter-in-aggregations-required/104263/3 "2017-12-01T13:28:43Z")

</div>

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