# Count distinct with conditions using Count API

**URL:** <https://discuss.elastic.co/t/count-distinct-with-conditions-using-count-api/203283>\
**Category:** Elasticsearch\
**Created:** [October 11, 2019, 7:01pm UTC](https://discuss.elastic.co/t/count-distinct-with-conditions-using-count-api/203283 "2019-10-11T19:01:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![killanch](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/killanch/32/55801_2.png) [@killanch](https://discuss.elastic.co/u/killanch)\
**Post date:** [October 11, 2019, 7:01pm UTC](https://discuss.elastic.co/t/count-distinct-with-conditions-using-count-api/203283/1 "2019-10-11T19:01:39Z")

</div>

Is there a way to count distinct records/documents that match a given condition?

I am aware that \_search API with cardinality aggregation can be used for this.

But is there a way this can be achieved using \_count API?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [October 14, 2019, 12:05pm UTC](https://discuss.elastic.co/t/count-distinct-with-conditions-using-count-api/203283/2 "2019-10-14T12:05:02Z")

</div>

Hey.

as the count API only returns the count of the search hits, but non of the aggregation results, you have to use the search API for this. You can use `size: 0` to only show the aggregation results though.

--Alex

---

<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:** [November 11, 2019, 12:05pm UTC](https://discuss.elastic.co/t/count-distinct-with-conditions-using-count-api/203283/3 "2019-11-11T12:05:08Z")

</div>

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