# HIGH PRIORITY -\> how to add subaggregtion in terms aggregation for spring-data-elasticsearch 5.1

**URL:** <https://discuss.elastic.co/t/high-priority-how-to-add-subaggregtion-in-terms-aggregation-for-spring-data-elasticsearch-5-1/344015>\
**Category:** Elasticsearch\
**Tags:** painless, language-clients\
**Created:** [September 27, 2023, 9:14pm UTC](https://discuss.elastic.co/t/high-priority-how-to-add-subaggregtion-in-terms-aggregation-for-spring-data-elasticsearch-5-1/344015 "2023-09-27T21:14:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abhinav\_Tyagi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abhinav_tyagi/32/126069_2.png) [@Abhinav\_Tyagi](https://discuss.elastic.co/u/Abhinav_Tyagi)\
**Post date:** [September 27, 2023, 9:14pm UTC](https://discuss.elastic.co/t/high-priority-how-to-add-subaggregtion-in-terms-aggregation-for-spring-data-elasticsearch-5-1/344015/1 "2023-09-27T21:14:36Z")

</div>

i am rewriting my older verison elasticsearch implementations.  
I latest spring-data-elasticsearch 5.1, queryBuilders got removed due to whic i am not able to use/ add "AggregationBuilders" inside my native query.  
Can any one help me to add a tophits aggregation in the terms aggregation provided below.

```auto
NativeQuery nativeQuery = NativeQuery.builder()
            .withQuery(queryBuilder)
            .withAggregation("property_room", TermsAggregation.of(tagg -> {
                  tagg.script(
                    Script.of(sc -> sc.inline(insc -> {
                      insc.lang(ScriptLanguage.Painless);
                      insc.source( "//test code");
                      insc.params(Collections.emptyMap());
                      return insc;
                    })));
                  tagg.size(10000);
                  tagg.minDocCount(datesToSearch.size());
                  return tagg;})._toAggregation())         
            .withPageable(PageRequest.of(0, 10000))
            .build();

```

---

<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 25, 2023, 9:14pm UTC](https://discuss.elastic.co/t/high-priority-how-to-add-subaggregtion-in-terms-aggregation-for-spring-data-elasticsearch-5-1/344015/2 "2023-10-25T21:14:49Z")

</div>

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