原英文版地址: https://www.elastic.co/guide/en/elasticsearch/reference/7.7/search-aggregations-bucket-filters-aggregation.html, 原文档版权归 www.elastic.co 所有
本地英文版地址: ../en/search-aggregations-bucket-filters-aggregation.html
本地英文版地址: ../en/search-aggregations-bucket-filters-aggregation.html
重要: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 当前版本文档。
定义一个多桶聚合,其中每个桶都与一个过滤器相关联。每个桶都会收集与其关联的过滤器匹配的所有文档。
示例:
PUT /logs/_bulk?refresh { "index" : { "_id" : 1 } } { "body" : "warning: page could not be rendered" } { "index" : { "_id" : 2 } } { "body" : "authentication error" } { "index" : { "_id" : 3 } } { "body" : "warning: connection timed out" } GET logs/_search { "size": 0, "aggs" : { "messages" : { "filters" : { "filters" : { "errors" : { "match" : { "body" : "error" }}, "warnings" : { "match" : { "body" : "warning" }} } } } } }
上面的例子是对日志(logs)的消息的分析。 例子中的聚合将构建两个日志的消息的集合(桶)——一个用于所有包含错误的消息,另一个用于所有包含警告的消息。
响应:
{ "took": 9, "timed_out": false, "_shards": ..., "hits": ..., "aggregations": { "messages": { "buckets": { "errors": { "doc_count": 1 }, "warnings": { "doc_count": 2 } } } } }
filters 字段也可以指定为一个筛选器数组,比如下面这个请求:
GET logs/_search { "size": 0, "aggs" : { "messages" : { "filters" : { "filters" : [ { "match" : { "body" : "error" }}, { "match" : { "body" : "warning" }} ] } } } }
过滤后的桶按照请求中给定的顺序返回。这个例子的响应是:
{ "took": 4, "timed_out": false, "_shards": ..., "hits": ..., "aggregations": { "messages": { "buckets": [ { "doc_count": 1 }, { "doc_count": 2 } ] } } }
可以设置 other_bucket
参数,以便向响应中添加一个桶,该桶将包含不匹配任何给定的过滤器的所有文档。
该参数的值可以设置为:
-
false
-
不会计算
other
桶 -
true
-
计算并返回
other
桶,当使用命名过滤器时返回一个命名(默认名称为_other_
)的桶,否则(使用匿名过滤器时)就是返回的桶中的最后一个
参数 other_bucket_key
可用于设置 other
桶的键,以取代默认的_other_
。
设置此参数将隐式地将 other_bucket
参数设置为true
。
下面的代码片段显示了一个响应,其中 other
桶在请求中被命名为 other_messages
。
PUT logs/_doc/4?refresh { "body": "info: user Bob logged out" } GET logs/_search { "size": 0, "aggs" : { "messages" : { "filters" : { "other_bucket_key": "other_messages", "filters" : { "errors" : { "match" : { "body" : "error" }}, "warnings" : { "match" : { "body" : "warning" }} } } } } }
响应应该是这样的:
{ "took": 3, "timed_out": false, "_shards": ..., "hits": ..., "aggregations": { "messages": { "buckets": { "errors": { "doc_count": 1 }, "warnings": { "doc_count": 2 }, "other_messages": { "doc_count": 1 } } } } }