David
David

Reputation: 143

Elasticsearch: Filter Documents Less than Current Time for Last 7 Days

I'm currently working on a query that filters for records that meet the following criteria:

For example, I would like to return all records that have occurrent between the beginning of the day and 3:33 pm (current time) for each day over the last 7 days. I want the 3:33 pm to be equal to the time that the query is run. So the result set would be documents with a timestamp between 12:00 AM and 3:33 PM for reach day, again with 3:33 PM being set to the current time.

Can anyone help?

I've updated what I have so far. This code works but I would like the params to come from the current time.

{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "Timestamp": {
              "gte": "now-7d"
            }
          }
        },
{
              "script": {"source": "(doc.Timestamp.date.getHourOfDay() <= params['hour'] && doc.Timestamp.date.getMinuteOfHour() <= params['minutes']) || doc.Timestamp.date.getHourOfDay() < params['hour']",
                   "params":{"hour":0, "minutes":51}
                 }
              }
            }
      ]
    }
  }
}

Upvotes: 1

Views: 6732

Answers (1)

Briomkez
Briomkez

Reputation: 577

I would suggest you to give a look to the date math documentation. According to this guide, you can write now/d to get 00:00 AM of today, (now-1d)/d to obtain 00:00 AM of yesterday. In general (now-Xd)/d where X is an integer. Similarly, to obtain the current timestamp rounded to the minute, you can write now/m, (now-1d)/m to obtain the exact timestamp of yesterday rounded to the minute and so on.

If we put all together we can write something like this to obtain the desired result:

{
  "query": {
    "bool": {
      "minimum_should_match": 1,
      "should": [
        {
          "range": {
            "Timestamp": {
              "gte": "now/d",
              "lte": "now/m"
            }
          }
        },
        {
          "range": {
            "Timestamp": {
              "gte": "(now-1d)/d",
              "lte": "(now-1d)/m"
            }
          }
        },
        {
          "range": {
            "Timestamp": {
              "gte": "(now-2d)/d",
              "lte": "(now-2d)/m"
            }
          }
        },
        {
          "range": {
            "Timestamp": {
              "gte": "(now-3d)/d",
              "lte": "(now-3d)/m"
            }
          }
        },
        {
          "range": {
            "Timestamp": {
              "gte": "(now-4d)/d",
              "lte": "(now-4d)/m"
            }
          }
        },
        {
          "range": {
            "Timestamp": {
              "gte": "(now-5d)/d",
              "lte": "(now-5d)/m"
            }
          }
        },
        {
          "range": {
            "Timestamp": {
              "gte": "(now-6d)/d",
              "lte": "(now-6d)/m"
            }
          }
        },
        {
          "range": {
            "Timestamp": {
              "gte": "(now-7d)/d",
              "lte": "(now-7d)/m"
            }
          }
        }
      ]
    }
  }
}

Upvotes: 4

Related Questions