# Multi terms aggregation java client

**URL:** https://forum.opensearch.org/t/multi-terms-aggregation-java-client/12349
**Category:** OpenSearch
**Tags:** all-clients, troubleshoot, feature-request
**Created:** [February 1, 2023, 4:37pm UTC](https://forum.opensearch.org/t/multi-terms-aggregation-java-client/12349 "2023-02-01T16:37:41Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![mcwiise](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@mcwiise](https://forum.opensearch.org/u/mcwiise)
#### Post date: [February 1, 2023, 4:37pm UTC](https://forum.opensearch.org/t/multi-terms-aggregation-java-client/12349/1 "2023-02-01T16:37:41Z")

</div>

**Versions** (relevant - OpenSearch/Dashboard/Server OS/Browser):  
Using gradle

dependencies {  
implementation ‘org.opensearch.client:opensearch-rest-client: 2.5.0’  
implementation ‘org.opensearch.client:opensearch-java:2.0.0’  
}

**Describe the issue** :  
According to the opensearch docs whenever we need to perform an aggregated query on multiple fields, it is just a matter to use `multi_terms` aggregation like this:

```auto
GET my_index/_search
{
  "query": {
    "bool" : {
      "must" : [
        {"match" : {"my_field" : {"query" : "my value"}}}
      ]
    }
  },
  "size": 0, 
  "aggs": {
    "my_aggregate": {
      "multi_terms": {
        "size": 500,
        "terms": [
          {
            "field": "second_field"
          },
          {
            "field": "other_field" 
          }
        ]
      }   
    }
  }
}

```

where we can see how easy is to specify the number of buckets we want to retrieve by setting `size=500`. The above aggregation can be translated to java like this:

```java
new Aggregation.Builder()
  .multiTerms(t -> t.terms(List.of(
      MultiTermLookup.of(mt -> mt.field("second_field")),
      MultiTermLookup.of(mt -> mt.field("other_field"))
  ))))
  .build();

```

But it seems that the java client does not allow to specify the number of buckets to retrieve, then an aggregated query with `multi_terms` always returns 10 buckets by default.

Is there any plans to allow `multi_terms` in java client to set the `size` parameter?

---

<div class="post-metadata">

### Author: ![kris](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.opensearch.org/kris/32/2208_2.png) [@kris](https://forum.opensearch.org/u/kris)
#### Post date: [February 24, 2023, 1:10am UTC](https://forum.opensearch.org/t/multi-terms-aggregation-java-client/12349/2 "2023-02-24T01:10:49Z")

</div>

Hello @mcwiise - per this document, I think you should be able to

> **[Bucket aggregations](https://opensearch.org/docs/latest/opensearch/bucket-agg/)**
>
> Bucket aggregations

---

<div class="post-metadata">

### Author: ![Wilfredo](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.opensearch.org/wilfredo/32/6700_2.png) [@Wilfredo](https://forum.opensearch.org/u/Wilfredo)
#### Post date: [February 16, 2024, 3:36pm UTC](https://forum.opensearch.org/t/multi-terms-aggregation-java-client/12349/4 "2024-02-16T15:36:47Z")

</div>

I had the same problem but in the latest versión it has been **solved**.  
**Code:**

```auto
        MultiTermLookup rt1 = MultiTermLookup.of(f -> f.field("decision.idtipodecision"));
        MultiTermLookup rt2 = MultiTermLookup.of(f -> f.field("decision.resumendecision"));

        List<MultiTermLookup> multiTermLookupList = new ArrayList<>();
        multiTermLookupList.add(rt1);
        multiTermLookupList.add(rt2);
		
		MultiTermsAggregation multi = new MultiTermsAggregation.Builder().terms(multiTermLookupList).size(50).build();
        Aggregation aggDecision = Aggregation.of(t -> t.multiTerms(multi));
        requestBuilder.aggregations(Map.of("decision", aggDecision));

```

Maven version:

```auto
<dependency>
  <groupId>org.opensearch.client</groupId>
  <artifactId>opensearch-java</artifactId>
  <version>2.8.1</version>
</dependency>

```

I hope someone finds it useful.
