【问题标题】:ElasticSearch - How to display an additional field name in "Multi level" aggregation query?ElasticSearch - 如何在“多级”聚合查询中显示附加字段名称?
【发布时间】:2021-01-20 23:32:21
【问题描述】:

如何在我的输出存储桶中添加一个名为“state_location,district_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 - How to display an additional field name in aggregation query 中解释的方法,但这不适用于多级聚合。我尝试了下面的代码

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

【问题讨论】:

    标签: elasticsearch elasticsearch-aggregation


    【解决方案1】:

    我得到了解决方案。可能会对其他人有所帮助。以下是更新后的查询:-

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

    【讨论】:

      猜你喜欢
      • 2015-10-21
      • 1970-01-01
      • 1970-01-01
      • 2021-10-09
      • 1970-01-01
      • 2020-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多