Highlighting for top hits aggregations


(Patrick Lam) #1

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.


(Patrick Lam) #2

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'}}]}

(system) #3