# Highlighting for top hits aggregations

**URL:** https://discuss.elastic.co/t/highlighting-for-top-hits-aggregations/41344
**Category:** Elasticsearch
**Created:** [February 10, 2016, 3:44am UTC](https://discuss.elastic.co/t/highlighting-for-top-hits-aggregations/41344 "2016-02-10T03:44:47Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Patrick\_Lam](https://avatars.discourse-cdn.com/v4/letter/p/ed8c4c/32.png) [@Patrick\_Lam](https://discuss.elastic.co/u/Patrick_Lam)
#### Post date: [February 10, 2016, 3:44am UTC](https://discuss.elastic.co/t/highlighting-for-top-hits-aggregations/41344/1 "2016-02-10T03:44:47Z")

</div>

According to the documentation for top hits aggregations (ES 2.1), there is support for highlighting of the hits returned. Is there an example of how to implement this?

I've tried the following (bucket agg combined with top hits agg) and it doesn't seem to return any highlighted fields:

```
{'aggs': {
    'dup_groups': {
        'aggs': {
            'first_doc': {
                'top_hits': { 
                    'highlight': {'fields': {'text.stemmed': {}}},
                    'size': 1,
                    'sort': 'date'}}},
    'terms': {'field': 'dup_parent', 'size': 0}}},
 'query': {'query_string': {'query': 'world'}}}

```

I should note that my field `text.stemmed` works perfectly fine for highlighting in a regular search query without aggregations.

---

<div class="post-metadata">

### Author: ![Patrick\_Lam](https://avatars.discourse-cdn.com/v4/letter/p/ed8c4c/32.png) [@Patrick\_Lam](https://discuss.elastic.co/u/Patrick_Lam)
#### Post date: [February 10, 2016, 4:50am UTC](https://discuss.elastic.co/t/highlighting-for-top-hits-aggregations/41344/2 "2016-02-10T04:50:08Z")

</div>

Hmm okay so for future reference, the answer to this is that highlight must be defined BOTH outside and inside the aggregation for it to work, not just one or the other.

```
{'aggs': {'dup_groups': {'aggs': {'first_doc': {'top_hits': {'highlight': {'fields': {'text.stemmed': {}}},
      'size': 1,
      'sort': 'date'}}},
   'terms': {'field': 'dup_parent', 'size': 100}}},
 'highlight': {'fields': {'text.stemmed': {'number_of_fragments': 0}}},
 'query': {'query_string': {'default_field': 'text.stemmed',
   'query': 'world'}},
 'sort': [{'_score': {'order': 'desc'}}]}
```

---

<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, 11:17pm UTC](https://discuss.elastic.co/t/highlighting-for-top-hits-aggregations/41344/3 "2017-07-05T23:17:42Z")

</div>


