如何在“多级”聚合查询中显示附加字段名?

pbpqsu0x  于 2021-06-10  发布在  ElasticSearch
关注(0)|答案(1)|浏览(293)

如何在输出桶中添加名为“state\u location,district\u location”的新键。
我正在运行一个聚合代码,如下所示

"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"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

我尝试了elasticsearch中解释的方法—如何在聚合查询中显示附加字段名,但这不适用于多级聚合。我试过下面的代码

{
  "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"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
}
2j4z5cfb

2j4z5cfb1#

我找到解决办法了。也许这对其他人有帮助。以下是更新的查询:-

{
  "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"
                          ]
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

相关问题