pferrel
pferrel

Reputation: 5702

Elasticsearch daterange using two date fields

I send a query from the client for docs where the currentDate is between two values attached to all docs. The docs have an expireDate and an availableDate.

How do I formulate this query? As a guess I came up with the following, which doesn't work:

{
  "constant_score": {
    "filter": {
      "range" : {
        "expiredate" : {
          "lte": "2015-08-15T11:28:45.114-07:00"
        },
        "availabledate" : {
          "gte": "2015-08-11T11:28:45.114-07:00"
        }
      }
    }
  }
}

This even looks wrong because afaik the range won't work on a compound object or an array of one-sided ranges. I guess I could add an entire new filter so one for each field. Is there a way to use one date range for two fields?

Upvotes: 1

Views: 841

Answers (1)

Val
Val

Reputation: 217514

The correct way of expressing that query is using two range filters (one for each date field) and place them in another bool/must filter.

{
  "query": {
    "constant_score": {
      "filter": {
        "bool": {
          "must": [
            {
              "range": {
                "expireDate": {
                  "lte": "2015-08-15T11:28:45.114-07:00"
                }
              }
            },
            {
              "range": {
                "availabledate": {
                  "gte": "2015-08-11T11:28:45.114-07:00"
                }
              }
            }
          ]
        }
      }
    }
  }
}

Upvotes: 1

Related Questions