Versions (relevant - OpenSearch/Dashboard/Server OS/Browser):
- org.opensearch.client:opensearch-java:3.1.0
- OpenSearch cluster version: 3.1
Describe the issue:
I’m trying to sort buckets by a metric sub-aggregation inside a multi_terms aggregation using the Java client, but I can’t figure out the correct .order() method signature on MultiTermsAggregation.Builder.
In the REST API this works perfectly:
GET /orders/_search
{
"size": 0,
"aggs": {
"breakdown": {
"multi_terms": {
"terms": [
{ "field": "customer_name" },
{ "field": "customer_id" }
],
"size": 1000,
"order": {
"active_orders": "desc"
}
},
"aggs": {
"inactive_orders": {
"filter": {
"bool": {
"must_not": {
"terms": { "status": ["SHIPPED", "DELIVERED"] }
}
}
}
},
"active_orders": {
"filter": {
"terms": { "status": ["DELIVERED"] }
},
"aggs": {
"price_ranges": {
"range": {
"field": "order_value",
"ranges": [
{ "key": "Low", "from": 0, "to": 100 },
{ "key": "Medium", "from": 100, "to": 500 },
{ "key": "High", "from": 500, "to": 9999 }
]
}
}
}
}
}
}
}
}
The order: { "active_orders": "desc" } sorts buckets by the doc count of the active_orders filter sub-aggregation. This works fine via REST.
But I can’t find the correct way to express the order part in the Java client. Things I’ve tried that don’t compile or don’t work:
- I assume it uses HistogramOrder class internally, but I don’t think it accepts these named parameters
.order(Map.of("active_orders", SortOrder.Desc))— method not foundMultiTermsOrder→
I tired with this class, but it does not exist in 3.1.0
Question:
What is the correct way to implement multi_terms aggregation ordering (based on sub-aggregation metrics using the OpenSearch Java client (3.1.0)?
Specifically:
- What is the correct Java DSL type expected for
.multiTerms().order()? - Is sub-aggregation ordering for
multi_termsfully supported in the typed Java client? - If not, is there an alternative recommended approach (without using raw JSON)?
Configuration:
implementation 'org.opensearch.client:opensearch-java:3.1.0'