# Best approach to paginating grouped results

**URL:** <https://discuss.elastic.co/t/best-approach-to-paginating-grouped-results/152380>\
**Category:** Elasticsearch\
**Created:** [October 14, 2018, 10:06am UTC](https://discuss.elastic.co/t/best-approach-to-paginating-grouped-results/152380 "2018-10-14T10:06:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vlad\_Dogarescu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vlad_dogarescu/32/56949_2.png) [@Vlad\_Dogarescu](https://discuss.elastic.co/u/Vlad_Dogarescu)\
**Post date:** [October 14, 2018, 10:06am UTC](https://discuss.elastic.co/t/best-approach-to-paginating-grouped-results/152380/1 "2018-10-14T10:06:57Z")

</div>

Hello,

I need to query some properties of "rooms" documents that each belongs to a "Hotel". And finally I want to display the "hotels" paginated.

The problem is that if I index the "rooms" then the pagination (using "size" and "from") will get multiple rooms that belong to the same hotel so instead of, let's say, 10 hotels in a page, I end up with 7 of them.

If I index the "hotels" and place the "rooms" as nested documents. Then I get the desired result, paginated "hotel" documents, but I don't know exactly which of the "rooms" in each "hotel" made the hit. And also, I cannot sort the "hotels" based on the "rooms" that made the hit.

If I index the "rooms" and aggregate buckets by term "hotel.id", then I get all that I need but I cannot paginate the results, since buckets do not support paginations. And I need up with huge results that I cannot work with.

Can anyone point me in the right direction?

Thank you!

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [October 14, 2018, 11:35am UTC](https://discuss.elastic.co/t/best-approach-to-paginating-grouped-results/152380/2 "2018-10-14T11:35:45Z")

</div>

If you go with nested types, then you can find out which of the "rooms" is the cause of a hit if you use [`inner_hits`](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html). You can also [sort on nested types](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html#nested-sorting).

If you create a separate document for each room, you could use [field collapsing](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-collapse.html) instead of using aggregations. That will make pagination easier.

---

<div class="post-metadata">

**Author:** ![Vlad\_Dogarescu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vlad_dogarescu/32/56949_2.png) [@Vlad\_Dogarescu](https://discuss.elastic.co/u/Vlad_Dogarescu)\
**Post date:** [October 14, 2018, 12:35pm UTC](https://discuss.elastic.co/t/best-approach-to-paginating-grouped-results/152380/3 "2018-10-14T12:35:48Z")

</div>

Thank you so much!! I think I'm going to go with "field collapsing", I just tested it and it is exactly what I needed.

---

<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:** [November 11, 2018, 12:36pm UTC](https://discuss.elastic.co/t/best-approach-to-paginating-grouped-results/152380/4 "2018-11-11T12:36:05Z")

</div>

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