Search code examples
elasticsearch

Elasticsearch query greater than date


I create an index and it save two date (D1, D2) for something, after that I want to query this monent is it between D1 and D2, so I query :

startdate <= "2019-08-22T03:55:47.165Z" AND endate>= "2019-08-22T03:55:47.165Z"

It successful return the data as I want, so I try to do the same things in Dev tools again with:

GET i_want_get_date/_search
{
  "query":{
    "query_string":{
      "query":"startdate<= \"2019-08-22T03:55:47.165Z\" AND enddate>= \"2019-08-22T03:55:47.165Z\""
    }
  }
}

The problem is:

it cannot search any result back, the query cannot compare the date using this method

Any suggestion? Thanks!

============================================================

Thanks Val answer the question, I using a very slimier query to do the same function, I change a little bit query syntax to make it works:

GET i_want_get_date/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "startdate":{
              "lte":"2019-08-22T03:55:47.165Z"
            }
          }
        },
        {
          "range": {
            "enddate":{
              "gte":"2019-08-22T03:55:47.165Z"
            }
          }
        }
      ]
    }
  }
}


Solution

  • I suggest to use two range queries instead

    GET i_want_get_date/_search
    {
      "query": {
        "bool": {
          "filter": [
            {
              "range": {
                "startdate": {
                  "gte": "2019-08-21T03:55:47.165Z"
                }
              }
            },
            {
              "range": {
                "enddate": {
                  "lt": "2019-08-22T03:55:47.165Z"
                }
              }
            }
          ]
        }
      }
    }