# Filter search results by aggregation (cardinality \> X)

**URL:** https://discuss.elastic.co/t/filter-search-results-by-aggregation-cardinality-x/90070
**Category:** Elasticsearch
**Created:** [June 20, 2017, 9:40am UTC](https://discuss.elastic.co/t/filter-search-results-by-aggregation-cardinality-x/90070 "2017-06-20T09:40:33Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![andreas\_es](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@andreas\_es](https://discuss.elastic.co/u/andreas_es)
#### Post date: [June 20, 2017, 9:40am UTC](https://discuss.elastic.co/t/filter-search-results-by-aggregation-cardinality-x/90070/1 "2017-06-20T09:40:33Z")

</div>

I am trying to run an ES query that uses a "cardinality" aggregation and filter the results so that I only see those with a cardinality greater than or equal to X.

This is a very basic version of my query:

```
"size": 0,
"query": {
    "constant_score": {
        "filter": {
            "term": {
                "foo": "bar"
            }
        }
    }
},
"aggs": {
    "agg1": {
        "terms": {
            "field": "baz",
            "order": {
                "agg2": "desc"
            }
        },
        "aggs": {
            "agg2": {
                "cardinality": {
                    "field": "baz"
                }
            },
            "filter": {
                "range": {
                    "baz": { "gte": 100 }
                }
            }
        }
    }
}

```

The meaning is: search for all documents that have "bar" in field "foo". Then count the distinct values all these documents have in field "baz" (the query works up to here) AND THEN only show those with more than 100 (or X) different values in the field "baz". It's the last part (only those with cardinality "gte" 100) I am struggling with.

ES returns an error:

```
"reason" : "[range] unknown field [baz], parser not found"

```

I tried different incantation of the query, inspired by various sources in the Internet, to no avail.

Can anyone point me into the correct direction?

---

<div class="post-metadata">

### Author: ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)
#### Post date: [June 20, 2017, 2:36pm UTC](https://discuss.elastic.co/t/filter-search-results-by-aggregation-cardinality-x/90070/2 "2017-06-20T14:36:26Z")

</div>

You'll want to look at the `bucket_selector` aggregation [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-selector-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-selector-aggregation.html)

---

<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: [July 18, 2017, 2:36pm UTC](https://discuss.elastic.co/t/filter-search-results-by-aggregation-cardinality-x/90070/3 "2017-07-18T14:36:27Z")

</div>

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