弹性搜索1.7:在嵌套术语聚合中返回空桶

时间:2015-08-06 19:56:31

标签: aggregation elasticsearch

我在这里有一个聚合查询:

  

按类型返回记录数,按创建者在过去6个月内分组。

查询如下:

GET /test/records/_search?search_type=count
{
  "aggs": {
    "timeRange": {
      "filter": {
        "range": {
          "When": {
            "gte": "now-6M",
            "lte": "now"
          }
        }
      },
      "aggs": {
        "groupBy": {
          "terms": {
            "field": "Creator",
            "min_doc_count": 0
          },
          "aggs": {
            "counts": {
              "terms": {
                "field": "Type",
                "min_doc_count": 0
              }
            }
          }
        }
      }
    }
  }
}

结果显示为:

{
   "took": 11,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 261,
      "max_score": 0,
      "hits": []
   },
   "aggregations": {
      "timeRange": {
         "doc_count": 192,
         "groupBy": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
               {
                  "key": "ff94d50a-9ced-4877-85cc-a08a00fd49f4",
                  "doc_count": 175,
                  "counts": {
                     "doc_count_error_upper_bound": 0,
                     "sum_other_doc_count": 3,
                     "buckets": [
                        {
                           "key": "9f937783-dc28-421c-a937-a0c201643aae",
                           "doc_count": 95
                        },
                        {
                           "key": "36e4b200-e8ca-47f5-b9bb-a09101058595",
                           "doc_count": 31
                        },
                        {
                           "key": "cf421f05-37b1-470e-9ab9-a0bb0100792d",
                           "doc_count": 11
                        }
                     ]
                  }
               },
               {
                  "key": "be8ca900-0011-0002-1976-c737a7e00000",
                  "doc_count": 0,
                  "counts": {
                     "doc_count_error_upper_bound": 0,
                     "sum_other_doc_count": 0,
                     "buckets": []
                  }
               },
               {
                  "key": "fae866a8-705e-e111-bd17-d6ec07ced130",
                  "doc_count": 0,
                  "counts": {
                     "doc_count_error_upper_bound": 0,
                     "sum_other_doc_count": 0,
                     "buckets": []
                  }
               }
            ]
         }
      }
   }
}

从上面列出的结果中可以看出,当一个术语聚合中有一个0计数时,它就不会在下面的级别中列出0。

  

我的问题是:在这些情况下是否有可能显示0个计数?

以下示例所需输出:

{
   "took": 11,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 261,
      "max_score": 0,
      "hits": []
   },
   "aggregations": {
      "timeRange": {
         "doc_count": 192,
         "groupBy": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
               {
                  "key": "ff94d50a-9ced-4877-85cc-a08a00fd49f4",
                  "doc_count": 175,
                  "counts": {
                     "doc_count_error_upper_bound": 0,
                     "sum_other_doc_count": 3,
                     "buckets": [
                        {
                           "key": "9f937783-dc28-421c-a937-a0c201643aae",
                           "doc_count": 95
                        },
                        {
                           "key": "36e4b200-e8ca-47f5-b9bb-a09101058595",
                           "doc_count": 31
                        },
                        {
                           "key": "cf421f05-37b1-470e-9ab9-a0bb0100792d",
                           "doc_count": 11
                        }
                     ]
                  }
               },
               {
                  "key": "be8ca900-0011-0002-1976-c737a7e00000",
                  "doc_count": 0,
                  "counts": {
                     "doc_count_error_upper_bound": 0,
                     "sum_other_doc_count": 0,
                     "buckets": [
                        {
                           "key": "9f937783-dc28-421c-a937-a0c201643aae",
                           "doc_count": 0
                        },
                        {
                           "key": "36e4b200-e8ca-47f5-b9bb-a09101058595",
                           "doc_count": 0
                        },
                        {
                           "key": "cf421f05-37b1-470e-9ab9-a0bb0100792d",
                           "doc_count": 0
                        }
                     ]
                  }
               },
               {
                  "key": "fae866a8-705e-e111-bd17-d6ec07ced130",
                  "doc_count": 0,
                  "counts": {
                     "doc_count_error_upper_bound": 0,
                     "sum_other_doc_count": 0,
                     "buckets": [
                        {
                           "key": "9f937783-dc28-421c-a937-a0c201643aae",
                           "doc_count": 0
                        },
                        {
                           "key": "36e4b200-e8ca-47f5-b9bb-a09101058595",
                           "doc_count": 0
                        },
                        {
                           "key": "cf421f05-37b1-470e-9ab9-a0bb0100792d",
                           "doc_count": 0
                        }
                     ]
                  }
               }
            ]
         }
      }
   }
}

1 个答案:

答案 0 :(得分:0)

这是Clemens Klein-Robbenhaar对主要帖子的评论。简而言之:我知道期望什么值,但Elasticsearch没有。一旦计算出该组的聚合总数为0,它就不知道在该网下搜索什么,因为什么都没有。