Alexandre Mélard
Alexandre Mélard

Reputation: 12659

elasticsearch date range get yesterday

I'm looking a way to get the data from "yesterday" instead of:

"range" : {
    "price" : {
        "gt" : "2014-11-24",
        "lt" : "2014-11-26"
    }
}

I would like something like:

"range" : {
    "price" : {
        "eq" : "2014-11-25"
    }
}

Does anybody think it could be possible?

I'm thinking of something like:

"range" : {
    "price" : {
        "gt" : "now-2d",
        "lt" : "now"
    }
}

But I would like to get data from 00:00 am to 00:00 pm

Upvotes: 11

Views: 19175

Answers (3)

Andrei Stefan
Andrei Stefan

Reputation: 52368

Try this:

  "query": {
    "range": {
      "price": {
        "gte": "now-1d/d",
        "lt": "now/d"
      }
    }
  }

Upvotes: 17

bosari
bosari

Reputation: 2000

Better to add timezone. For example if you are in CST6CDT then to fetch data from 00:00 to 23:59 use this code -

"query": {
    "range": {
      "price": {
        "gt": "now/d-1d/d",
        "lt": "now/d-1d/d",
        "time_zone":"CST6CDT"
      }
    }
  }

Upvotes: 0

Mnebuerquo
Mnebuerquo

Reputation: 5949

The answer from @andrei-stefan is the way to do this, but I wanted to add a little extra example for other people hitting this question.

If you wanted to get data from yesterday between 12:00pm and 1:00pm rather than the whole day, you could do that with a little more date math:

  "query": {
    "range": {
      "price": {
        "gt": "now-1d/d+12h",
        "lt": "now-1d/d+13h"
      }
    }
  }

https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#date-math

Upvotes: 6

Related Questions