# No public build() method on Aggregation.Builder class

**URL:** <https://discuss.elastic.co/t/no-public-build-method-on-aggregation-builder-class/303173>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [April 25, 2022, 2:54pm UTC](https://discuss.elastic.co/t/no-public-build-method-on-aggregation-builder-class/303173 "2022-04-25T14:54:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sbrunot](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sbrunot/32/103222_2.png) [@sbrunot](https://discuss.elastic.co/u/sbrunot)\
**Post date:** [April 25, 2022, 2:54pm UTC](https://discuss.elastic.co/t/no-public-build-method-on-aggregation-builder-class/303173/1 "2022-04-25T14:54:53Z")

</div>

Hello,

In the new Elasticsearch Java Client API, the method build() of class co.elastic.clients.Elasticsearch.\_types.aggregations.Aggregation.Builder is not public. What is the correct way of creating an instance of co.elastic.clients.Elasticsearch.\_types.aggregations.Aggregation without using the Builder class ?

Thanks for your help,

Sébastien Brunot

---

<div class="post-metadata">

**Author:** ![sbrunot](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sbrunot/32/103222_2.png) [@sbrunot](https://discuss.elastic.co/u/sbrunot)\
**Post date:** [April 25, 2022, 3:31pm UTC](https://discuss.elastic.co/t/no-public-build-method-on-aggregation-builder-class/303173/2 "2022-04-25T15:31:25Z")

</div>

I found the anwser, that is quite simple: as building an instance of Aggregation itself doesn't make any sense, the build() method of Aggregation.Builder is not public. But that does not prevent me from creating an actual aggregation, for example:

```auto
		Aggregation add = new Aggregation.Builder()
				.avg(new AverageAggregation.Builder()
                                                [...]
						.build())
				.build();

```

---

<div class="post-metadata">

**Author:** ![swallez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/swallez/32/11972_2.png) [@swallez](https://discuss.elastic.co/u/swallez)\
**Post date:** [April 25, 2022, 5:34pm UTC](https://discuss.elastic.co/t/no-public-build-method-on-aggregation-builder-class/303173/3 "2022-04-25T17:34:52Z")

</div>

Your analysis is exactly right: an aggregation is just a wrapper object that contains the variant kind and the value. So its builder only contains methods to set a variant. And the result of these methods is an `ObjectBuilder` on which you can call `build()` to create the final object.

---

<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:** [May 23, 2022, 5:35pm UTC](https://discuss.elastic.co/t/no-public-build-method-on-aggregation-builder-class/303173/4 "2022-05-23T17:35:21Z")

</div>

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