首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在elasticsearch中使用日期直方图获取特定日期范围内的插入文档数

在elasticsearch中使用日期直方图获取特定日期范围内的插入文档数
EN

Stack Overflow用户
提问于 2021-10-11 03:50:43
回答 1查看 33关注 0票数 0

我在elasticsearch中列出了包含各种文件的文档。文档如下所示。

代码语言:javascript
运行
复制
    {
        "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

我正在尝试下面的聚合结果。

代码语言:javascript
运行
复制
{
    "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。

EN

回答 1

Stack Overflow用户

发布于 2021-10-11 07:01:21

下面的解决方案有效

代码语言:javascript
运行
复制
{
   "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
         }
      }
   }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69520831

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档