# How do I get unique ids from a geoquery?

**URL:** <https://discuss.elastic.co/t/how-do-i-get-unique-ids-from-a-geoquery/25562>\
**Category:** Elasticsearch\
**Created:** [July 14, 2015, 11:24pm UTC](https://discuss.elastic.co/t/how-do-i-get-unique-ids-from-a-geoquery/25562 "2015-07-14T23:24:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![saipa](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@saipa](https://discuss.elastic.co/u/saipa)\
**Post date:** [July 14, 2015, 11:24pm UTC](https://discuss.elastic.co/t/how-do-i-get-unique-ids-from-a-geoquery/25562/1 "2015-07-14T23:24:47Z")

</div>

I have millions of documents, each represents a point in time of individually tracked insects and their heart rate, speed, location, etc.

Is there a way to efficiently query based off of a specific bounding box of latitude longitude, and a time range, and return all the "UNIQUE/DISTINCT" values for a specific field? (i.e. insect\_id)? If so, how?

I can see a query returning well over 20,000 documents, of which there may only be 10 unique/distinct insect\_id. Whats the most efficient way to do this?

---

<div class="post-metadata">

**Author:** ![msimos](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@msimos](https://discuss.elastic.co/u/msimos)\
**Post date:** [July 15, 2015, 3:16am UTC](https://discuss.elastic.co/t/how-do-i-get-unique-ids-from-a-geoquery/25562/2 "2015-07-15T03:16:13Z")

</div>

If you want unique values you can use a terms aggregation:

[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html)

Or you can use a cardinality aggregation:

[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-cardinality-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-cardinality-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 6, 2017, 12:01am UTC](https://discuss.elastic.co/t/how-do-i-get-unique-ids-from-a-geoquery/25562/3 "2017-07-06T00:01:26Z")

</div>


