Date Range Query Java

I'm trying to create a range query in ES.
Here's my Java example:
QueryBuilder qb = QueryBuilders.rangeQUery("updateTime")
.from(fromDate)
.to(toDate)

WHen I print it out, it looks like this:
{
"range" : {
"updateTime" : {
"from" : "2016-03-18-T15:00:00.000Z",
"to" : "2016-03-28-T15:00:00.000Z",
"include_lower" : true,
"include_upper" : true
}
}
}
I get all shards failed error.
Then I get a Null Pointer when trying to access the response.
I know there is data for those dates..

Full stacktrace?

Looks like I'm getting a NumberFormatException fo{for input string: "2016-03-18T15:00:00.000Z"
at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.onFirstPhaseResult(TransportSearchTypeAction.java:228)
org.elasticsearch.action.seasrch.type.TransportSearchTypeAction$BaseAsyncAction$1.onFailure(TransportSearchTypeAction.java:174)
at org.elasticsearch.action.ActionListenerResponseHandler.handleException(ActionListenerResponseHandlerjava:46)

I can't copy the whole error because I'm working on a unconnected system.

But you said Null Pointer, right?

This is not the same error here.

For what is worth, the string you passed is definitely not a number.

Yes, I was wrong on the error. What value can I use for a range query .from and .to if I want to do a range query using dates ?

If you reproduce with a full script your error, then we can probably help you.

Here I have no idea about the mapping, the data...

I suppose that updateTime is not a date???

But unsure

updateTime is a date. I tried to setup the range query using:
QueryBuilder qb = QueryBuilders.rangeQuery("updateTime"
.gte("now - 2d/d")
.lte("now/d")

But I still get a NumberFormatException
How can I use "now" as a date format ?
What format should I use in the RangeQuery ?

1 Like

Works well on my end:

DELETE test
PUT test/doc/1
{
  "updateTime": "2016-03-18T15:00:00"
}
GET test/_search
{
  "query": {
    "range": {
      "updateTime": {
        "gte": "2016-03-18T15:00:00.000Z",
        "lte": "2017-01-01T00:00:00.000Z"
      }
    }
  }
}

Gives:

{
  "took": 8,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "test",
        "_type": "doc",
        "_id": "1",
        "_score": 1,
        "_source": {
          "updateTime": "2016-03-18T15:00:00"
        }
      }
    ]
  }
}

I'm doing this in Java. How do I setup the rangeQuery using dates in java ?

As you did. It looks correct to me (but the spaces).

I told you already. I think your mapping is incorrect.