# Calculate size of top hit in elasticsearch based on number of buckets

**URL:** <https://discuss.elastic.co/t/calculate-size-of-top-hit-in-elasticsearch-based-on-number-of-buckets/289577>\
**Category:** Elasticsearch\
**Created:** [November 18, 2021, 10:39am UTC](https://discuss.elastic.co/t/calculate-size-of-top-hit-in-elasticsearch-based-on-number-of-buckets/289577 "2021-11-18T10:39:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nebojsa](https://avatars.discourse-cdn.com/v4/letter/n/5f8ce5/32.png) [@Nebojsa](https://discuss.elastic.co/u/Nebojsa)\
**Post date:** [November 18, 2021, 10:39am UTC](https://discuss.elastic.co/t/calculate-size-of-top-hit-in-elasticsearch-based-on-number-of-buckets/289577/1 "2021-11-18T10:39:07Z")

</div>

I'm filtering documents based on top left and bottom right coordinates and after that using geohash forming buckets. Each bucket has fixed number of top documents in it. I want to always show for example 60 documents, equally populated in buckets. So if in my result for some geohash precision I have 2 buckets I want to have 30 top hit results in each bucket. Or if result has 10 buckets I want to have 6 top hit results in each bucket. Is there any way to have calculated size of top hits based on number of buckets?

My query:

```auto
{
  "query": {
    "bool": {
      "must": {
        "terms": {
          "category": [
            6
          ]
        }
      },
      "filter": {
        "geo_bounding_box": {
          "type": "indexed",
          "geoLocations.geoLocation": {
            "top_left": {
              "lat": 46.383606,
              "lon": 17.780508
            },
            "bottom_right": {
              "lat": 41.014818,
              "lon": 28.931074
            }
          }
        }
      }
    }
  },
  "aggregations": {
    "large-grid": {
      "geohash_grid": {
        "field": "geoLocations.geoLocation",
        "precision": 5
      },
      "aggs":{
          "document":{
            "top_hits": {
              "sort": [
                {"geoLocations.priority": {
                  "order": "desc"
                }}
                ], 
              "size": 1 <----- this is what I want to be calculated based on number of buckets in geohash aggregation
            }
          }
        }
    }
  }
}

```

---

<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 16, 2021, 10:39am UTC](https://discuss.elastic.co/t/calculate-size-of-top-hit-in-elasticsearch-based-on-number-of-buckets/289577/2 "2021-12-16T10:39:53Z")

</div>

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