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

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

{"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:

{
  "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.

Any help from anyone is greatly appreciated.

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 ,

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

same problem here.

I think you can do something like:

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

Does this work?

No , it does not work.
{"type":"x_content_parse_exception","reason":"[1:153] [terms] order doesn't support values of type: START_ARRAY"}]

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) which can help to provide a better answer

Could not find the 8.9.0 version in Maven repo

<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.

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

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

is that one needed to have the fix ?

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

I have tried with latest 8.9.0

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

Above problem still exists.

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

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

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