13

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"
            }
          }
        }
      ]
    }
  }
}

1

4 Answers 4

15

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"
            }
          }
        }
      ]
    }
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

@VaI Thanks for answer the question, base on your suggestion I find the way to query it !
2

This is the query that worked for me to select dates after based on a specific field:

{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "_kuzzle_info.createdAt": {
              "gte": "2023-01-09T13:29:27.537+00:00"
            }
          }
        }
      ]
    }
  }
}

_kuzzle_info.createdAt is the field with the date

Comments

0

there is a syntax error in your answer, the elastic fields should come before the gte or lte

{
   "range": {
         "**startdate**": {
              "**lte**": "2019-08-22T03:55:47.165Z"
          }
      }
}

Comments

0

If nested fields, then below query will work. Make sure the schedule data type is 'date'

"query": {
    "bool": {
      "filter": {
        "nested": {
          "path": "trans",
          "query": {
            "range": {
              "trans.schedule": {
                "lte": "2023-03-23T00:35:45"
              }
            }
          }
        }
      }
    }
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.