# Take top n Documents per buckets and do further sub-aggregation

**URL:** <https://discuss.elastic.co/t/take-top-n-documents-per-buckets-and-do-further-sub-aggregation/19510>\
**Category:** Elasticsearch\
**Created:** [August 28, 2014, 1:38pm UTC](https://discuss.elastic.co/t/take-top-n-documents-per-buckets-and-do-further-sub-aggregation/19510 "2014-08-28T13:38:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![janek\_sendrowski](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@janek\_sendrowski](https://discuss.elastic.co/u/janek_sendrowski)\
**Post date:** [August 28, 2014, 1:38pm UTC](https://discuss.elastic.co/t/take-top-n-documents-per-buckets-and-do-further-sub-aggregation/19510/1 "2014-08-28T13:38:36Z")

</div>

Hi,

I like to take the best n Documents per user which is stored as user\_id in  
my index. This wouldn't be a problem until now. It could be done like this:

{  
"query":{  
"match":{  
"field":{  
"query":"query\_string"  
}  
}  
},  
"aggs":{  
"group\_by\_user":{  
"terms":{  
"field":"user\_id"  
},  
"aggs":{  
"top\_n":{  
"top\_hits":{  
"size":10  
}  
}  
}  
}  
}  
}

But now I like to do a sub-aggregation on it to calculate some expensive  
scoring and this isn't possible anymore, because top\_hits is a metric  
aggregation.

"aggs":{  
"max\_score\_per\_user":{  
"max":{  
"script":"advanced\_scoring"  
}  
}  
}  
}

My scoring algorithm is very expensive, so I can't apply it on the full  
document set per user which is returned by the query

I also can't use the rescore feature which provides a window parameter,  
because I first have to bucket the documents per user and then take the  
best n docs per user.

The range query would work, but the scoring aren't comparable because of  
the IDF. So I can't define a fixed range.

So I either have to make the scoring results comparable, which would be  
simple, but the constant\_score query doesn't work with the match query  
which I am using _or_ I have to find a way to reduce the bucket size to a  
certain limit while ordering by relevance.

I'm trying since days to find a way to do that, but it seems that it's not  
possible.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/197f68ab-8de4-445a-a7b8-d9b865370540%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/197f68ab-8de4-445a-a7b8-d9b865370540%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 1:05am UTC](https://discuss.elastic.co/t/take-top-n-documents-per-buckets-and-do-further-sub-aggregation/19510/2 "2017-07-06T01:05:48Z")

</div>


