# Aggregations and sub-Aggregations in java API client

**URL:** <https://discuss.elastic.co/t/aggregations-and-sub-aggregations-in-java-api-client/341363>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [August 22, 2023, 12:13pm UTC](https://discuss.elastic.co/t/aggregations-and-sub-aggregations-in-java-api-client/341363 "2023-08-22T12:13:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dt2244](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dt2244/32/124858_2.png) [@dt2244](https://discuss.elastic.co/u/dt2244)\
**Post date:** [August 22, 2023, 12:13pm UTC](https://discuss.elastic.co/t/aggregations-and-sub-aggregations-in-java-api-client/341363/1 "2023-08-22T12:13:22Z")

</div>

i am trying to rewrite my code from elasticsearch version 7.10.2 to latest version es 8.9 but i am having some problems:  
code version 7.10.2:

```auto
FilterAggregationBuilder filteredAggs = AggregationBuilders
                        .filter(newsCategory.CategoryID + "",
                                termQuery("ListTreeCategory", "g" + newsCategory.CategoryID + "g"))
                        .subAggregation(terms("byActivedDate").field("NewsID")
                                .order(BucketOrder.aggregation("activedDateDescending", false))
                                .subAggregation(max("activedDateDescending").field("ActivedDate"))
                                .size(newsQuery.PageSize)
                                .subAggregation(AggregationBuilders.topHits("topActivedDate").size(1).from(0)
                                        .sort("ActivedDate", SortOrder.DESC).fetchSource(topHitIncludes, null)));
                builder.aggregation(filteredAggs)

```

code version 8.9  
`

```auto
 <Map<String, Aggregation> map = new HashMap<>();
                Aggregation subAggregation3 = new Aggregation.Builder().topHits(TopHitsAggregation.of(t -> t
                        .field("topActivedDate")
                        .size(1)
                        .from(0))).build();
                Aggregation subAggregation2 = new Aggregation.Builder()
                        .max(new MaxAggregation.Builder().field("ActivedDate").build())
                        .aggregations("subAggregation3",subAggregation3).build();
                Aggregation subAggregation1 = new Aggregation.Builder()
                        .filter(ElasticsearchQueryBuilder.terms("ListTreeCategory", "g" + newsCategory.getCategoryID() + "g"))
                        .aggregations("subAggregation2",subAggregation2)
                        .build();
                Aggregation aggregations = new Aggregation.Builder()
                        .terms(new TermsAggregation.Builder().field("NewsID")
                                .order(NamedValue.of("activedDateDescending", SortOrder.Asc))
                                .size(newsQuery.getPageSize()).build())
                        .aggregations("activedDateDescending",subAggregation1).build(); `

                SortOptions sb = new SortOptions.Builder()
                        .field(f -> f
                                .field("ActivedDate")
                                .order(SortOrder.Desc)).build();
                map.put(newsCategory.getCategoryID() + "", aggregations);
                searchRequest.aggregations(map).sort(sb).source(SourceConfig.of(sou -> sou.filter(in -> in.includes(Arrays.asList(topHitIncludes)))));

```

I got the following error and don't know how to solve it, please help:  
`co.elastic.clients.elasticsearch._types.ElasticsearchException: [es/search] failed: [parsing_exception] Unknown key for a VALUE_STRING in [field].`

---

<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:** [September 19, 2023, 12:14pm UTC](https://discuss.elastic.co/t/aggregations-and-sub-aggregations-in-java-api-client/341363/2 "2023-09-19T12:14:06Z")

</div>

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