搜索Elasticsearch每天第一次发生事件

时间:2015-03-19 09:45:29

标签: elasticsearch kibana

我们使用Logstash接收日志,传递给Elasticsearch,并使用Kibana进行浏览。一个非常常见的设置。

每个条目中的一个字段是@timestamp,示例内容为03/18/2015 18:02:52。我应该使用什么过滤器来显示每天的第一个条目?

1 个答案:

答案 0 :(得分:3)

我不相信你可以用过滤器做到这一点 - 一天中的第一个不是你可以通过查看单个文件确定的属性。但是,您应该能够使用聚合执行此操作:首先使用带有间隔日的date_histogram进行聚合,以按日对事件进行分组。然后使用top_hits聚合每天提取一个结果(需要elasticsearch 1.3或更高版本)。您的查询应如下所示

{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "by-day": {
      "date_histogram": {
        "field": "timestamp",
        "interval": "day"
      },
      "aggs": {
        "top_for_day": {
          "top_hits": {
            "size": 1,
            "sort": [
              {
                "timestamp": {
                  "order": "asc"
                }
              }
            ]
          }
        }
      }
    }
  }
}

哪个应该产生结果(为了简洁而略微修剪)

{
  "aggregations": {
    "by-day": {
      "buckets": [
        {
          "key_as_string": "2015-02-01T00:00:00.000Z",
          "key": 1422748800000,
          "doc_count": 7635,
          "top_for_day": {
            "hits": {
              "total": 7635,
              "max_score": null,
              "hits": [
                {
                  "_index": "events-2015-02",
                  "_type": "event",
                  "_id": "c64f85ac-a870-441f-bedb-e24db47fd02a",
                  "_score": null,
                  "_source": {
                    "eventTime": "2015-02-01T00:00:26Z"
                  },
                  "sort": [
                    1422748826000
                  ]
                }
              ]
            }
          }
        },
        {
          "key_as_string": "2015-02-02T00:00:00.000Z",
          "key": 1422835200000,
          "doc_count": 8182,
          "top_for_day": {
            "hits": {
              "total": 8182,
              "max_score": null,
              "hits": [
                {
                  "_index": "events-2015-02",
                  "_type": "event",
                  "_id": "c544278d-9f51-41a8-827b-9c70c0a057ca",
                  "_score": null,
                  "_source": {
                    "timestamp": "2015-02-02T00:00:19Z"
                  },
                  "sort": [
                    1422835219000
                  ]
                }
              ]
            }
          }
        }
      ]
    }
  }
}
相关问题