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

**URL:** <https://discuss.elastic.co/t/term-aggregation-that-return-documents-matching-two-different-nested-value-on-the-same-field/221347>\
**Category:** Elasticsearch\
**Created:** [February 28, 2020, 1:41am UTC](https://discuss.elastic.co/t/term-aggregation-that-return-documents-matching-two-different-nested-value-on-the-same-field/221347 "2020-02-28T01:41:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Charles\_Drolet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/charles_drolet/32/63494_2.png) [@Charles\_Drolet](https://discuss.elastic.co/u/Charles_Drolet)\
**Post date:** [February 28, 2020, 1:41am UTC](https://discuss.elastic.co/t/term-aggregation-that-return-documents-matching-two-different-nested-value-on-the-same-field/221347/1 "2020-02-28T01:41:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Charles\_Drolet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/charles_drolet/32/63494_2.png) [@Charles\_Drolet](https://discuss.elastic.co/u/Charles_Drolet)\
**Post date:** [February 28, 2020, 1:36pm UTC](https://discuss.elastic.co/t/term-aggregation-that-return-documents-matching-two-different-nested-value-on-the-same-field/221347/2 "2020-02-28T13:36:03Z")

</div>

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.

---

<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:** [March 27, 2020, 1:36pm UTC](https://discuss.elastic.co/t/term-aggregation-that-return-documents-matching-two-different-nested-value-on-the-same-field/221347/3 "2020-03-27T13:36:04Z")

</div>

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