# How to use the ElasticSearch Java API to build an Aggregation from JSON

**URL:** https://discuss.elastic.co/t/how-to-use-the-elasticsearch-java-api-to-build-an-aggregation-from-json/70773
**Category:** Elasticsearch
**Created:** [January 6, 2017, 10:40am UTC](https://discuss.elastic.co/t/how-to-use-the-elasticsearch-java-api-to-build-an-aggregation-from-json/70773 "2017-01-06T10:40:21Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![jonskeet](https://avatars.discourse-cdn.com/v4/letter/j/9dc877/32.png) [@jonskeet](https://discuss.elastic.co/u/jonskeet)
#### Post date: [January 6, 2017, 10:40am UTC](https://discuss.elastic.co/t/how-to-use-the-elasticsearch-java-api-to-build-an-aggregation-from-json/70773/1 "2017-01-06T10:40:21Z")

</div>

Hi everyone,

My code has an ElasticSearch query and aggregations in JSON format, and wants to call the ElasticSearch Java API.

For the Query portion, I can use WrapperQuery to build the query from the JSON as so:

```
val query = Json.obj(
  "query_string" -> Json.obj("query" -> "*"))

val aggs = Json.obj(
  "gender" -> Json.obj("terms" -> Json.obj("field": "gender")),
  "age" -> Json.obj("terms" -> Json.obj("field": "age")))

val aggsRequestBuilder = new SearchRequestBuilder(client)
  .setIndices(index())
  .setQuery(QueryBuilders.wrapperQuery(query.toString())
  .addAggregation(AggregationBuilders.???(aggs.toString())

```

But then, I also have the JSON for the aggregations and I don't see an AggregationsBuilder.wrapperAggregation() function that I can use to build aggregations object from JSON.

Am I missing something?

Best regards,

The Notorious Jon Skeet

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [January 6, 2017, 11:00am UTC](https://discuss.elastic.co/t/how-to-use-the-elasticsearch-java-api-to-build-an-aggregation-from-json/70773/2 "2017-01-06T11:00:24Z")

</div>

Why not using the Aggregation API?

[https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/\_bucket\_aggregations.html#java-aggs-bucket-terms](https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/_bucket_aggregations.html#java-aggs-bucket-terms)

```
val aggsRequestBuilder = new SearchRequestBuilder(client)
  .setIndices(index())
  .setQuery(QueryBuilders.wrapperQuery(query.toString())
  .addAggregation(AggregationBuilders.terms("gender").field("gender"))
  .addAggregation(AggregationBuilders.terms("age").field("age"))
```

---

<div class="post-metadata">

### Author: ![jonskeet](https://avatars.discourse-cdn.com/v4/letter/j/9dc877/32.png) [@jonskeet](https://discuss.elastic.co/u/jonskeet)
#### Post date: [January 6, 2017, 11:32am UTC](https://discuss.elastic.co/t/how-to-use-the-elasticsearch-java-api-to-build-an-aggregation-from-json/70773/3 "2017-01-06T11:32:34Z")

</div>

Because the aggregations in our system are dynamic and I don't want to parse them.

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [January 6, 2017, 12:11pm UTC](https://discuss.elastic.co/t/how-to-use-the-elasticsearch-java-api-to-build-an-aggregation-from-json/70773/4 "2017-01-06T12:11:04Z")

</div>

I don't think you can.  
You can probably pass the full query DSL as a string but not only the agg part.

I did not check the code though.

---

<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: [February 3, 2017, 12:11pm UTC](https://discuss.elastic.co/t/how-to-use-the-elasticsearch-java-api-to-build-an-aggregation-from-json/70773/5 "2017-02-03T12:11:25Z")

</div>

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

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [April 7, 2018, 3:01am UTC](https://discuss.elastic.co/t/how-to-use-the-elasticsearch-java-api-to-build-an-aggregation-from-json/70773/6 "2018-04-07T03:01:38Z")

</div>

Updating this old thread as @monde shared with me a solution he found on [SOF](https://stackoverflow.com/questions/34889026/how-to-use-the-elasticsearch-java-api-to-build-an-aggregation-from-json):

```
 val aggsRequestBuilder = new SearchRequestBuilder(client)
  .setIndices(index())
  .setQuery(QueryBuilders.wrapperQuery(query.toString())
  .setAggregations(agg.toString().getBytes())
```
