将查询与日期直方图相结合

时间:2019-05-16 14:00:45

标签: elasticsearch

我想计算最近一小时的文档数量,并将其汇总到5分钟的存储桶中。 这是我的查询:


    GET logs-tsi-2019.05/tsi-json-log/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "range": {
                "@timestamp": {
                  "gte": "now-1h",
                  "lt": "now"
                }
              }
            },
            {
              "term" : {
                "application" : "sso"
              }
            },
            {
              "query_string": {
                "default_field": "*",
                "query": "grant_type=refresh_token",
                "analyze_wildcard": true
              }
            }
          ]
        }
      }
    }

如何将此查询与date_histogram汇总结合使用?

致谢, 克里斯

1 个答案:

答案 0 :(得分:1)

良好的开始!您可以这样做:

GET logs-tsi-2019.05/tsi-json-log/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "@timestamp": {
              "gte": "now-1h",
              "lt": "now"
            }
          }
        },
        {
          "term" : {
            "application" : "sso"
          }
        },
        {
          "query_string": {
            "default_field": "*",
            "query": "grant_type=refresh_token",
            "analyze_wildcard": true
          }
        }
      ]
    }
  },
  "aggs": {
    "5min": {
      "date_histogram": {
        "field": "@timestamp",
        "interval": "5m"
      }
    }
  }
}