# Sort with limiting consecutive amount of hits by field value

**URL:** <https://discuss.elastic.co/t/sort-with-limiting-consecutive-amount-of-hits-by-field-value/310154>\
**Category:** Elasticsearch\
**Created:** [July 20, 2022, 1:01pm UTC](https://discuss.elastic.co/t/sort-with-limiting-consecutive-amount-of-hits-by-field-value/310154 "2022-07-20T13:01:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vitalyiegorov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vitalyiegorov/32/108574_2.png) [@vitalyiegorov](https://discuss.elastic.co/u/vitalyiegorov)\
**Post date:** [July 20, 2022, 1:01pm UTC](https://discuss.elastic.co/t/sort-with-limiting-consecutive-amount-of-hits-by-field-value/310154/1 "2022-07-20T13:01:39Z")

</div>

We have an ecommerce products index, which has **Brand** field(keyword) and a **PublishDate** field(date).

We need a **New items** sort, which should sort results by returning  
no more than **5 consecutive products of each brand** sorted by **PublishDate** , the index is size 10k+ and needs pagination.

BrandA - 7 products(5 with same PublishDate and 2 with the older PublishDate)  
BrandB - 6 products(5 with same PublishDate, older than BrandA first 5 and 1 older then the others))  
BrandC - 3 products(PublishDate is older then BrandA and BrandB)

We should have results `[BrandA, BrandA, BrandA, BrandA, BrandA, BrandB, BrandB, BrandB, BrandB, BrandB, BrandC, BrandC, BrandC, BrandA, BrandA, Brand B]`

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [July 22, 2022, 4:09pm UTC](https://discuss.elastic.co/t/sort-with-limiting-consecutive-amount-of-hits-by-field-value/310154/2 "2022-07-22T16:09:36Z")

</div>

What is the difference between sorting all products just by their publish date? Please provide a more specific example to illustrate the sorting.

---

<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:** [August 19, 2022, 4:09pm UTC](https://discuss.elastic.co/t/sort-with-limiting-consecutive-amount-of-hits-by-field-value/310154/3 "2022-08-19T16:09:45Z")

</div>

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