# How to support complex filters in nested aggregation?

**URL:** <https://discuss.elastic.co/t/how-to-support-complex-filters-in-nested-aggregation/337444>\
**Category:** Elasticsearch\
**Created:** [July 3, 2023, 10:55am UTC](https://discuss.elastic.co/t/how-to-support-complex-filters-in-nested-aggregation/337444 "2023-07-03T10:55:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![crowod](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/crowod/32/122878_2.png) [@crowod](https://discuss.elastic.co/u/crowod)\
**Post date:** [July 3, 2023, 10:55am UTC](https://discuss.elastic.co/t/how-to-support-complex-filters-in-nested-aggregation/337444/1 "2023-07-03T10:55:01Z")

</div>

Here is my index mapping:

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

```

and query:

```auto
{
  "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

---

<div class="post-metadata">

**Author:** ![crowod](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/crowod/32/122878_2.png) [@crowod](https://discuss.elastic.co/u/crowod)\
**Post date:** [July 13, 2023, 8:51am UTC](https://discuss.elastic.co/t/how-to-support-complex-filters-in-nested-aggregation/337444/2 "2023-07-13T08:51:56Z")

</div>

Assuming my document is as follows:

```auto
{
  "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.

---

<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:** [August 10, 2023, 8:52am UTC](https://discuss.elastic.co/t/how-to-support-complex-filters-in-nested-aggregation/337444/3 "2023-08-10T08:52:30Z")

</div>

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