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

前端 未结 5 1330
無奈伤痛
無奈伤痛 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 03:58

    What I do is use something like the following query:

    "aggs" : {
        "products" : {
          "filter" : { "term": { "item.category": "children" }},
          "aggs" : {
            "count" : {
              "terms" : {
                "script": "doc['item.id'].value + ':' + doc['item.name'].value"
              }
            }
          }
        }
      }
    

    Which returns something like this:

    ...
    "aggregations" : {
        "products" : {
          "doc_count" : 1050,
          "count" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "x2_90QBj9k:Baby Oil",
                "doc_count" : 45
              },
              ...
            ]
    ...
    

    And then I can use a string operation on bucket[i]["key"], for each i in a loop, to extract the relevant field.

提交回复
热议问题