# Filter on distinct count on aggregation result

**URL:** <https://discuss.elastic.co/t/filter-on-distinct-count-on-aggregation-result/245144>\
**Category:** Elasticsearch\
**Created:** [August 16, 2020, 1:16pm UTC](https://discuss.elastic.co/t/filter-on-distinct-count-on-aggregation-result/245144 "2020-08-16T13:16:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bharat\_Data](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bharat_data/32/51588_2.png) [@Bharat\_Data](https://discuss.elastic.co/u/Bharat_Data)\
**Post date:** [August 16, 2020, 1:16pm UTC](https://discuss.elastic.co/t/filter-on-distinct-count-on-aggregation-result/245144/1 "2020-08-16T13:16:20Z")

</div>

I have a use case in which I want to filter the results which have the more than one distinct value.

For example :-

Query - Returns all the distinct models under each different category

```auto
{
  "aggs": {
    "distinct_category": {
      "terms": {
        "field": "category",
        "size": 10
      }, "aggs": {
        "distinct_model": {
          "terms": {
            "field": "model",
            "size": 10
          }
        }
      }
    }
  }
}

```

Result -

```auto
{
  "took" : 433,
  "timed_out" : false,
  "_shards" : {
    "total" : 32,
    "successful" : 32,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 50764205,
    "max_score" : 0.0,
    "hits" : []
  },
  "aggregations" : {
    "distinct_category" : {
      "doc_count_error_upper_bound" : 915,
      "sum_other_doc_count" : 50041707,
      "buckets" : [
        {
          "key" : "Shirt",
          "doc_count" : 244,
          "distinct_model" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "A",
                "doc_count" : 122
              },
              {
                "key" : "B",
                "doc_count" : 122
              }
            ]
          }
        },
        {
          "key" : "Hoods",
          "doc_count" : 168,
          "distinct_model" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "C",
                "doc_count" : 84
              }
            ]
          }
        }
      ]
    }
  }
}

```

But I want to return only those records for which the number of distinct model is greater than 1. So I should only get result for "key" : "Shirt" and not for "key" : "Hoods".

I am new to ELK stack, can someone please help me to get the desired result.

---

<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:** [September 13, 2020, 1:16pm UTC](https://discuss.elastic.co/t/filter-on-distinct-count-on-aggregation-result/245144/2 "2020-09-13T13:16:20Z")

</div>

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