多个过滤器和弹性搜索中的聚合

如何使用与弹性搜索中的聚合相关联的过滤器?

官方文档仅给出了filteraggregations的简单示例,并没有对查询dsl的正式描述 – 比较它。与postgres documentation

通过尝试,我发现以下查询,这是通过elasticsearch(没有解析错误)接受,但忽略给定的过滤器:

{
  "filter": {
    "and": [
      {
        "term": {
          "_type": "logs"
        }
      },{
        "term": {
          "dc": "eu-west-12"
        }
      },{
        "term": {
          "status": "204"
        }
      },{
        "range": {
          "@timestamp": {
            "from": 1398169707,"to": 1400761707
          }
        }
      }
    ]
  },"size": 0,"aggs": {
    "time_histo": {
      "date_histogram": {
        "field": "@timestamp","interval": "1h"
      },"aggs": {
        "name": {
          "percentiles": {
            "field": "upstream_response_time","percents": [
              98.0
            ]
          }
        }
      }
    }
  }
}

有些人建议使用查询而不是过滤器。但官方文档通常建议the opposite对精确值进行过滤。查询的另一个问题是:当过滤器提供and时,查询不会。

有人可以指出文档,博客或书籍,它们描述了编写非平凡查询:至少一个聚合加多个过滤器。

我最终使用了 filter aggregation – 未过滤的查询。所以现在我有3个嵌套的aggs元素。

我也使用布尔过滤器,而不是由@ alex-brasetvik推荐,因为http://www.elasticsearch.org/blog/all-about-elasticsearch-filter-bitsets/

我的最终实现:

{
  "aggs": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
              "term": {
                "_type": "logs"
              }
            },{
              "term": {
                "dc": "eu-west-12"
              }
            },{
              "term": {
                "status": "204"
              }
            },{
              "range": {
                "@timestamp": {
                  "from": 1398176502000,"to": 1400768502000
                }
              }
            }
          ]
        }
      },"aggs": {
        "time_histo": {
          "date_histogram": {
            "field": "@timestamp","interval": "1h"
          },"aggs": {
            "name": {
              "percentiles": {
                "field": "upstream_response_time","percents": [
                  98.0
                ]
              }
            }
          }
        }
      }
    }
  },"size": 0
}

相关文章

迭代器模式(Iterator)迭代器模式(Iterator)[Cursor]意图...
高性能IO模型浅析服务器端编程经常需要构造高性能的IO模型,...
策略模式(Strategy)策略模式(Strategy)[Policy]意图:定...
访问者模式(Visitor)访问者模式(Visitor)意图:表示一个...
命令模式(Command)命令模式(Command)[Action/Transactio...
生成器模式(Builder)生成器模式(Builder)意图:将一个对...