# Include/Exclude for cardinality aggregation

**URL:** <https://discuss.elastic.co/t/include-exclude-for-cardinality-aggregation/267759>\
**Category:** Elasticsearch\
**Created:** [March 19, 2021, 7:50am UTC](https://discuss.elastic.co/t/include-exclude-for-cardinality-aggregation/267759 "2021-03-19T07:50:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![robmartin11](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/robmartin11/32/99177_2.png) [@robmartin11](https://discuss.elastic.co/u/robmartin11)\
**Post date:** [March 19, 2021, 7:50am UTC](https://discuss.elastic.co/t/include-exclude-for-cardinality-aggregation/267759/1 "2021-03-19T07:50:01Z")

</div>

I have a terms aggregation with an IncludeExclude filter:

```auto
{
  "size": 0,
  "query": {
    ...
  },
 "aggregations": {
    "facets": {
      "terms": {
        "field": "type_id",
        "size": 20,
        "order": [
          {
            "_count": "desc"
          },
          {
            "_key": "asc"
          }
        ],
        "include": "PARENT$.*",
        "exclude": ".+__\\*"
      }
    }
}

```

This will return me the top 20 facets for field `type_id`. I also want to know the total number of unique terms for this facet, so I want to add another aggregate:

```auto
 "total": {
      "cardinality": {
        "field": "type_id"
      }
    }

```

but I can't filter this total by include/exclude, so the count doesn't match with the terms aggregate above. Is there an efficient way to achieve this?

---

<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:** [April 16, 2021, 7:50am UTC](https://discuss.elastic.co/t/include-exclude-for-cardinality-aggregation/267759/2 "2021-04-16T07:50:05Z")

</div>

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