# Question on top hits aggregation

**URL:** <https://discuss.elastic.co/t/question-on-top-hits-aggregation/19776>\
**Category:** Elasticsearch\
**Created:** [September 14, 2014, 6:45pm UTC](https://discuss.elastic.co/t/question-on-top-hits-aggregation/19776 "2014-09-14T18:45:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![AgentV](https://avatars.discourse-cdn.com/v4/letter/a/7bcc69/32.png) [@AgentV](https://discuss.elastic.co/u/AgentV)\
**Post date:** [September 14, 2014, 6:45pm UTC](https://discuss.elastic.co/t/question-on-top-hits-aggregation/19776/1 "2014-09-14T18:45:54Z")

</div>

Hello

I need to use the top hits aggregation  
[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html)  
for a grouping feature. The aggregation should be applied on nested fields.

e.g. My nested field is F with path P.

Would appreciate it if someone can share an example to create a top hits  
aggregation query for this scenario.

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/1fedd65a-c7af-407b-a1db-cd1e5c0918d7%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1fedd65a-c7af-407b-a1db-cd1e5c0918d7%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [September 15, 2014, 6:48am UTC](https://discuss.elastic.co/t/question-on-top-hits-aggregation/19776/2 "2014-09-15T06:48:31Z")

</div>

Hi,

In that case you would need to wrap a terms agg in a nested aggregation  
(with the desired path) and a top\_hits inside that terms aggregation.  
Unfortunately the top\_hits aggregation doesn't yet work inside a nested  
aggregation. However work is being done that will add this support:

> <https://github.com/elastic/elasticsearch/pull/7164>
>
> At the moment the \`top\_hits\` aggregation fails if it is being put in a nested or… reverse\_nested aggregations. This PR add proper support for nested inner objects being emitted as hits in the \`top\_hits\` aggregator. All the known fetch phase features are supported.
> 
> This PR adds fetch infrastructure to get #3022 in as well.

Martijn

On 14 September 2014 20:45, AgentV [coder.agentv@gmail.com](mailto:coder.agentv@gmail.com) wrote:

> Hello
> 
> I need to use the top hits aggregation  
> [http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html)  
> for a grouping feature. The aggregation should be applied on nested fields.
> 
> e.g. My nested field is F with path P.
> 
> Would appreciate it if someone can share an example to create a top hits  
> aggregation query for this scenario.
> 
> 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/1fedd65a-c7af-407b-a1db-cd1e5c0918d7%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1fedd65a-c7af-407b-a1db-cd1e5c0918d7%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/1fedd65a-c7af-407b-a1db-cd1e5c0918d7%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/1fedd65a-c7af-407b-a1db-cd1e5c0918d7%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
Met vriendelijke groet,

Martijn van Groningen

--  
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/CA%2BA76TwbOKqvHOfGU6zgUSzZE8wXtsodnDroJa3OUktKh%3DM4Dg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CA%2BA76TwbOKqvHOfGU6zgUSzZE8wXtsodnDroJa3OUktKh%3DM4Dg%40mail.gmail.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:02am UTC](https://discuss.elastic.co/t/question-on-top-hits-aggregation/19776/3 "2017-07-06T01:02:23Z")

</div>


