# Filter documents using aggregation in Discover

**URL:** <https://discuss.elastic.co/t/filter-documents-using-aggregation-in-discover/325401>\
**Category:** Kibana\
**Created:** [February 13, 2023, 2:54pm UTC](https://discuss.elastic.co/t/filter-documents-using-aggregation-in-discover/325401 "2023-02-13T14:54:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Suresh\_Subramaniyan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/suresh_subramaniyan/32/98464_2.png) [@Suresh\_Subramaniyan](https://discuss.elastic.co/u/Suresh_Subramaniyan)\
**Post date:** [February 13, 2023, 2:54pm UTC](https://discuss.elastic.co/t/filter-documents-using-aggregation-in-discover/325401/1 "2023-02-13T14:54:59Z")

</div>

Need to filter the documents based on aggregated results in kibana discover .

```auto
{
  "aggs": {
    "match_id": {
      "terms": {
        "field": "MATCH_ID",
        "size": 10000
      },
      "aggs": {
        "count_id": {
          "terms": {
            "field": "MATCH_ID"
          }
        },
        "filtering_agg": {
          "bucket_selector": {
            "buckets_path": { 
              "total_ids": "count_id"
            },
            "script": {
              "source": "params.total_ids == 2"
            }
          }
        }
      }
    }
  }
}

```

is there any way to apply aggregation and filter documents in Discover .

---

<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 13, 2023, 2:55pm UTC](https://discuss.elastic.co/t/filter-documents-using-aggregation-in-discover/325401/2 "2023-03-13T14:55:28Z")

</div>

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