# How do I refer to parent document's fields from within a Filter aggregation's Query clause

**URL:** <https://discuss.elastic.co/t/how-do-i-refer-to-parent-documents-fields-from-within-a-filter-aggregations-query-clause/160809>\
**Category:** Elasticsearch\
**Created:** [December 13, 2018, 8:17pm UTC](https://discuss.elastic.co/t/how-do-i-refer-to-parent-documents-fields-from-within-a-filter-aggregations-query-clause/160809 "2018-12-13T20:17:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Derya](https://avatars.discourse-cdn.com/v4/letter/d/c57346/32.png) [@Derya](https://discuss.elastic.co/u/Derya)\
**Post date:** [December 13, 2018, 8:17pm UTC](https://discuss.elastic.co/t/how-do-i-refer-to-parent-documents-fields-from-within-a-filter-aggregations-query-clause/160809/1 "2018-12-13T20:17:53Z")

</div>

Hello all. I opened a feature request for a reverse\_nested filter yesterday but as I thought about it more I feel like there should be a way to do this particular thing I want already. I have a filter aggregation that's inside a nested aggregation. Let's call the root level `foo` and the child level `events`. In this filter's query clause, I have an OR where one child of the OR refers to a field on `foo` and the other refers to a field on `events`. However, that second child has no way to actually reference the parent like that! I can't use a reverse\_nested aggregation because I can't put one of those as a child of a compound query, and I can't filter before nesting because I'd lose the OR semantics that way. How do I reference the field on `foo`?

If it helps illustrate, here is a concrete example. Mapping:

```
{
  "foo": {
    "properties": {
      "customer_id": { "type": "long" },
      "events": {
        "type": "nested",
        "properties": {
          "color": { "type": "keyword" },
          "coord_y": { "type": "double" }
        }
      }
    }
  }
}

```

query I want to make:

```
{
  "aggs": {
    "OP0_nest": {
      "nested": { "path": "events" },
      "aggs": {
        "OP0_custom_filter": {
          "filter": {
            "bool": {
              "should": [
                { "term": { "events.color": "orange" } },
                { "term": { "customer_id": 35 } }
              ]
            }
          },
          "aggs": {
            "OP0_op": {
              "avg": { "field": "events.coord_y" }
            }
          }
        }
      }
    }
  }
}

```

Of course, this does not work, because the child of the `should` clause containing `customer_id` does not work. That term query is always false because `customer_id` can't be accessed inside the nested aggregation.

Thanks in advance!

---

<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:** [January 10, 2019, 8:17pm UTC](https://discuss.elastic.co/t/how-do-i-refer-to-parent-documents-fields-from-within-a-filter-aggregations-query-clause/160809/2 "2019-01-10T20:17:55Z")

</div>

Closed on January 10, 2019, 8:17pm UTC

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