ElasticSearch - How to display an additional field name in aggregation query

前端 未结 5 1329
無奈伤痛
無奈伤痛 2021-02-12 03:51

How can I add a new key called \'agency_name\' in my output bucket.

I am running an aggregation code as shown below

{
  \"aggs\": {
    \"name\": {
             


        
5条回答
  •  悲&欢浪女
    2021-02-12 04:14

    You can use the top hits aggregation like in the link below. The format will be slightly different since creating the extra aggregation will embed the agency name under another 'hits' key.

    Adding additional fields to ElasticSearch terms aggregation

    {
      "aggs": {
        "name": {
          "terms": {
            "field": "agency_code"
          },
          "aggs": {
            "agency_names" : {
               "top_hits": {
                    size: 1, 
                    _source: {
                        include: ['agency_name']
                    }
                }
             } 
           }
        }
      }
    }
    

提交回复
热议问题