Limit number of results for each value of one field


(Christophe Rosko) #1

Hi !

I'm currently using filtered queries, and I need to get only the best 2 results for each value of field "type". For example, if my documents are :
{id: 1; type: 1; value:1}
{id: 2; type: 1; value:2}
{id: 3; type: 2; value:3}
{id: 4; type: 2; value:4}
{id: 5; type: 2; value:5}
{id: 6; type: 3; value:6}
{id: 7; type: 1; value:7}

When I'm querying and sorting by value desc, I want the results to be :
{id: 7; type: 1; value:7}
{id: 6; type: 3; value:6}
{id: 5; type: 2; value:5}
{id: 4; type: 2; value:4}
{id: 2; type: 1; value:2}

I tried to use top_hits aggregation but I couldn't get what I want.
Is there a way to do something like that ?

Thanks for your help :smile:


(Christophe Rosko) #2

Up ! :slight_smile:


(system) #3