# Filter and aggregation case

**URL:** <https://discuss.elastic.co/t/filter-and-aggregation-case/63090>\
**Category:** Elasticsearch\
**Created:** [October 15, 2016, 12:33am UTC](https://discuss.elastic.co/t/filter-and-aggregation-case/63090 "2016-10-15T00:33:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohit\_nagpal](https://avatars.discourse-cdn.com/v4/letter/m/f14d63/32.png) [@mohit\_nagpal](https://discuss.elastic.co/u/mohit_nagpal)\
**Post date:** [October 15, 2016, 12:33am UTC](https://discuss.elastic.co/t/filter-and-aggregation-case/63090/1 "2016-10-15T00:33:35Z")

</div>

ID User Color priority  
1 1 blue 1  
2 1 black 2  
3 1 blue 3  
4 2 blue 2  
5 2 blue 3  
6 3 blue 3  
7 4 Orange 3  
8 4 Orange 4  
9 5 Red 4  
10 5 Orange 5

Data set is like:

User like certain color but there is a priority associated with the liking. 1 is lowest priority.  
User can like same color with different priority

Target query : For each color tell the user count. for a user and color combination row with highest priority should be counted

For above data , result should be

blue -\> 3  
Black -\> 1  
Orange -\> 2  
Red -\> 1

Rows should include in result  
for blue color  
1 blue 3 // consider only one row with highest priority among the all rows for user = 1 and color = blue  
2 blue 3 // consider only one row with highest priority among the all rows for user = 2 and color = blue  
3 blue 3 // consider only one row with highest priority among the all rows for user = 3 and color = blue

for black color  
1 black 2

for orange color  
4 Orange 4 //consider only one row with highest priority among the all rows for user = 4 and color = Orange  
5 Orange 5

for red color  
5 Red 4

Using filter on color -\> blue following rows  
1 1 blue 1  
3 1 blue 3  
4 1 blue 2  
5 2 blue 3  
6 2 blue 3

This row count is 5 but target is to get row count-\> 3.  
Which rows should be counted :  
1 1 blue 3 // consider only one row with highest priority among the all rows for user = one and color = blue  
4 2 blue 3 // consider only one row with highest priority among the all rows for user = two and color = blue  
6 3 blue 3 // consider only one row with highest priority among the all rows for user = three and color = blue

Is there any way to achieve that using nested filers / aggregrations or any other way ?

---

<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:** [July 5, 2017, 10:12pm UTC](https://discuss.elastic.co/t/filter-and-aggregation-case/63090/2 "2017-07-05T22:12:06Z")

</div>

Closed on July 5, 2017, 10:12pm UTC


