How to sort by a sub-aggregation field in multi_terms aggregation using opensearch-java 3.1.0?

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 found
  • MultiTermsOrder:cross_mark: 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_terms fully 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'

Hi @UmangKamdar ,

It seems like the MultiTermsAggregation does not expose the right APIs for ordering (the one it has is based on HistogramOrder which does not seem right). I see you already opened an issue there [1], it should be enough.

[1] [BUG] How to sort by a sub-aggregation field in multi_terms aggregation using opensearch-java 3.1.0? · Issue #1964 · opensearch-project/opensearch-java · GitHub