# Java Client for Composite Aggregation using Terms queries

**URL:** <https://discuss.elastic.co/t/java-client-for-composite-aggregation-using-terms-queries/309227>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [July 8, 2022, 7:34pm UTC](https://discuss.elastic.co/t/java-client-for-composite-aggregation-using-terms-queries/309227 "2022-07-08T19:34:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![parashar1505](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/parashar1505/32/108092_2.png) [@parashar1505](https://discuss.elastic.co/u/parashar1505)\
**Post date:** [July 8, 2022, 7:34pm UTC](https://discuss.elastic.co/t/java-client-for-composite-aggregation-using-terms-queries/309227/1 "2022-07-08T19:34:58Z")

</div>

Hi Team,

I have created a composite query for aggregating on 2 different attributes as below -

```auto
{
    "from": 0,
    "size": 0,
    "query": {
        "bool": {
            "must": [
                {
                    "nested": {
                        "query": {
                            "script": {
                                "script": {
                                    "source": "params.territoryIds.contains(doc['territoryHierarchy.id'].value) ",
                                    "lang": "painless",
                                    "params": {
                                        "territoryIds": [
                                            12345678
                                        ]
                                    }
                                },
                                "boost": 1.0
                            }
                        },
                        "path": "territoryHierarchy",
                        "ignore_unmapped": false,
                        "score_mode": "none",
                        "boost": 1.0
                    }
                },
                {
                    "bool": {
                        "should": [
                            {
                                "nested": {
                                    "query": {
                                        "script": {
                                            "script": {
                                                "source": "doc['forecastHeaders.id'].value == params.id && doc['forecastHeaders.revenueCategory'].value == params.revenueCategory ",
                                                "lang": "painless",
                                                "params": {
                                                    "revenueCategory": 0,
                                                    "id": 987654321
                                                }
                                            },
                                            "boost": 1.0
                                        }
                                    },
                                    "path": "forecastHeaders",
                                    "ignore_unmapped": false,
                                    "score_mode": "none",
                                    "boost": 1.0
                                }
                            },
                            {
                                "nested": {
                                    "query": {
                                        "script": {
                                            "script": {
                                                "source": "doc['forecastHeaders.id'].value == params.id && doc['forecastHeaders.revenueCategory'].value == params.revenueCategory ",
                                                "lang": "painless",
                                                "params": {
                                                    "revenueCategory": 0,
                                                    "id": 987654321
                                                }
                                            },
                                            "boost": 1.0
                                        }
                                    },
                                    "path": "forecastHeaders",
                                    "ignore_unmapped": false,
                                    "score_mode": "none",
                                    "boost": 1.0
                                }
                            }
                        ],
                        "adjust_pure_negative": true,
                        "boost": 1.0
                    }
                },
                {
                    "terms": {
                        "revnWinProbability": [
                            40,
                            50
                        ],
                        "boost": 1.0
                    }
                },
                {
                    "terms": {
                        "revenueStatus.keyword": [
                            "OPEN"
                        ],
                        "boost": 1.0
                    }
                },
                {
                    "range": {
                        "recordUpdateTime":{
                            "gte":1655117440000
                        }
                    }
                }
            ],
            "adjust_pure_negative": true,
            "boost": 1.0
        }
    },
    "version": true,
    "aggregations": {
        "TopLevelAggregation": {
            "composite" : {
                "size" : 10000,
                "sources" : [
                    {
                        "directs": {
                            "terms": {
                                "script": {
                                    "source": "def territoryNamesList = new ArrayList(); def name; def thLength = params._source.territoryHierarchy.length; for(int i = 0; i< thLength;i++) { def thRecord = params._source.territoryHierarchy[i]; if (params.territoryIds.contains(thRecord.id) && i+params.levelToReturn < thLength) { territoryNamesList.add(params._source.territoryHierarchy[i+params.levelToReturn].name);} } return territoryNamesList;",
                                    "lang": "painless",
                                    "params": {
                                        "territoryIds": [
                                            12345678
                                        ],
                                        "levelToReturn": 1
                                    }
                                }
                            }
                        }
                    },
                    {
                        "qtr" : {
                            "terms" : {
                                "field" : "quarter.keyword",
                                "missing_bucket" : false,
                                "order" : "asc"
                            }
                        }
                    }
                ]
            },
            "aggregations": {
                "revnRevenueAmount": {
                    "sum": {
                        "script": {
                            "source": "doc['revenueTypeCategory.keyword'].value != 'Other' ? doc['revnRevenueAmount']:doc['revnRevenueAmount']",
                            "lang": "painless"
                        },
                        "value_type": "long"
                    }
                }
            }
        }
    }
}

```

So this query returns the result properly.

I am trying to create a corresponding java implementation for it. So I have created the code as below -

```auto
BoolQueryBuilder baseQueryBuilder = getQueryBuilder(searchCriteria);

List<TermsAggregationBuilder> aggregationBuilders = getMultiBaseAggregationBuilders(searchCriteria, baseQueryBuilder);

```

Where the getMultiBaseAggregationBuilders returns the 2 different terms aggregations shown in the query above - directs and qtr. Now I am not finding any API to send this list of terms aggregations to the composite aggregation builder. Would be really grateful if someone can give me a pointer as to how this list of terms aggregations can be used inside the composite aggregation builder. Thanks in advance!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 5, 2022, 7:35pm UTC](https://discuss.elastic.co/t/java-client-for-composite-aggregation-using-terms-queries/309227/2 "2022-08-05T19:35:12Z")

</div>

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