简体   繁体   中英

Elasticsearch Highlight issue on aggregated fields

I am using ElasticSearch and want to get highlighted field on the aggregated result of the search query.

I do not want to get results of search query, so I kept the size to be 0 which only gives me aggregated results.

Now I want to apply highlighters on the aggregated results, but that is not working. I am using a term aggregator and a top-hits aggregator as the sub aggregator. In the ES docs they have mentioned top-hits aggregator supports highlighting.

My structure of query goes like this:

{
    size:0,
    query:{
        .......
    },
    aggregation:{
        name-of-agg:{
            term:{
                ....
            },
            aggregation:{
                name-of-sub-agg:{
                    top-hits:{
                        ....
                    }
                }
            }
        },
        highlight:{
            fields:{
                fieldname:{

                }
            }
        }
    }
}

You have to set the highlight inside the top_hits aggregation properties (not inside of aggregation ).

Here is a working minimal example:

echo create index
curl -XPUT 'http://127.0.0.1:9010/files?pretty=1' -d '
{
  "settings": {
  }
}'
echo create type
curl -XPUT 'http://127.0.0.1:9010/files/_mapping/file?pretty=1' -d'
{
  "properties":{
    "fileName":{
      "type":"string",
      "term_vector":"with_positions_offsets"
    }
  }
}
'
echo insert files
curl -XPUT 'http://127.0.0.1:9010/files/file/1?pretty=1' -d'
{
  "fileName":"quick brown fox"
}
'
echo flush
curl -XPOST 'http://127.0.0.1:9010/files/_flush?pretty=1'
echo search brown tophits
curl -XGET 'http://127.0.0.1:9010/files/file/_search?pretty=1' -d '
{
  "size" : 0,
  "query":{
    "match":{
      "fileName":"brown"
    }
  },
  "aggregations" : {
    "docs" : {
      "top_hits" : {
        "highlight": {
          "fields": {
            "fileName": {}
          }
        }
      }
    }
  }
}'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM