I use Elasticsearch for product listings. Each product has a rank that determines sorting in the list. Each product belongs to certain category. I would like to sort products in such a way, that products from the same category are not adjacent to each other in the result.
For example if I had 6 products sorted only by rank ("A1" means first product in category A):
A1(rank=1), A2(rank=2), B1(rank=3), B2(rank=4), A3(rank=5), C1(rank=6)
It would be better for result to re-sort the result like this:
A1(rank=1), B1(rank=3), A2(rank=2), B2(rank=4), A3(rank=5), C1(rank=6)
(A2 is put after B1, so A2 is not adjacent to A1.)
Changing product rank to incorporate this behavior would not suffice, because products can be arbitrarily filtered (ie. there can be list with only A1, B1, or A2, B1, C1 - these lists do not need to be re-sorted).
Is there a way to achieve this with Elasticsearch? Would it be possible to achieve this writing custom plugin?