# How to sort by a sub-aggregation field in multi\_terms aggregation using opensearch-java 3.1.0?

**URL:** <https://forum.opensearch.org/t/how-to-sort-by-a-sub-aggregation-field-in-multi-terms-aggregation-using-opensearch-java-3-1-0/27984>\
**Category:** OpenSearch Client Libraries\
**Tags:** opensearch-java, clients-general\
**Created:** [April 13, 2026, 11:07pm UTC](https://forum.opensearch.org/t/how-to-sort-by-a-sub-aggregation-field-in-multi-terms-aggregation-using-opensearch-java-3-1-0/27984 "2026-04-13T23:07:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![UmangKamdar](https://avatars.discourse-cdn.com/v4/letter/u/ad7895/32.png) [@UmangKamdar](https://forum.opensearch.org/u/UmangKamdar)\
**Post date:** [April 13, 2026, 11:07pm UTC](https://forum.opensearch.org/t/how-to-sort-by-a-sub-aggregation-field-in-multi-terms-aggregation-using-opensearch-java-3-1-0/27984/1 "2026-04-13T23:07:26Z")

</div>

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

```json
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` → ❌ 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:**

```groovy
implementation 'org.opensearch.client:opensearch-java:3.1.0'

```

* * *

---

<div class="post-metadata">

**Author:** ![reta](https://avatars.discourse-cdn.com/v4/letter/r/cdc98d/32.png) [@reta](https://forum.opensearch.org/u/reta)\
**Post date:** [April 14, 2026, 12:43am UTC](https://forum.opensearch.org/t/how-to-sort-by-a-sub-aggregation-field-in-multi-terms-aggregation-using-opensearch-java-3-1-0/27984/2 "2026-04-14T00:43:54Z")

</div>

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](https://github.com/opensearch-project/opensearch-java/issues/1964)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/mauve_hedgehog/original/2X/3/33547ea01a5b12dcca2958411d3edd97ae2ea8c1.png) [@system](https://forum.opensearch.org/u/system)\
**Post date:** [June 13, 2026, 12:44am UTC](https://forum.opensearch.org/t/how-to-sort-by-a-sub-aggregation-field-in-multi-terms-aggregation-using-opensearch-java-3-1-0/27984/3 "2026-06-13T00:44:15Z")

</div>

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