# Count how many items appear once and only once

**URL:** <https://discuss.elastic.co/t/count-how-many-items-appear-once-and-only-once/107745>\
**Category:** Kibana\
**Created:** [November 15, 2017, 12:51pm UTC](https://discuss.elastic.co/t/count-how-many-items-appear-once-and-only-once/107745 "2017-11-15T12:51:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthew\_Field](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matthew_field/32/26599_2.png) [@Matthew\_Field](https://discuss.elastic.co/u/Matthew_Field)\
**Post date:** [November 15, 2017, 12:51pm UTC](https://discuss.elastic.co/t/count-how-many-items-appear-once-and-only-once/107745/1 "2017-11-15T12:51:32Z")

</div>

I have a set of documents with a user ID field which may appear once or many times accross the set of documents.

I know i can count unique users. But i would like to know how many users appear in my document set once and only once. (and also how many appear more than once).

If anyone has a solution would appreciate your help!  
Thanks

---

<div class="post-metadata">

**Author:** ![Bargs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bargs/32/5429_2.png) [@Bargs](https://discuss.elastic.co/u/Bargs)\
**Post date:** [November 15, 2017, 10:59pm UTC](https://discuss.elastic.co/t/count-how-many-items-appear-once-and-only-once/107745/2 "2017-11-15T22:59:26Z")

</div>

To find users that appear more than once, you could use a terms agg with the [min\_doc\_count](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#_minimum_document_count_4) set to 2 in the Advanced section of the aggregation config. Like this:

 ![12 PM](https://us1.discourse-cdn.com/elastic/original/3X/2/9/29ebfac17ae392b9a48c2efc0ab8aaff7efeb8d1.png)

Finding users that appear exactly once is harder. You could simply sort ascending with a `min_doc_count` of 1, but that operation is deprecated because it results in [document counts with unbounded errors](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-order). I can't think of a good way to find users that appear exactly once with the given schema.

---

<div class="post-metadata">

**Author:** ![Matthew\_Field](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matthew_field/32/26599_2.png) [@Matthew\_Field](https://discuss.elastic.co/u/Matthew_Field)\
**Post date:** [November 16, 2017, 10:19am UTC](https://discuss.elastic.co/t/count-how-many-items-appear-once-and-only-once/107745/3 "2017-11-16T10:19:53Z")

</div>

Thanks Bargs! Works great.

---

<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:** [December 14, 2017, 10:19am UTC](https://discuss.elastic.co/t/count-how-many-items-appear-once-and-only-once/107745/4 "2017-12-14T10:19:57Z")

</div>

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