【问题标题】:How to do two nested aggregations in elasticsearch?如何在elasticsearch中做两个嵌套聚合?
【发布时间】:2018-04-25 13:24:44
【问题描述】:

城市和家庭类型是以下文档映射中的两个嵌套对象:

"mappings" : {
  "home_index_doc" : {
    "properties" : {
      "city" : {
        "type" : "nested",
        "properties" : {
          "country" : {
            "type" : "nested",
            "properties" : {
              "name" : {
                "type" : "text"
              }
            }
          },
          "name" : {
            "type" : "keyword"
          }
        }
      },
      "home_type" : {
        "type" : "nested",
        "properties" : {
          "name" : {
            "type" : "keyword"
          }
        }
      },
      ...
    }
  }
}

我正在尝试进行以下聚合: 获取所有现有文件并显示每个城市的所有 home_types。

我想它应该类似于:

"aggregations": {
  "all_cities": {
    "buckets": [
      {
        "key": "Tokyo",
         "doc_count": 12,
         "home_types": {
            "buckets": [
               {
                  "key": "apartment", 
                  "doc_count": 5
               },
               {
                  "key": "house",
                  "doc_count": 12
               }
            ]
         }
      },
      {
        "key": "New York",
         "doc_count": 1,
         "home_types": {
            "buckets": [
               {
                  "key": "house", 
                  "doc_count": 1
               }
            ]
         }
      }
    ]
  }
}

在尝试了无数种方法和组合之后,我已经用 Kibana 做到了这一点:

GET home-index/home_index_doc/_search
{
  "size": 0,
  "aggs": {
    "all_cities": {
     "nested": {
         "path": "city"
      },
      "aggs": {
        "city_name": {
          "terms": {
            "field": "city.name"
          }
        }
      }
    },
    "aggs": {
      "all_home_types": {
        "nested": {
          "path": "home_type"
        },
        "aggs": {
          "home_type_name": {
            "terms": {
              "field": "home_type.name"
            }
          }
        }
      }
    }
  }
}

我得到以下异常:

    "type": "unknown_named_object_exception",
    "reason": "Unknown BaseAggregationBuilder [all_home_types]",

【问题讨论】:

    标签: elasticsearch nested aggregation elasticsearch-5


    【解决方案1】:

    您需要使用reverse_nested 以便在根级别跳出city 嵌套类型并为home_type 嵌套类型执行另一个nested 聚合。基本上是这样的:

    {
      "size": 0,
      "aggs": {
        "all_cities": {
          "nested": {
            "path": "city"
          },
          "aggs": {
            "city_name": {
              "terms": {
                "field": "city.name"
              },
              "aggs": {
                "by_home_types": {
                  "reverse_nested": {},
                  "aggs": {
                    "all_home_types": {
                      "nested": {
                        "path": "home_type"
                      },
                      "aggs": {
                        "home_type_name": {
                          "terms": {
                            "field": "home_type.name"
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-14
      • 2015-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-25
      相关资源
      最近更新 更多