# JAVA API not found for top\_metric aggregation

**URL:** <https://discuss.elastic.co/t/java-api-not-found-for-top-metric-aggregation/282880>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [August 31, 2021, 6:54am UTC](https://discuss.elastic.co/t/java-api-not-found-for-top-metric-aggregation/282880 "2021-08-31T06:54:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![crchaulagain](https://avatars.discourse-cdn.com/v4/letter/c/ed655f/32.png) [@crchaulagain](https://discuss.elastic.co/u/crchaulagain)\
**Post date:** [August 31, 2021, 6:54am UTC](https://discuss.elastic.co/t/java-api-not-found-for-top-metric-aggregation/282880/1 "2021-08-31T06:54:11Z")

</div>

I am using Elasticsearch 7.11.1 and going to use top\_metric aggregation to get the top records under the terms aggregation bucket. And my query is something like the following:

> {  
> "size": 0,  
> "query": {  
> "bool": {  
> "must": [  
> {  
> "terms": {  
> "type": [  
> "data\_type"  
> ]  
> }  
> }  
> ]  
> }  
> },  
> "aggregations": {  
> "field1": {  
> "terms": {  
> "field": "field1",  
> "size": 10  
> },  
> "aggregations": {  
> "field2": {  
> "terms": {  
> "field": "field2",  
> "size": 10  
> },  
> "aggregations": {  
> "field3": {  
> "terms": {  
> "field": "field3",  
> "size": 10  
> },  
> "aggregations": {  
> "amount": {  
> "extended\_stats": {  
> "field": "amount"  
> }  
> },  
> "hits\_here": {  
> "top\_metrics": {  
> "metrics": [  
> {  
> "field": "field3"  
> },  
> {  
> "field": "field4"  
> },  
> {  
> "field": "field5"  
> }  
> ],  
> "sort": "\_score"  
> }  
> }  
> }  
> }  
> }  
> }  
> }  
> }  
> },  
> "track\_total\_hits": 2147483647  
> }

This query yields the expected results from ES, and now I would like to use this via JAVA API but I'm unable to find its related JAVA class in both ES 7.11 and 7.14, am I missing anything here or is it actually not available in API? If not available in API, is there any options to use the similar functionality?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 31, 2021, 9:11am UTC](https://discuss.elastic.co/t/java-api-not-found-for-top-metric-aggregation/282880/2 "2021-08-31T09:11:50Z")

</div>

Have you seen the [TopMetricAggregationBuilder](https://github.com/elastic/elasticsearch/blob/7.11/client/rest-high-level/src/main/java/org/elasticsearch/client/analytics/TopMetricsAggregationBuilder.java)?

---

<div class="post-metadata">

**Author:** ![crchaulagain](https://avatars.discourse-cdn.com/v4/letter/c/ed655f/32.png) [@crchaulagain](https://discuss.elastic.co/u/crchaulagain)\
**Post date:** [August 31, 2021, 1:10pm UTC](https://discuss.elastic.co/t/java-api-not-found-for-top-metric-aggregation/282880/3 "2021-08-31T13:10:35Z")

</div>

Thank you for pointing this out. It worked.

---

<div class="post-metadata">

**Author:** ![SanshilaG](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sanshilag/32/84391_2.png) [@SanshilaG](https://discuss.elastic.co/u/SanshilaG)\
**Post date:** [September 2, 2021, 6:43pm UTC](https://discuss.elastic.co/t/java-api-not-found-for-top-metric-aggregation/282880/4 "2021-09-02T18:43:20Z")

</div>

Is there a chance that Top Metric Aggregation support will be added to Java Transport Client API?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [September 3, 2021, 2:57pm UTC](https://discuss.elastic.co/t/java-api-not-found-for-top-metric-aggregation/282880/5 "2021-09-03T14:57:47Z")

</div>

Not sure I understand the question. The transport client is not intended for client-to-cluster communication, so this is only available in the REST client.

---

<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:** [October 1, 2021, 2:58pm UTC](https://discuss.elastic.co/t/java-api-not-found-for-top-metric-aggregation/282880/6 "2021-10-01T14:58:01Z")

</div>

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