# Combining queries while preserving order

**URL:** <https://discuss.elastic.co/t/combining-queries-while-preserving-order/319760>\
**Category:** Elasticsearch\
**Created:** [November 24, 2022, 1:59pm UTC](https://discuss.elastic.co/t/combining-queries-while-preserving-order/319760 "2022-11-24T13:59:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Strasser](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/peter_strasser/32/99068_2.png) [@Peter\_Strasser](https://discuss.elastic.co/u/Peter_Strasser)\
**Post date:** [November 24, 2022, 1:59pm UTC](https://discuss.elastic.co/t/combining-queries-while-preserving-order/319760/1 "2022-11-24T13:59:09Z")

</div>

Hi Elastic Community,

I have the use case, that I want to search blocks of data in order.

If we take products as an example a requirement could look like this:

Group1: First 5 products are products with the id 120, 121, 122, 123, 124  
Group2: Then 10 products in the category "shoes".  
Group3: Then all products in the band "nike".

I can easily solve this by running 3 queries one after the other and merging the results. The problem with this solution is, that I'd like to aggregate data over this results set. For example I could be interested in the distribution of prices of those products.

So preferably I'd like to make this work with one single query, that I can run my aggregations on.

I thought about using scoring tiers with additive boosts like so:  
Group 1: +10\_000  
Group 2: +1\_000  
Group 3: +100  
etc.

The problem with that is, that I can not find a way to limit the groups by n amount of products, within my boolean query. The size parameter only works on the entire result set.

Is there a way to limit single sub queries or does anyone have an idea how this can be solved otherwise?

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:** [December 22, 2022, 1:59pm UTC](https://discuss.elastic.co/t/combining-queries-while-preserving-order/319760/2 "2022-12-22T13:59:47Z")

</div>

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