# Elasticsearch Java API client 8.7.1, No Option available to generate the correct format for source ordering for composition aggregation

**URL:** https://discuss.elastic.co/t/elasticsearch-java-api-client-8-7-1-no-option-available-to-generate-the-correct-format-for-source-ordering-for-composition-aggregation/337477
**Category:** Elasticsearch
**Created:** [July 3, 2023, 3:06pm UTC](https://discuss.elastic.co/t/elasticsearch-java-api-client-8-7-1-no-option-available-to-generate-the-correct-format-for-source-ordering-for-composition-aggregation/337477 "2023-07-03T15:06:07Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![ramyogi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ramyogi/32/120207_2.png) [@ramyogi](https://discuss.elastic.co/u/ramyogi)
#### Post date: [July 3, 2023, 3:06pm UTC](https://discuss.elastic.co/t/elasticsearch-java-api-client-8-7-1-no-option-available-to-generate-the-correct-format-for-source-ordering-for-composition-aggregation/337477/1 "2023-07-03T15:06:07Z")

</div>

Elasticsearch Java API client 8.7.1 does not have option to supply order for the composite term aggregation but it was available server lib.

Query runs fine in KIBANA but cannot build same in Java client library

```auto
{"from":0,"size":0,"query":{"bool":{"must":[{"query_string":{"query":"test","fields":["field1"]}}],"adjust_pure_negative":true,"boost":1}},"aggregations":{"db":{"composite":{"size":10,"sources":[{"example":{"terms":{"field":"ex.agg","missing_bucket":false,"order":"desc"}}}]}}}}

```

Just look at aggregation.  
Java client Lib code snippet

```
   final Map<String, CompositeAggregationSource> comAggrSrcMap = new HashMap<>();
        CompositeAggregationSource compositeAggregationSource = new CompositeAggregationSource.Builder()
                .terms(termsAggrBuilder ->
                termsAggrBuilder
                        .field("ex.agg"))
                        .missingBucket(false)
        ).build();
        comAggrSrcMap.put("example", compositeAggregationSource);
     CompositeAggregation compAgg = new CompositeAggregation.Builder().size(10).sources(comAggrSrcMap):
              
    }

```

Cannot add "order":"desc" in the termsAggrBuilder. it accepts order only NamedValue  
termsAggrBuilder  
.order(new NamedValue\<\>("\_count", SortOrder.Desc))

But E throws Error.

this was working fine old lib and order bucket come back in high hit count ordered desc.

New Library code throws Error:

```auto
{
  "error": {
    "root_cause": [
      {
        "type": "x_content_parse_exception",
        "reason": "[31:21] [terms] order doesn't support values of type: START_ARRAY"
      }
    ],
    "type": "x_content_parse_exception",
    "reason": "[31:21] [composite] failed to parse field [sources]",
    "caused_by": {
      "type": "x_content_parse_exception",
      "reason": "[31:21] [terms] order doesn't support values of type: START_ARRAY"
    }
  },
  "status": 400
}

```

Looking forward to get some answer.

---

<div class="post-metadata">

### Author: ![ramyogi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ramyogi/32/120207_2.png) [@ramyogi](https://discuss.elastic.co/u/ramyogi)
#### Post date: [July 7, 2023, 2:27pm UTC](https://discuss.elastic.co/t/elasticsearch-java-api-client-8-7-1-no-option-available-to-generate-the-correct-format-for-source-ordering-for-composition-aggregation/337477/2 "2023-07-07T14:27:28Z")

</div>

Any help from anyone is greatly appreciated.

> **[Composite aggregation | Elasticsearch Guide \[8.8\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html#_order)**

As per the documentation from ES order can be passed for Sources Term aggregation of composite facet but Java Client API does not provides any option to set order , it always expect NamedValue for Order method inside termaggregation builder.

so it is it hard to understand how to implement using ES Java client ,

> **[TermsAggregation (java-client 8.7.1 API)](https://artifacts.elastic.co/javadoc/co/elastic/clients/elasticsearch-java/8.7.1/co/elastic/clients/elasticsearch/_types/aggregations/TermsAggregation.html#order())**
>
> declaration: package: co.elastic.clients.elasticsearch.\_types.aggregations, class: TermsAggregation

---

<div class="post-metadata">

### Author: ![ramyogi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ramyogi/32/120207_2.png) [@ramyogi](https://discuss.elastic.co/u/ramyogi)
#### Post date: [July 10, 2023, 12:34pm UTC](https://discuss.elastic.co/t/elasticsearch-java-api-client-8-7-1-no-option-available-to-generate-the-correct-format-for-source-ordering-for-composition-aggregation/337477/3 "2023-07-10T12:34:31Z")

</div>

Any alternative to form the same query feature in different way through Java API ?

---

<div class="post-metadata">

### Author: ![yingqing\_zzz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yingqing_zzz/32/123887_2.png) [@yingqing\_zzz](https://discuss.elastic.co/u/yingqing_zzz)
#### Post date: [July 26, 2023, 2:30am UTC](https://discuss.elastic.co/t/elasticsearch-java-api-client-8-7-1-no-option-available-to-generate-the-correct-format-for-source-ordering-for-composition-aggregation/337477/4 "2023-07-26T02:30:29Z")

</div>

same problem here.

---

<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: [July 26, 2023, 1:14pm UTC](https://discuss.elastic.co/t/elasticsearch-java-api-client-8-7-1-no-option-available-to-generate-the-correct-format-for-source-ordering-for-composition-aggregation/337477/5 "2023-07-26T13:14:41Z")

</div>

I think you can do something like:

```
.order(NamedValue.of("ex.agg", SortOrder.Desc))

```

Does this work?

---

<div class="post-metadata">

### Author: ![ramyogi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ramyogi/32/120207_2.png) [@ramyogi](https://discuss.elastic.co/u/ramyogi)
#### Post date: [July 26, 2023, 2:14pm UTC](https://discuss.elastic.co/t/elasticsearch-java-api-client-8-7-1-no-option-available-to-generate-the-correct-format-for-source-ordering-for-composition-aggregation/337477/6 "2023-07-26T14:14:02Z")

</div>

No , it does not work.  
{"type":"x\_content\_parse\_exception","reason":"[1:153] [terms] order doesn't support values of type: START\_ARRAY"}]

---

<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: [July 26, 2023, 2:29pm UTC](https://discuss.elastic.co/t/elasticsearch-java-api-client-8-7-1-no-option-available-to-generate-the-correct-format-for-source-ordering-for-composition-aggregation/337477/7 "2023-07-26T14:29:35Z")

</div>

Could you:

- Upgrade the client to 8.9.0
- If it fails again, share a piece of code like a test (see [this as an example](https://github.com/dadoonet/elasticsearch-java-client-demo/blob/main/src/test/java/fr/pilato/test/elasticsearch/hlclient/EsClientIT.java#L305-L319)) which can help to provide a better answer

---

<div class="post-metadata">

### Author: ![ramyogi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ramyogi/32/120207_2.png) [@ramyogi](https://discuss.elastic.co/u/ramyogi)
#### Post date: [July 26, 2023, 3:21pm UTC](https://discuss.elastic.co/t/elasticsearch-java-api-client-8-7-1-no-option-available-to-generate-the-correct-format-for-source-ordering-for-composition-aggregation/337477/8 "2023-07-26T15:21:28Z")

</div>

Could not find the 8.9.0 version in Maven repo

```auto
<dependency>
    <groupId>co.elastic.clients</groupId>
    <artifactId>elasticsearch-java</artifactId>
    <version>8.8.2</version>
</dependency>

```

the most higher version is 8.8.2  
. Can you please suggest how to test 8.9.0 version for the change.

---

<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: [July 26, 2023, 3:51pm UTC](https://discuss.elastic.co/t/elasticsearch-java-api-client-8-7-1-no-option-available-to-generate-the-correct-format-for-source-ordering-for-composition-aggregation/337477/9 "2023-07-26T15:51:43Z")

</div>

It should have been published. I'll check that later. In the meantime, please use 8.8.2.

---

<div class="post-metadata">

### Author: ![ramyogi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ramyogi/32/120207_2.png) [@ramyogi](https://discuss.elastic.co/u/ramyogi)
#### Post date: [July 26, 2023, 9:00pm UTC](https://discuss.elastic.co/t/elasticsearch-java-api-client-8-7-1-no-option-available-to-generate-the-correct-format-for-source-ordering-for-composition-aggregation/337477/10 "2023-07-26T21:00:54Z")

</div>

It did not work the version 8.8.2 as well .  
I have noticed some below reference ,

> <https://github.com/elastic/elasticsearch-specification/pull/2151>
>
> This fixes composite aggregation which members are not standard aggregations.
> 
> …
> Documentation:
> https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html#search-aggregations-bucket-composite-aggregation
> 
> Source for ref:
> https://github.com/elastic/elasticsearch/tree/main/server/src/main/java/org/elasticsearch/search/aggregations/bucket/composite
> 
> Found in https://github.com/elastic/elasticsearch-java/issues/534

is that one needed to have the fix ?

---

<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: [July 27, 2023, 10:31am UTC](https://discuss.elastic.co/t/elasticsearch-java-api-client-8-7-1-no-option-available-to-generate-the-correct-format-for-source-ordering-for-composition-aggregation/337477/11 "2023-07-27T10:31:55Z")

</div>

Probably yes. I'd wait for @swallez to comment on that.

---

<div class="post-metadata">

### Author: ![ramyogi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ramyogi/32/120207_2.png) [@ramyogi](https://discuss.elastic.co/u/ramyogi)
#### Post date: [July 31, 2023, 3:13pm UTC](https://discuss.elastic.co/t/elasticsearch-java-api-client-8-7-1-no-option-available-to-generate-the-correct-format-for-source-ordering-for-composition-aggregation/337477/12 "2023-07-31T15:13:42Z")

</div>

I have tried with latest 8.9.0

```auto
      <groupId>co.elastic.clients</groupId>
      <artifactId>elasticsearch-java</artifactId>
      <version>8.9.0</version>
    </dependency>

```

Above problem still exists.

---

<div class="post-metadata">

### Author: ![ramyogi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ramyogi/32/120207_2.png) [@ramyogi](https://discuss.elastic.co/u/ramyogi)
#### Post date: [August 8, 2023, 8:39pm UTC](https://discuss.elastic.co/t/elasticsearch-java-api-client-8-7-1-no-option-available-to-generate-the-correct-format-for-source-ordering-for-composition-aggregation/337477/13 "2023-08-08T20:39:45Z")

</div>

Any suggestion. As per ES documentation example we cannot form query through Java Client API.

> **[Composite aggregation | Elasticsearch Guide \[8.9\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html#_order)**

Which seems hard to find how to solve this Java client API.

---

<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: [September 5, 2023, 8:40pm UTC](https://discuss.elastic.co/t/elasticsearch-java-api-client-8-7-1-no-option-available-to-generate-the-correct-format-for-source-ordering-for-composition-aggregation/337477/14 "2023-09-05T20:40:01Z")

</div>

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