条款汇总未返回其他存储桶

时间:2019-06-18 06:27:53

标签: elasticsearch

在组合过滤器聚合时,我无法通过术语聚合获得其他存储桶。无论如何要在Elasticsearch中做到这一点?

映射:具有嵌套地址的客户。具有嵌套属性的地址。

我尝试了以下方法,

{
  "size": 0,
  "aggs": {
    "address": {
      "nested": {
        "path": "address"
      },
      "aggs": {
        "shipping_to_address": {
          "aggs": {
            "city": {
              "terms": {
                "field": "address.city.name.keyword",
                "size": 10,
                "missing": "others"
              }
            }
          },
          "filter": {
            "bool": {
              "must": [
                {
                  "nested": {
                    "path": "address.properties",
                    "query": {
                      "bool": {
                        "filter": [
                          {
                            "term": {
                              "address.properties.type": "shipping_to"
                            }
                          }
                        ]
                      }
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }
  }
}

以上内容仅返回与过滤器匹配的存储桶。

{
  "hits": {
    "total": 3,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "address": {
      "doc_count": 3,
      "shipping_to_address": {
        "doc_count": 1,
        "city": {
          "doc_count_error_upper_bound": 0,
          "sum_other_doc_count": 0,
          "buckets": [
            {
              "key": "new york",
              "doc_count": 1
            }
          ]
        }
      }
    }
  }
}

我希望看到其他存储桶,如下所示:

          "buckets": [
            {
              "key": "new york",
              "doc_count": 1
            },
            {
              "key": "others",
              "doc_count": 2
            }      
          ]

1 个答案:

答案 0 :(得分:0)

您需要在条款聚合中添加“ min_doc_count”:0,它将返回空存储桶。 Link供参考

{
  "size": 0,
  "aggs": {
    "address": {
      "nested": {
        "path": "address"
      },
      "aggs": {
        "shipping_to_address": {
          "aggs": {
            "city": {
              "terms": {
                "field": "address.city.name.keyword",
                "size": 10,
                "min_doc_count":0,
                "missing": "others"
              }
            }
          },
          "filter": {
            "bool": {
              "must": [
                {
                  "nested": {
                    "path": "address.properties",
                    "query": {
                      "bool": {
                        "filter": [
                          {
                            "term": {
                              "address.properties.type": "shipping_to"
                            }
                          }
                        ]
                      }
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }
  }
}
相关问题