Terms aggregation with order with Java High Level Rest Client fails

The elasticSearch version is 5.6.4

The following terms aggregation query works perfectly fine on my Kibana console

GET zipkin:*/_search
{
  "size": 0,
  "aggs": {
    "group_by_traceid": {
      "terms": {
        "field": "traceId",
        "order": {
          "start_time": "desc"
        }
      },
      "aggs": {
        "start_time": {
          "min": {
            "field": "timestamp"
          }
        }
      }
    }
  }
}

I am trying to perform the similar query using Elasticsearch Java High Level Rest Client 5.6.16

SearchRequest searchRequest = new SearchRequest("zipkin*");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
searchSourceBuilder.size(0);
searchSourceBuilder.aggregation(AggregationBuilders
        .terms("group_by_traceid")
        .field("traceId")
        .order(BucketOrder.aggregation("start_time", false))
        .subAggregation(AggregationBuilders
                .min("start_time")
                .field("timestamp"))
);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest);

Then I get the following error

java.lang.NoSuchMethodError: org.elasticsearch.client.Request.(Ljava/lang/String;Ljava/lang/String;)V
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:317)
at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:396)
at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:382)
at org.elasticsearch.client.RestHighLevelClient.search(RestHighLevelClient.java:323)
at com.isoftstone.mis.framwork.zipkinserver.api.QueryApi.getWithParam(QueryApi.java:49)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.linecorp.armeria.internal.annotation.AnnotatedHttpService.invoke(AnnotatedHttpService.java:254)
at com.linecorp.armeria.internal.annotation.AnnotatedHttpService.lambda$serve0$3(AnnotatedHttpService.java:241)
at java.util.concurrent.CompletableFuture.uniApply(CompletableFuture.java:602)
at java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:577)
at java.util.concurrent.CompletableFuture$Completion.run(CompletableFuture.java:442)
at com.linecorp.armeria.common.AbstractRequestContext.lambda$makeContextAware$1(AbstractRequestContext.java:69)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:748)

After search, I've updated Elasticsearch Java High Level Rest Client to 6.5.4 and modified my code

SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

Then I get a new error

{"error":{"root_cause":[{"type":"aggregation_execution_exception","reason":"Invalid term-aggregator order path [_key]. Unknown aggregation [_key]"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"zipkin:span-2019-05-22","node":"E5jJC_W8SsGVCg6oI2gG2g","reason":{"type":"aggregation_execution_exception","reason":"Invalid term-aggregator order path [_key]. Unknown aggregation [_key]"}}]},"status":500}

It takes a lot of time to find out what the problem is

org.elasticsearch.search.aggregations.InternalOrder.CompoundOrder#CompoundOrder(java.util.List<org.elasticsearch.search.aggregations.BucketOrder>, boolean)

will add a BucketOrder with _key

How can I do it to remove the _key BucketOrder?

1 Like

I just find the problem is that the org.springframework.boot:spring-boot-dependencies:2.1.3 manages org.elasticsearch:elasticsearch, org.elasticsearch.client:elasticsearch-rest-client, org.elasticsearch.client: elasticsearch-rest-high-level-client with the version 6.4.3 in pom.xml

But I just set org.elasticsearch.client: elasticsearch-rest-high-level-client with version 5.6.16 in my pom.xml. So the versions of org.elasticsearch:elasticsearch and org.elasticsearch.client:elasticsearch-rest-client are both 6.4.3

When I set the versions to 5.6.16, the code works OK

Yeah. When using springboot with
elasticsearch, you need to be explicit with some transitive dependencies as SpringBoot declares a version 6.4...

Basically you can put this in your pom.xml:

<properties>
  <elasticsearch.version>7.0.0<elasticsearch.version>
</properties>

See documentation here: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-build.html#howto-customize-dependency-versions

Thanks for replying. I never know this way to customize dependency versions.

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