# Using a geo\_bound aggregation together with pagination

**URL:** <https://discuss.elastic.co/t/using-a-geo-bound-aggregation-together-with-pagination/39521>\
**Category:** Elasticsearch\
**Created:** [January 19, 2016, 9:50am UTC](https://discuss.elastic.co/t/using-a-geo-bound-aggregation-together-with-pagination/39521 "2016-01-19T09:50:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![iammichiel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/iammichiel/32/6557_2.png) [@iammichiel](https://discuss.elastic.co/u/iammichiel)\
**Post date:** [January 19, 2016, 9:50am UTC](https://discuss.elastic.co/t/using-a-geo-bound-aggregation-together-with-pagination/39521/1 "2016-01-19T09:50:46Z")

</div>

Hi all,

When using a geo\_bound aggregation (ie. the smallest window of coordinates to display all results) and pagination (x documents per page) together, I am wondering if the aggregation is run on the subset of results selected by the pagination or the complete data set ?

For example, if I search all documents within a given radius, and select top 10 documents. Will the aggregation use all documents as calculation reference, or use the top 10 only ?

I was not able to find that information from the docs...  
Thanks !

Michiel

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [January 19, 2016, 9:54am UTC](https://discuss.elastic.co/t/using-a-geo-bound-aggregation-together-with-pagination/39521/2 "2016-01-19T09:54:16Z")

</div>

Aggregations run over all documents returned but the query not just the hits (the page of results). So for example if you run a query which matches 5000 documents but ask for `from:0, size:10` you will get the 10 hits (results) back but the aggregations will be run over all 5000 matching documents.

Hope that helps

---

<div class="post-metadata">

**Author:** ![iammichiel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/iammichiel/32/6557_2.png) [@iammichiel](https://discuss.elastic.co/u/iammichiel)\
**Post date:** [January 19, 2016, 9:59am UTC](https://discuss.elastic.co/t/using-a-geo-bound-aggregation-together-with-pagination/39521/3 "2016-01-19T09:59:05Z")

</div>

Great news! Thanks for the answer ! 😉

---

<div class="post-metadata">

**Author:** ![iammichiel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/iammichiel/32/6557_2.png) [@iammichiel](https://discuss.elastic.co/u/iammichiel)\
**Post date:** [January 19, 2016, 12:43pm UTC](https://discuss.elastic.co/t/using-a-geo-bound-aggregation-together-with-pagination/39521/4 "2016-01-19T12:43:14Z")

</div>

Now, I am facing another problem, can I add a central point to the query ? To be considered as extra point?

For example, when I search for a given city and all my points are situated in the souther half, I would like to show a map containing both center of the city and all available results. Using the geo\_bound aggregations, it does not include the original point I was looking for.

Is this possible?

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [January 19, 2016, 1:34pm UTC](https://discuss.elastic.co/t/using-a-geo-bound-aggregation-together-with-pagination/39521/5 "2016-01-19T13:34:22Z")

</div>

Unfortunately no you can't have the aggregation include the original query point as the aggregations don't know anything about the actual query, they are only given the documents matching the query. However, in your app you could take the output of your geo\_bounds query and extend the bounding box if the point is not inside it. Something like this (in pseudocode):

```auto
if (geo_bounds.top < query.lat()) {
    geobounds.top = query.lat()
}

if (geo_bounds.bottom > query.lat()) {
    geobounds.bottom = query.lat()
}

if (geo_bounds.right < query.lon()) {
    geobounds.right = query.lon()
}

if (geo_bounds.left > query.lon()) {
    geobounds.left = query.lon()
}

```

---

<div class="post-metadata">

**Author:** ![iammichiel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/iammichiel/32/6557_2.png) [@iammichiel](https://discuss.elastic.co/u/iammichiel)\
**Post date:** [February 23, 2016, 10:03am UTC](https://discuss.elastic.co/t/using-a-geo-bound-aggregation-together-with-pagination/39521/6 "2016-02-23T10:03:03Z")

</div>

Thanks, I will definitely try this strategy ! I'll let you know if this worked !

---

<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, 11:14pm UTC](https://discuss.elastic.co/t/using-a-geo-bound-aggregation-together-with-pagination/39521/7 "2017-07-05T23:14:15Z")

</div>


