date - 使用elasticsearch中的日期直方图获取特定日期范围内的插入文档计数
问题描述
我在 elasticsearch 中列出了包含各种文件的文档。文件如下所示。
{
"role": "api_user",
"apikey": "key1"
"data":{},
"@timestamp": "2021-10-06T16:47:13.555Z"
},
{
"role": "api_user",
"apikey": "key1"
"data":{},
"@timestamp": "2021-10-06T18:00:00.555Z"
},
{
"role": "api_user",
"apikey": "key1"
"data":{},
"@timestamp": "2021-10-07T13:47:13.555Z"
}
]
我想以 1 天的间隔查找特定日期范围内存在的文档数量,比方说
2021-10-05T00:47:13.555Z to 2021-10-08T00:13:13.555Z
我正在尝试以下聚合结果。
{
"size": 0,
"query": {
"filter": {
"bool": {
"must": [
{
"range": {
"@timestamp": {
"gte": "2021-10-05T00:47:13.555Z",
"lte": "2021-10-08T00:13:13.555Z",
"format": "strict_date_optional_time"
}
}
}
]
}
}
},
"aggs": {
"data": {
"date_histogram": {
"field": "@timestamp",
"calendar_interval": "day"
}
}
}
}
预期的输出应该是:- 因为2021-10-06
我应该得到 2 个文档,2021-10-07
我应该得到 1 个文档,如果文档不存在,我应该得到计数为 0。
解决方案
以下解决方案有效
{
"size":0,
"query":{
"bool":{
"must":[
],
"filter":[
{
"match_all":{
}
},
{
"range":{
"@timestamp":{
"gte":"2021-10-05T00:47:13.555Z",
"lte":"2021-10-08T00:13:13.555Z",
"format":"strict_date_optional_time"
}
}
}
],
"should":[
],
"must_not":[
]
}
},
"aggs":{
"data":{
"date_histogram":{
"field":"@timestamp",
"fixed_interval":"12h",
"time_zone":"Asia/Calcutta",
"min_doc_count":1
}
}
}
}
推荐阅读
- amazon-web-services - 使用 Terraform 使用 AWS Systems Manager Automation 进行集中式多账户修补
- java - 在 Clojure 中初始化 Java File 对象总是给出 Windows 中的相对路径。我如何给它绝对路径?
- javascript - JavaScript forEach 循环特定项目
- unit-testing - xUnit-Test 中的模拟异步方法总是返回 null
- python - 网络抓取从 olx 获取二手车价格
- authentication - 使用链接的 Swagger OAS 3.0.0 不会填充下一个 operationId 的参数
- python - 随机森林回归器,试图将树木文本输出
- javascript - 在对象内调用js对象方法
- html - 如何在另一个div下重叠div?
- java - 如何使用 Java Hipster 构建 vaadin 14 项目?