# Terms Aggregation Restricted to Top Matches?

**URL:** <https://discuss.elastic.co/t/terms-aggregation-restricted-to-top-matches/21194>\
**Category:** Elasticsearch\
**Created:** [December 10, 2014, 4:59pm UTC](https://discuss.elastic.co/t/terms-aggregation-restricted-to-top-matches/21194 "2014-12-10T16:59:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_H\_3](https://avatars.discourse-cdn.com/v4/letter/c/b487fb/32.png) [@Chris\_H\_3](https://discuss.elastic.co/u/Chris_H_3)\
**Post date:** [December 10, 2014, 4:59pm UTC](https://discuss.elastic.co/t/terms-aggregation-restricted-to-top-matches/21194/1 "2014-12-10T16:59:19Z")

</div>

Hi,

Is it possible to do a Terms aggregation in Elasticsearch, but restricted  
to the top N matching documents?  
I'm aware of the top\_hits aggregator, but it does not allow sub  
aggregations.  
So while I can get the top N matches per term, I can't find a way to get  
the terms of the top N matches.

My current query is something like:

GET /myindex/mydoc/\_search  
{  
"size": 0,  
"query": {"match": {  
"field\_to\_search": "some search string"  
}}  
,"aggs": {  
"term\_aggregation": {  
"terms": {  
"field": "Tags",  
"size": 0,  
"order": {  
"summed\_score": "desc"  
}  
},  
"aggs":{"summed\_score":{"sum":{"script":"doc.score"}} }  
}  
}  
}

which aggregates the scores of ALL matching documents by terms in the Tags  
field.

Is there any way to do what I want?

Thanks

--  
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/8a3df48e-2551-45db-91e7-d827f141dcb8%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8a3df48e-2551-45db-91e7-d827f141dcb8%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood\_2](https://avatars.discourse-cdn.com/v4/letter/m/c77e96/32.png) [@Mark\_Harwood\_2](https://discuss.elastic.co/u/Mark_Harwood_2)\
**Post date:** [December 10, 2014, 5:31pm UTC](https://discuss.elastic.co/t/terms-aggregation-restricted-to-top-matches/21194/2 "2014-12-10T17:31:56Z")

</div>

Working on it ... [Aggregations: new “Sampler” provides a filter for top-scoring docs by markharwood · Pull Request #8191 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/pull/8191)

On Wednesday, December 10, 2014 4:59:19 PM UTC, Chris H wrote:

> Hi,
> 
> Is it possible to do a Terms aggregation in Elasticsearch, but restricted  
> to the top N matching documents?  
> I'm aware of the top\_hits aggregator, but it does not allow sub  
> aggregations.  
> So while I can get the top N matches per term, I can't find a way to get  
> the terms of the top N matches.
> 
> My current query is something like:
> 
> GET /myindex/mydoc/\_search  
> {  
> "size": 0,  
> "query": {"match": {  
> "field\_to\_search": "some search string"  
> }}  
> ,"aggs": {  
> "term\_aggregation": {  
> "terms": {  
> "field": "Tags",  
> "size": 0,  
> "order": {  
> "summed\_score": "desc"  
> }  
> },  
> "aggs":{"summed\_score":{"sum":{"script":"doc.score"}} }  
> }  
> }  
> }
> 
> which aggregates the scores of ALL matching documents by terms in the Tags  
> field.
> 
> Is there any way to do what I want?
> 
> Thanks

--  
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/574a1596-e705-405c-868b-bb73d3452372%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/574a1596-e705-405c-868b-bb73d3452372%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, 12:44am UTC](https://discuss.elastic.co/t/terms-aggregation-restricted-to-top-matches/21194/3 "2017-07-06T00:44:23Z")

</div>


