Term aggregation that return documents matching two different nested value on the same field

Hello,

I’m running on 6.8 and I’m unable to build a working term aggregation that can return a document matching two different specific value on the same nested field.

My filtering needs to occur on the aggregation itself, not via a query filter or a post filter.

So, let say I have the following index mapping:

PUT nested_test 
{
    "mappings": {
        "doc": {
            "properties": {
                "name": {
                    "type": "keyword"
                },
                "items": {
                    "type": "nested",
                    "properties": {
                        "id": {
                            "type": "keyword"
                        }
                    }
                }
            }
        }
    }
}

with the following data:

PUT nested_test/doc/1
{
  "name": "john",
  "items": [
      {
        "id": "drawer"
      },
      {
        "id": "desk"
      }
    ]
}

PUT nested_test/doc/2
{
  "name": "vlad",
  "items": [
      {
        "id": "carpet"
      },
      {
        "id": "drawer"
      }
    ]
}

I want to retrieve the name of the persons having BOTH a carpet and a drawer through a term aggregations

I tried the following:

POST nested_test/_search
{
    "size": 0,
    "aggregations": {
        "NESTED": {
            "nested": {
                "path": "items"
            },
            "aggregations": {
                "FILTER1": {
                    "filter": {
                        "term": {
                            "items.id": "carpet"
                        }
                    },
                    "aggregations": {
                        "FILTER2": {
                            "filter": {
                                "term": {
                                    "items.id": "drawer"
                                }
                            },
                            "aggregations": {
                                "REVERSE_NESTED": {
                                    "reverse_nested": {},
                                    "aggregations": {
                                        "NAME": {
                                            "terms": {
                                                "field": "name",
                                                "size": 10
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

No result are returned.
It's work well when filtering a single value or using a filter on multiple value as an "OR" condition.
Wondering what I'm doing wrong

Thanks for your help

The only way around that I figure out is to insert a reverse nested after the first filter and then add back a nested aggregation on the same field before the second filter. This appear as a bug to me and greatly pollute my aggregation.

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