# How to return the count of unique documents by using elasticsearch aggregation

**URL:** <https://discuss.elastic.co/t/how-to-return-the-count-of-unique-documents-by-using-elasticsearch-aggregation/21323>\
**Category:** Elasticsearch\
**Created:** [December 19, 2014, 12:05am UTC](https://discuss.elastic.co/t/how-to-return-the-count-of-unique-documents-by-using-elasticsearch-aggregation/21323 "2014-12-19T00:05:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xiaoting\_Ye](https://avatars.discourse-cdn.com/v4/letter/x/eb9ed0/32.png) [@Xiaoting\_Ye](https://discuss.elastic.co/u/Xiaoting_Ye)\
**Post date:** [December 19, 2014, 12:05am UTC](https://discuss.elastic.co/t/how-to-return-the-count-of-unique-documents-by-using-elasticsearch-aggregation/21323/1 "2014-12-19T00:05:09Z")

</div>

Hi,

Is there a way to return the count of the unique documents by using  
aggregation?

My use case is pretty simple:

In my data model I have an array of locations :  
{  
...,  
"locations" : [  
{  
"city" : "new york",  
"state" : "ny"  
},  
{  
"city" : "woodbury",  
"state" : "ny"  
}  
]  
}

The "locations" field is of nested object, and it has "city" and "state"  
field.

Let's say I only have this one entry in my elasticsearch server and I ran  
terms aggregation on the state field, however the doc\_count in the "ny"  
bucket is 2 rather than 1.

Basically I want elasticsearch to return the count of distinct results (or  
return the distinct results).

Is there anyway to do it?

Thanks!

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/06744c33-e265-4cc4-8c59-8fca953ed7d7%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/06744c33-e265-4cc4-8c59-8fca953ed7d7%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 12:42am UTC](https://discuss.elastic.co/t/how-to-return-the-count-of-unique-documents-by-using-elasticsearch-aggregation/21323/2 "2017-07-06T00:42:47Z")

</div>


