# RangeQuery for Date

**URL:** <https://discuss.elastic.co/t/rangequery-for-date/113001>\
**Category:** Elasticsearch\
**Created:** [December 22, 2017, 12:55pm UTC](https://discuss.elastic.co/t/rangequery-for-date/113001 "2017-12-22T12:55:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hym](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hym/32/22399_2.png) [@hym](https://discuss.elastic.co/u/hym)\
**Post date:** [December 22, 2017, 12:55pm UTC](https://discuss.elastic.co/t/rangequery-for-date/113001/1 "2017-12-22T12:55:09Z")

</div>

Hi,  
I am trying to send date range query to the ELS from java app and I create my query using Java API. like as follow:  
QueryBuilders.rangeQuery("dateField").from(new Date()).to(new Date())  
Just for a test.  
But I get an exception due to the NumberFormat, although, from() method accepts Object as the input but it processes just Long.  
On the other hand, No way to use [Date Math](https://www.elastic.co/guide/en/elasticsearch/reference/5.5/common-options.html#date-math) in Java API

Any Idea?  
Thanks in advance

---

<div class="post-metadata">

**Author:** ![luiz.santos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/luiz.santos/32/24664_2.png) [@luiz.santos](https://discuss.elastic.co/u/luiz.santos)\
**Post date:** [December 26, 2017, 5:39pm UTC](https://discuss.elastic.co/t/rangequery-for-date/113001/2 "2017-12-26T17:39:28Z")

</div>

Hi @hym,

I used this code and worked as expected:

```
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;
import java.util.Date;

public class Rest {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("localhost", 9200, "http")));

        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(QueryBuilders.rangeQuery("timestamp").from(new Date()).to(new Date()));

        SearchRequest searchRequest = new SearchRequest();
        searchRequest.source(sourceBuilder);

        SearchResponse searchResponse = client.search(searchRequest);
        System.out.println(searchResponse.getHits().totalHits);

        client.close();
    }
} 

```

Can you share more details of what is happening?

Cheers,  
LG

---

<div class="post-metadata">

**Author:** ![hym](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hym/32/22399_2.png) [@hym](https://discuss.elastic.co/u/hym)\
**Post date:** [January 2, 2018, 1:02pm UTC](https://discuss.elastic.co/t/rangequery-for-date/113001/3 "2018-01-02T13:02:57Z")

</div>

Hi,  
Thanks for the reply, I use this code exactly in my project and I just noticed it works when we try to connect real running ELS server. But I write test using ELS test framework which uses some embedded stuff to mock the ELS server. In this case, I got the Error. Please find below the error and the generated Query for the timeRange part:

```
{
            "range" : {
              "@timestamp" : {
                "from" : "now",
                "to" : 1514613600000,
                "include_lower" : true,
                "include_upper" : true,
                "boost" : 1.0
              }
            }
          },

```

the error:

```
nested: NumberFormatException[For input string: "now"];
at org.elasticsearch.index.query.QueryShardContext.toQuery(QueryShardContext.java:329)
at org.elasticsearch.index.query.QueryShardContext.toQuery(QueryShardContext.java:312)
at org.elasticsearch.search.SearchService.parseSource(SearchService.java:617)
at org.elasticsearch.search.SearchService.createContext(SearchService.java:485)
at org.elasticsearch.search.SearchService.createAndPutContext(SearchService.java:461)
at org.elasticsearch.search.SearchService.executeQueryPhase(SearchService.java:257)
at org.elasticsearch.action.search.SearchTransportService$6.messageReceived(SearchTransportService.java:340)
at org.elasticsearch.action.search.SearchTransportService$6.messageReceived(SearchTransportService.java:337)
at org.elasticsearch.transport.RequestHandlerRegistry.processMessageReceived(RequestHandlerRegistry.java:69)
at org.elasticsearch.transport.TransportService$7.doRun(TransportService.java:644)
at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:638)
at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37)

```

By the way, I am using version 6.6.0 of lucene-test-framework

Thanks in advance

---

<div class="post-metadata">

**Author:** ![luiz.santos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/luiz.santos/32/24664_2.png) [@luiz.santos](https://discuss.elastic.co/u/luiz.santos)\
**Post date:** [January 2, 2018, 1:28pm UTC](https://discuss.elastic.co/t/rangequery-for-date/113001/4 "2018-01-02T13:28:29Z")

</div>

Hi @hym,

Maybe you can take some hint from elasticsearch tests source code, for example:

> <https://github.com/elastic/elasticsearch/blob/1b660821a21853b98a7a46683ad2e3442dd29dc9/core/src/test/java/org/elasticsearch/search/query/SearchQueryIT.java#L1788>

Cheers,  
LG

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 30, 2018, 1:36pm UTC](https://discuss.elastic.co/t/rangequery-for-date/113001/5 "2018-01-30T13:36:28Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
