Products sort with pick products from every brand and list accordingly

I do have 10 products with different brands.
Brand1 contains 3 products
Brand2 contains 4 products
Brand3 contains 3 products

Currently I am getting output as per relevance sorting (given example below)
{
'id': 1,
'name': 'product1',
'brand': 'brand3',
},{
'id': 3,
'name': 'product3',
'brand': 'brand2',
},{
'id': 13,
'name': 'product3',
'brand': 'brand1',
},{
'id': 2,
'name': 'product4',
'brand': 'brand3',
},{
'id': 5,
'name': 'product5',
'brand': 'brand2',
},{
'id': 9,
'name': 'product6',
'brand': 'brand3',
},{
'id': 17,
'name': 'product7',
'brand': 'brand2',
},{
'id': 20,
'name': 'product12',
'brand': 'brand2',
},{
'id': 23,
'name': 'product15',
'brand': 'brand1',
},{
'id': 7,
'name': 'product10',
'brand': 'brand1',
}

Now, what I want is as per below.
Sort products as per pick 1 products from every brand and then next 1 products from every brand and so on (as per below example).
{
'id': 13,
'name': 'product3',
'brand': 'brand1',
},{
'id': 3,
'name': 'product3',
'brand': 'brand2',
},{
'id': 1,
'name': 'product1',
'brand': 'brand3',
},{
'id': 23,
'name': 'product15',
'brand': 'brand1',
},{
'id': 5,
'name': 'product5',
'brand': 'brand2',
},{
'id': 2,
'name': 'product4',
'brand': 'brand3',
},{
'id': 7,
'name': 'product10',
'brand': 'brand1',
},{
'id': 17,
'name': 'product7',
'brand': 'brand2',
},{
'id': 9,
'name': 'product6',
'brand': 'brand3',
},{
'id': 20,
'name': 'product12',
'brand': 'brand2',
}

Welcome!

I don't see how you can do that automatically.
I mean that I'd may be do a terms agg and a top_hits agg (See an example at https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html) and then I'd do the split you need "manually" in your application.

Or I'd use a terms agg to get the list of all brands and then run a multisearch API with one search request per brand. Then again I'd take the result and reorganize it to match your needs.

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