# How to get only n documents for each value

**URL:** <https://discuss.elastic.co/t/how-to-get-only-n-documents-for-each-value/49795>\
**Category:** Elasticsearch\
**Created:** [May 11, 2016, 3:36pm UTC](https://discuss.elastic.co/t/how-to-get-only-n-documents-for-each-value/49795 "2016-05-11T15:36:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bakhtiyor\_Homidov](https://avatars.discourse-cdn.com/v4/letter/b/51bf81/32.png) [@Bakhtiyor\_Homidov](https://discuss.elastic.co/u/Bakhtiyor_Homidov)\
**Post date:** [May 11, 2016, 3:36pm UTC](https://discuss.elastic.co/t/how-to-get-only-n-documents-for-each-value/49795/1 "2016-05-11T15:36:53Z")

</div>

for example we've some documents

```auto
{ "id": 1, "name": "name1", "state": "NY" }
{ "id": 2, "name": "name2", "state": "NY" }
{ "id": 3, "name": "name3", "state": "NY" }
{ "id": 4, "name": "name4", "state": "NY" }
{ "id": 5, "name": "name5", "state": "NY" }
....
n-records
{ "id": n + 1, "name": "namen+1", "state": "NJ" }
{ "id": n + 1, "name": "namen+2", "state": "NJ" }
{ "id": n + 1, "name": "namen+3", "state": "NJ" }
{ "id": n + 1, "name": "namen+4", "state": "NJ" }
{ "id": n + 1, "name": "namen+5", "state": "NJ" }
....
m-records

and so on

```

and want to get only 5 documents for each states. (bonus: randomize n-records and get 5 docs)

how to query such case?

cheers!

---

<div class="post-metadata">

**Author:** ![loren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/loren/32/44942_2.png) [@loren](https://discuss.elastic.co/u/loren)\
**Post date:** [May 11, 2016, 4:27pm UTC](https://discuss.elastic.co/t/how-to-get-only-n-documents-for-each-value/49795/2 "2016-05-11T16:27:38Z")

</div>

Check out the [top hits](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html) aggregation.

```auto
PUT top_hits_example
POST top_hits_example/doc/1
{
  "name": "name1",
  "state": "NY"
}
POST top_hits_example/doc/2
{
  "name": "name2",
  "state": "NY"
}
POST top_hits_example/doc/3
{
  "name": "name3",
  "state": "NY"
}
POST top_hits_example/doc/4
{
  "name": "name4",
  "state": "NJ"
}
POST top_hits_example/doc/5
{
  "name": "name5",
  "state": "NJ"
}
POST top_hits_example/doc/6
{
  "name": "name6",
  "state": "NJ"
}
GET top_hits_example/_search?size=0
{
  "query": {
    "function_score": {
      "random_score": {}
    }
  },
  "aggs": {
    "names": {
      "terms": {
        "field": "state"
      },
      "aggs": {
        "top_tag_hits": {
          "top_hits": {
            "size": 2
          }
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Bakhtiyor\_Homidov](https://avatars.discourse-cdn.com/v4/letter/b/51bf81/32.png) [@Bakhtiyor\_Homidov](https://discuss.elastic.co/u/Bakhtiyor_Homidov)\
**Post date:** [May 11, 2016, 6:33pm UTC](https://discuss.elastic.co/t/how-to-get-only-n-documents-for-each-value/49795/3 "2016-05-11T18:33:09Z")

</div>

thanks a lot

have some question too ))

need to get only documents which distance of between each documents location higher than Xkm

e.g. first we get first document (id#1) and if distance between second document (id#2) and first document (id#1) location higher than Xkm, we include, if not check with third document (id#3)

```auto
{ "id": 1, "name": "name1", "state": "NY", location: "lat, log" }
{ "id": 2, "name": "name2", "state": "NY", location: "lat, log" }
{ "id": 3, "name": "name3", "state": "NY", location: "lat, log" }
{ "id": 4, "name": "name4", "state": "NY", location: "lat, log" }
{ "id": 5, "name": "name5", "state": "NY", location: "lat, log" }

```

how to achieve such case?

cheers!

---

<div class="post-metadata">

**Author:** ![loren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/loren/32/44942_2.png) [@loren](https://discuss.elastic.co/u/loren)\
**Post date:** [May 11, 2016, 8:24pm UTC](https://discuss.elastic.co/t/how-to-get-only-n-documents-for-each-value/49795/4 "2016-05-11T20:24:30Z")

</div>

Once you have your lat-lon from document #1, you can write a [Geo Distance Range Query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-range-query.html) to find all documents at least Xkm away from document #1's location.

---

<div class="post-metadata">

**Author:** ![Bakhtiyor\_Homidov](https://avatars.discourse-cdn.com/v4/letter/b/51bf81/32.png) [@Bakhtiyor\_Homidov](https://discuss.elastic.co/u/Bakhtiyor_Homidov)\
**Post date:** [May 12, 2016, 5:57am UTC](https://discuss.elastic.co/t/how-to-get-only-n-documents-for-each-value/49795/5 "2016-05-12T05:57:43Z")

</div>

thanks for your reply,  
but the query is not suitable, you maybe misunderstood

need select those documents which their distance greater than a number from each others, not from only first document or specific geo coord, it maybe #3 \<=\> #2, #4 \<=\> #5, #3\<=\> #5, ....

cheers!

---

<div class="post-metadata">

**Author:** ![loren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/loren/32/44942_2.png) [@loren](https://discuss.elastic.co/u/loren)\
**Post date:** [May 12, 2016, 10:42pm UTC](https://discuss.elastic.co/t/how-to-get-only-n-documents-for-each-value/49795/6 "2016-05-12T22:42:44Z")

</div>

Sorry, I'm not familiar with any single ES query that would solve a [proximity problem](https://en.wikipedia.org/wiki/Proximity_problems) like that. The brute force approach of iterating through your set of points and running that Geo Distance Range Query is probably the most straightforward, but you may be able to implement something faster using computational geometry, like in the [closest pair of points problem](https://en.wikipedia.org/wiki/Closest_pair_of_points_problem).

---

<div class="post-metadata">

**Author:** ![Bakhtiyor\_Homidov](https://avatars.discourse-cdn.com/v4/letter/b/51bf81/32.png) [@Bakhtiyor\_Homidov](https://discuss.elastic.co/u/Bakhtiyor_Homidov)\
**Post date:** [May 13, 2016, 12:40pm UTC](https://discuss.elastic.co/t/how-to-get-only-n-documents-for-each-value/49795/7 "2016-05-13T12:40:03Z")

</div>

thanks a lot

---

<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 5, 2017, 10:51pm UTC](https://discuss.elastic.co/t/how-to-get-only-n-documents-for-each-value/49795/8 "2017-07-05T22:51:48Z")

</div>


