Best approach to paginating grouped results

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!

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. You can also sort on nested types.

If you create a separate document for each room, you could use field collapsing instead of using aggregations. That will make pagination easier.

1 Like

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.

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