RangeQuery for Date

(Hossein Yeganeh Markid) #1

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 in Java API

Any Idea?
Thanks in advance

(Luiz Santos) #2

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(
                        new HttpHost("localhost", 9200, "http")));

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

        SearchRequest searchRequest = new SearchRequest();

        SearchResponse searchResponse = client.search(searchRequest);


Can you share more details of what is happening?


(Hossein Yeganeh Markid) #3

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

(Luiz Santos) #4

Hi @hym,

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


(system) #5

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