在ElasticSearch中组合范围和直方图过滤器

时间:2013-04-04 20:34:43

标签: elasticsearch

考虑存储在ElasticSearch中的事件日志。事件看起来像这样

{
  "timestamp": "2013-04-04T15:38:17Z",
  "color": "red"
}

{
  "timestamp": "2013-04-04T17:51:21Z",
  "color": "green"
}

我想绘制每种颜色的频率时间轴。我知道我可以通过以下两个查询得到时间表:

{
  "query": {
    "match": {
      "color": "red"
    }
  },
  "size": 0,
  "facets": {
    "freq": {
      "date_histogram": {
        "interval": "day",
        "field": "timestamp"
      }
    }
  }
}

第二个查询"match": { "color": "green" }

我可以将这些查询合并为一个,这样我的任何一个

  • count(颜色)/ day / bucket
  • 或每种颜色的一个面,包含该颜色的直方图?

对于两种颜色,单独的查询可以正常工作,但您可能会怀疑这是一个受到尊重的示例。我真的处理十种以上的颜色,此时单个查询会非常好。

1 个答案:

答案 0 :(得分:4)

您可以使用facet_filter将不同颜色的构面组合到同一个请求中:

{
    "facets": {
        "freq_red": {
            "date_histogram": {
                "interval": "day",
                "field": "timestamp"
            },
            "facet_filter": {
                "term": {
                    "color": "red"
                }
            }
        },
        "freq_green": {
            "date_histogram": {
                "interval": "day",
                "field": "timestamp"
            },
            "facet_filter": {
                "term": {
                    "color": "green"
                }
            }
        }
    }
}