# Terms aggregation with order with Java High Level Rest Client fails

**URL:** https://discuss.elastic.co/t/terms-aggregation-with-order-with-java-high-level-rest-client-fails/183452
**Category:** Elasticsearch
**Created:** [May 30, 2019, 3:32am UTC](https://discuss.elastic.co/t/terms-aggregation-with-order-with-java-high-level-rest-client-fails/183452 "2019-05-30T03:32:08Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Isarg](https://avatars.discourse-cdn.com/v4/letter/i/f475e1/32.png) [@Isarg](https://discuss.elastic.co/u/Isarg)
#### Post date: [May 30, 2019, 3:32am UTC](https://discuss.elastic.co/t/terms-aggregation-with-order-with-java-high-level-rest-client-fails/183452/1 "2019-05-30T03:32:08Z")

</div>

The elasticSearch version is 5.6.4

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

```json
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**

> ```auto
> 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

> ```auto
> 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?

---

<div class="post-metadata">

### Author: ![Isarg](https://avatars.discourse-cdn.com/v4/letter/i/f475e1/32.png) [@Isarg](https://discuss.elastic.co/u/Isarg)
#### Post date: [May 31, 2019, 2:10am UTC](https://discuss.elastic.co/t/terms-aggregation-with-order-with-java-high-level-rest-client-fails/183452/2 "2019-05-31T02:10:35Z")

</div>

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

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [May 31, 2019, 2:22am UTC](https://discuss.elastic.co/t/terms-aggregation-with-order-with-java-high-level-rest-client-fails/183452/3 "2019-05-31T02:22:07Z")

</div>

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`:

```auto
<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](https://docs.spring.io/spring-boot/docs/current/reference/html/howto-build.html#howto-customize-dependency-versions)

---

<div class="post-metadata">

### Author: ![Isarg](https://avatars.discourse-cdn.com/v4/letter/i/f475e1/32.png) [@Isarg](https://discuss.elastic.co/u/Isarg)
#### Post date: [May 31, 2019, 3:24am UTC](https://discuss.elastic.co/t/terms-aggregation-with-order-with-java-high-level-rest-client-fails/183452/4 "2019-05-31T03:24:01Z")

</div>

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

---

<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: [June 28, 2019, 3:24am UTC](https://discuss.elastic.co/t/terms-aggregation-with-order-with-java-high-level-rest-client-fails/183452/5 "2019-06-28T03:24:02Z")

</div>

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