【问题标题】:Get inserted document counts in specific date range using date histogram in elasticsearch使用elasticsearch中的日期直方图获取特定日期范围内的插入文档计数
【发布时间】:2021-11-29 21:17:26
【问题描述】:

我在 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。

【问题讨论】:

    标签: date elasticsearch search date-histogram


    【解决方案1】:

    以下解决方案有效

    {
       "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
             }
          }
       }
    }
    

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2017-01-12
    • 2017-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-05
    • 1970-01-01
    • 2019-02-23
    • 2022-12-18
    相关资源
    最近更新 更多