ElasticSearch - How to display an additional field name in "Multi level" aggregation query?

83 Views Asked by At

How can I add a new key called 'state_location,district_location' in my output bucket.

I am running an aggregation code as shown below

  "from": 0,
  "size": 3,
  "query": {
    "bool": {}
  },
  "aggs": {
    "State Location": {
      "terms": {
        "field": "block_location"
      },
      "aggs": {
        "stakeholder Level": {
          "terms": {
            "field": "stakeholder_level"
          },
          "aggs": {
            "stakeholder_category": {
              "terms": {
                "field": "stakeholder_category"
              },
              "aggs": {
                "Coverage Calculation": {
                  "terms": {
                    "field": "stakeholder_name"
                  },
                  "aggs": {
                    "Total Hours": {
                      "sum": {
                        "field": "field_capacity_building_duration"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

I tried method explained in ElasticSearch - How to display an additional field name in aggregation query , but that is not working for multi level aggregation. I tried the code below

 {
  "from": 0,
  "size": 0,
  "query": {
    "bool": {}
  },
  "aggs": {
    "State Location": {
      "terms": {
        "field": "block_location"
      },
      "aggs": {
        "state": {
          "top_hits": {
            "size": 1,
            "_source": {
              "includes": ["state_location","district_location"]
            }
          }
        },
      
      "aggs": {
        "stakeholder Level": {
          "terms": {
            "field": "stakeholder_level"
          },
          "aggs": {
            "stakeholder_category": {
              "terms": {
                "field": "stakeholder_category"
              },
              "aggs": {
                "Coverage Calculation": {
                  "terms": {
                    "field": "stakeholder_name"
                  },
                  "aggs": {
                    "Total Hours": {
                      "sum": {
                        "field": "field_capacity_building_duration"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
}
1

There are 1 best solutions below

0
On

I got the solution. May be it will help for someone else. Following is the updated query :-

{
  "from": 0,
  "size": 0,
  "query": {
    "bool": {}
  },
  "aggs": {
    "State Location": {
      "terms": {
        "field": "block_location"
      },
      "aggs": {
        "stakeholder Level": {
          "terms": {
            "field": "stakeholder_level"
          },
          "aggs": {
            "stakeholder_category": {
              "terms": {
                "field": "stakeholder_category"
              },
              "aggs": {
                "Coverage Calculation": {
                  "terms": {
                    "field": "stakeholder_name"
                  },
                  "aggs": {
                    "Total Hours": {
                      "sum": {
                        "field": "field_capacity_building_duration"
                      }
                    },
                    "include_source": {
                      "top_hits": {
                        "size": 1,
                        "_source": {
                          "includes": [
                            "state_location",
                            "district_location"
                          ]
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}