# Terms aggregation on named queries

**URL:** <https://discuss.elastic.co/t/terms-aggregation-on-named-queries/62595>\
**Category:** Elasticsearch\
**Created:** [October 10, 2016, 5:01am UTC](https://discuss.elastic.co/t/terms-aggregation-on-named-queries/62595 "2016-10-10T05:01:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonsgreen](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@jonsgreen](https://discuss.elastic.co/u/jonsgreen)\
**Post date:** [October 10, 2016, 5:01am UTC](https://discuss.elastic.co/t/terms-aggregation-on-named-queries/62595/1 "2016-10-10T05:01:31Z")

</div>

I was hoping to be able to do a terms aggregation on the 'matched\_queries' field that is generated when doing a named query. Since the matched\_queries is added outside the document it is not accessible as a field for a terms aggregation at this time. I am curious if this could be a feature request or is it just not possible due to how aggregations are built.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [October 10, 2016, 10:56am UTC](https://discuss.elastic.co/t/terms-aggregation-on-named-queries/62595/2 "2016-10-10T10:56:32Z")

</div>

I had a similar disappointment when I discovered I couldn't use these names in terms aggs.

Unfortunately this information is only derived at the fetch phase for individual docs not inline in the collect phase when aggs run.

> I am curious if this could be a feature request

It is likely to require changes to core Lucene. My view was that Lucene Query clauses currently gather only a score for each stream of matching docs. Maybe, like the Lucene tokenization API [1] , additional metadata could optionally be emitted via a search equivalent of TokenStream Attribute objects.

This would allow each doc to have arbitrary "match metadata" attached as is if they were properties of the document. A name/tag is an example of one piece of metadata e.g. your Boolean OR query that looks for terms `elasticsearch`, `logstash` or `kibana` could associate the user-supplied tag `elasticstack` for use in aggs. As well as specifically tagging a user-defined category like this you could attach a numeric measure of belonging to a category e.g. music-listener profiles could be ranked on their "death-metal-ness" or "jazz-ness" by supplying lists of bands in these genres and returning the number of band names a user matched in each query clause. These numbers provide a level of "about-ness" which could be plotted in a histogram agg etc.

Some of this is achievable today if you mess around with boosts, constant\_score and scripted aggs to smuggle metadata out in Lucene's single float score but it is a less than ideal way of getting at details behind the Lucene matching logic.

[1] [TokenStream (Lucene 5.3.1 API)](https://lucene.apache.org/core/5_3_1/core/org/apache/lucene/analysis/TokenStream.html)

---

<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:13pm UTC](https://discuss.elastic.co/t/terms-aggregation-on-named-queries/62595/3 "2017-07-05T22:13:38Z")

</div>


