elasticsearch 聚合分桶之后,获取桶的总数

因为es 分桶之后, 默认返回桶的size =10 . 也就是如果分桶之后,桶很多的这种情况怎么获取桶总数?

那么 目前我需要指定 分桶之后,桶的总数多少?也就是 一共会分出多少个桶?

同事说 可以通过 返回结果的 里面的: sum_other_doc_count 来 得出还有多少个桶没有返回。

其实是不对,它只是表示了还有多少个文档没有 分桶而已。因为你需要的 桶 数据已经 找出来了。

并不能作为 分桶数的。

目前我的做法是 只能通过 调整 size的 桶大小来了, 把size 写到最大,就把所有桶都返回了

例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
GET /collect_data_store_goods_fact*/_search
{
 
  "query": {
    "match_all": {}
  },
  "size": 0,
 
  "aggs": {
   
       "brand":{
         "terms": {
           "field": "orgId",
            "size": 1000000
         
         }
         
         
       }
       
       
      ,"stats_monthly_sales": {
            "stats_bucket": {
                "buckets_path": "brand>_count"
            }
        }  
       
   
  }
   
 
}

结果
{
  "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 10,
    "successful" : 10,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 30,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "brand" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 13274,
          "doc_count" : 17
        },
        {
          "key" : 19,
          "doc_count" : 7
        },
        {
          "key" : 7381,
          "doc_count" : 1
        },
        {
          "key" : 13278,
          "doc_count" : 1
        },
        {
          "key" : 13295,
          "doc_count" : 1
        },
        {
          "key" : 13296,
          "doc_count" : 1
        },
        {
          "key" : 13297,
          "doc_count" : 1
        },
        {
          "key" : 13298,
          "doc_count" : 1
        }
      ]
    },
    "stats_monthly_sales" : {
      "count" : 8,
      "min" : 1.0,
      "max" : 17.0,
      "avg" : 3.75,
      "sum" : 30.0
    }
  }
}

然后通过 count 来 获取桶的数量即可。 也是没有办法的事了