ElasticSearch: How to query a date field using an hours-range filter

后端 未结 3 983
庸人自扰
庸人自扰 2020-12-10 12:04

Currently, I already know how to filter a days range from a (timestamp) date field. That\'s an easy one:

\"range\": {
    \"date\": {
        \"gte\": \"2015         


        
3条回答
  •  猫巷女王i
    2020-12-10 12:46

    Here's what I once used to only get results from start of current day to 6pm:

    {
      "query": {
        "bool": {
          "must": [
            {
              "query_string": {
                "query": "(log_message:\"My Search String\")"
              }
            },
            {
              "range": {
                "@timestamp": {
                  "time_zone": "CET",
                  "gt": "now-24h/d",
                  "lte": "now-24h/d+18h"
                }
              }
            }
          ]
        }
      }
    }
    

    the important part is "now-24h/d" which will round to midnight / begin of current day, although it is a bit tricky as it depends on whether you use gt/lt(e), see reference doc for details.

提交回复
热议问题