How to support complex filters in nested aggregation?

Here is my index mapping:

{
  "mappings": {
    "properties": {
      "non_nested_field": {
        "type": "keyword"
      },
      "nested_field": {
        "type": "nested",
        "properties": {
          "subfield": {
            "type": "text"
          },
          "subfield1": {
            "type": "keyword"
          },
          "subfield2": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

and query:

{
  "from": 0,
  "size": 0,
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must": [
              {
                "term": {
                  "non_nested_field": "value1"
                }
              },
              {
                "nested": {
                  "path": "nested_field",
                  "query": {
                    "bool": {
                      "must": [
                        {
                          "term": {
                            "nested_field.subfield1": "subvalue1"
                          }
                        }
                      ]
                    }
                  }
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must": [
              {
                "term": {
                  "non_nested_field": "value2"
                }
              },
              {
                "nested": {
                  "path": "nested_field",
                  "query": {
                    "bool": {
                      "must": [
                        {
                          "term": {
                            "nested_field.subfield1": "subvalue2"
                          }
                        }
                      ]
                    }
                  }
                }
              }
            ]
          }
        }
      ]
    }
  },
  "aggregations": {
    "nested_field": {
      "nested": {
        "path": "nested_field"
      },
      "aggregations": {
        "filter_nested": {
          "filter": {
            ...
          },
          "aggregations": {
            "by_path": {
              "terms": {
                "shard_min_doc_count": 0,
                "field": "nested_field.subfield2",
                "show_term_doc_count_error": false,
                "min_doc_count": 1,
                "order": [
                  {
                    "_count": "desc"
                  },
                  {
                    "_key": "asc"
                  }
                ]
              }
            }
          }
        }
      }
    }
  }
}

The above query combines a nested_field query with a non_nested_query within a bool query.
I would like to learn how to write a nested filter aggregation for the query mentioned above. The nested aggregation appears to only support nested field queries.
My elasticsearch version: 6.8.x

Assuming my document is as follows:

{
  "non_nested_field": "value1",
  "nested_field": [
    {
      "subfield1": "subvalue1",
      "subfield2": 1
    },
    {
      "subfield1": "subvalue2",
      "subfield2": 2
    }
  ]
}

I want to aggregate the results only for documents where the subfield: 2 and exclude documents where the subfield: 1, given that the non_nested_field: value1. But the nested aggregation appears to only support nested field queries.

I attempted to combine a nested query with a non-nested query within a bool should query and use it as a filter in the nested aggregation. However, it did not work.

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