标签:嵌套 bucket 问题 disable can index pre histogram 否则
按时间统计:date_histogram
GET /cars/transactions/_search { "size" : 0, "aggs": { "sales": { "date_histogram": { "field": "sold", "interval": "month", "format": "yyyy-MM-dd" } } } }
注意,这种写法在数字字段没有问题,但是在文本字段使用之前必须先把对应字段的fielddata设为true才可以。否则会报错:
"caused_by" : { "type" : "illegal_argument_exception", "reason" : "Fielddata is disabled on text fields by default. Set fielddata=true on [name] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory." }
注意,设置了fielddata会消耗大量内存。
返回空buckets:设置参数 min_doc_count
限制返回时间范围:设置参数 extended_bounds
GET /cars/transactions/_search { "size" : 0, "aggs": { "sales": { "date_histogram": { "field": "sold", "interval": "month", "format": "yyyy-MM-dd", "min_doc_count" : 0, "extended_bounds" : { "min" : "2014-01-01", "max" : "2014-12-31" } } } } }
扩展例子:
GET /cars/transactions/_search { "size" : 0, "aggs": { "sales": { "date_histogram": { "field": "sold", "interval": "quarter", "format": "yyyy-MM-dd", "min_doc_count" : 0, "extended_bounds" : { "min" : "2014-01-01", "max" : "2014-12-31" } }, "aggs": { "per_make_sum": { "terms": { "field": "make" }, "aggs": { "sum_price": { "sum": { "field": "price" } } } }, "total_sum": { "sum": { "field": "price" } } } } } }
分析一下上面的例子:
先是一个聚合,名为sales,类型是date_histogram,会根据时间生成桶
再看sales桶里,嵌套了一个聚合,包括per_make_sum和total_sum,
total_sum:用到了sum,这是一个指标,不会产生新的桶,会生成数值。
per_make_sum:用到了terms,会根据make字段划分子桶
在per_make_sum桶中,嵌套了sum_price聚合,也是一个sum指标,不会产生新桶。
上面的层级结构表示了桶的嵌套关系。关键在于区分哪些是桶,哪些是指标。
标签:嵌套 bucket 问题 disable can index pre histogram 否则
原文地址:http://www.cnblogs.com/dplearning/p/7018656.html