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

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!

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