Home > Mobile >  Ranged query with Java Client API for Elasticseach 8.0
Ranged query with Java Client API for Elasticseach 8.0

Time:03-17

I found some topics about ranged queries and Elasticsearch but these use the deprecated High Level REST Client interface. I also found a discussion on the elasticseach page which uses the new interface but for version 7.15.2. The problem here is, that the query is built by a json-formatted string because the range query interface was not fully supported at this version.

Unfortunately, I can't find something helpful in the documentation, when it comes to the new Java API Client interface with version 8.0 or above.

I tried the following piece of code to get messages in multiple indices (my-index-*). I set the date and time format corresponding to the format used in the database. Next, I set the time boundaries with from() and to() and there exist data for the given period of time.

// ...
var client   = new ElasticsearchClient(...);
var response = client.search(
    s -> s.index("my-index-*")
          .query(q -> q.range(
              r -> r.field("recordtime")
                    .format("yyyy/MM/dd HH:mm:ss")
                    .from("2020/01/01 00:00:00")
                    .to("2020/01/01 00:30:00"))),
    JsonNode.class);

When I execute the code above I get the following exception:

Exception in thread "main" co.elastic.clients.elasticsearch._types.ElasticsearchException: [es/search] failed: [search_phase_execution_exception] all shards failed
    at co.elastic.clients.transport.rest_client.RestClientTransport.getHighLevelResponse(RestClientTransport.java:281)
    at co.elastic.clients.transport.rest_client.RestClientTransport.performRequest(RestClientTransport.java:147)
    at co.elastic.clients.elasticsearch.ElasticsearchClient.search(ElasticsearchClient.java:1526)
    at co.elastic.clients.elasticsearch.ElasticsearchClient.search(ElasticsearchClient.java:1543)
    at // the function including the line of code above.

These are my dependencies of my build.gradle corresponding to elasticsearch.

dependencies {
    // ...
    implementation group: 'co.elastic.clients',         name: 'elasticsearch-java', version: '8.1.0'
    implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind',   version: '2.13.0'
    // ...
}

So, what did I wrong? I prefer the way to use the builders API and not to create a json-string transform this to a JsonData-object and pass this to the ElasticsearchClient client.


I also tried now to query the search with a curl request and used the following lines:

curl -X GET -k -u elastic:$ESPASS "https://<es-server-url>:9200/my-index-*/_search"
  -H 'Content-Type: application/json' -d'{
  "query": {
    "range": {
      "recordtime": {
        "format": "yyyy/MM/dd HH:mm:ss",
        "from": "2020/01/29 00:00:00",
        "to": "2020/01/29 00:30:00"
      }
    }
  }
}'

Here I get data from the server.

CodePudding user response:

OK, I found the error.

I messed up with the date format. After correcting it I get results with the Java API.

The code of the question above works as intended. But in my real code I used a function to get the format of the timed field. Well, the format returned by the function was dd/MM/yyyy HH:mm:ss but the correct format is (as written in the question) yyyy/MM/dd HH:mm:ss. As you can see I swapped the days and the years.

  • Related