四、范围
range
使用户能够定义一组范围 - 每个范围代表一个存储桶。在聚合过程中,将从每个文档中提取的值将针对每个存储桶范围进行检查,并针对相关/匹配文档进行"存储桶"检查。请注意,此聚合包括值,并排除每个范围的值请求示例:
GET sales/_search{ "aggs": { "price_ranges": { "range": { "field": "price", "keyed": true, "ranges": [ { "key": "cheap", "to": 100 }, { "key": "average", "from": 100, "to": 200 }, { "key": "expensive", "from": 200 } ] } } }}
返回示例:
{ ... "aggregations": { "price_ranges": { "buckets": { "cheap": { "to": 100.0, "doc_count": 2 }, "average": { "from": 100.0, "to": 200.0, "doc_count": 2 }, "expensive": { "from": 200.0, "doc_count": 3 } } } }}
dateRange
专用于日期值的范围聚合。此聚合与正常范围聚合之间的主要区别在于,可以在 Date Math 表达式中表示 and 值,并且还可以指定返回 和 响应字段的日期格式。请注意,此聚合包括值,并排除每个范围的值。请求示例:
POST /sales/_search?size=0{ "aggs": { "range": { "date_range": { "field": "date", "format": "MM-yyyy", "ranges": [ { "to": "now-10M/M" }, { "from": "now-10M/M" } ] } } }}
返回示例:
{ ... "aggregations": { "range": { "buckets": [ { "to": 1.4436576E12, "to_as_string": "10-2015", "doc_count": 7, "key": "*-10-2015" }, { "from": 1.4436576E12, "from_as_string": "10-2015", "doc_count": 0, "key": "10-2015-*" } ] } }}
ipRange和上面的时间范围聚合类型,此针对的是IP类型的数据请求示例:
GET /ip_addresses/_search{ "size": 10, "aggs": { "ip_ranges": { "ip_range": { "field": "ip", "ranges": [ { "to": "10.0.0.5" }, { "from": "10.0.0.5" } ] } } }}
返回示例:
{ ... "aggregations": { "ip_ranges": { "buckets": [ { "key": "*-10.0.0.5", "to": "10.0.0.5", "doc_count": 10 }, { "key": "10.0.0.5-*", "from": "10.0.0.5", "doc_count": 260 } ] } }}
还有更多的IP范围查询,详细:官方文档