# How to use the Java API to build an Aggregation from JSON in 6.2.4?

**URL:** https://discuss.elastic.co/t/how-to-use-the-java-api-to-build-an-aggregation-from-json-in-6-2-4/131129
**Category:** Elasticsearch
**Created:** [May 9, 2018, 8:38am UTC](https://discuss.elastic.co/t/how-to-use-the-java-api-to-build-an-aggregation-from-json-in-6-2-4/131129 "2018-05-09T08:38:04Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![luxiaoxun](https://avatars.discourse-cdn.com/v4/letter/l/c2a13f/32.png) [@luxiaoxun](https://discuss.elastic.co/u/luxiaoxun)
#### Post date: [May 9, 2018, 8:38am UTC](https://discuss.elastic.co/t/how-to-use-the-java-api-to-build-an-aggregation-from-json-in-6-2-4/131129/1 "2018-05-09T08:38:04Z")

</div>

I found the same question [here](https://discuss.elastic.co/t/how-to-use-the-elasticsearch-java-api-to-build-an-aggregation-from-json/70773?source_topic_id=91639). But this is not working in ES 6.2.4.

In old version, we can do this:  
SearchRequestBuilder builder = client.prepareSearch(index);  
QueryBuilder queryBuilder = getQueryBuilder(query);  
builder.setQuery(queryBuilder); // set query  
builder.setAggregations(agg.toString().getBytes()); // set aggregation

But in 6.2.4, we only have these 2 APIs:  
builder.addAggregation(AggregationBuilder aggregation)  
builder.addAggregation(PipelineAggregationBuilder aggregation)

How to make the "set aggregation" from JSON string work correctly?

---

<div class="post-metadata">

### Author: ![Matt\_Chambers1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matt_chambers1/32/43254_2.png) [@Matt\_Chambers1](https://discuss.elastic.co/u/Matt_Chambers1)
#### Post date: [May 23, 2018, 11:16pm UTC](https://discuss.elastic.co/t/how-to-use-the-java-api-to-build-an-aggregation-from-json-in-6-2-4/131129/3 "2018-05-23T23:16:56Z")

</div>

I have same issue, high level rest client doesn't seem to let you do it.

---

<div class="post-metadata">

### Author: ![luxiaoxun](https://avatars.discourse-cdn.com/v4/letter/l/c2a13f/32.png) [@luxiaoxun](https://discuss.elastic.co/u/luxiaoxun)
#### Post date: [May 25, 2018, 1:07am UTC](https://discuss.elastic.co/t/how-to-use-the-java-api-to-build-an-aggregation-from-json-in-6-2-4/131129/4 "2018-05-25T01:07:33Z")

</div>

In the end, we use low level client to send the "json query string" directly to ES to get response.

---

<div class="post-metadata">

### Author: ![Matt\_Chambers1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matt_chambers1/32/43254_2.png) [@Matt\_Chambers1](https://discuss.elastic.co/u/Matt_Chambers1)
#### Post date: [May 25, 2018, 9:52am UTC](https://discuss.elastic.co/t/how-to-use-the-java-api-to-build-an-aggregation-from-json-in-6-2-4/131129/5 "2018-05-25T09:52:42Z")

</div>

I took a different route, made an XContentParser and parsed the json directly into a SearchSourceBuilder. The ES unit tests had some code for doing it.

---

<div class="post-metadata">

### Author: ![luxiaoxun](https://avatars.discourse-cdn.com/v4/letter/l/c2a13f/32.png) [@luxiaoxun](https://discuss.elastic.co/u/luxiaoxun)
#### Post date: [May 25, 2018, 10:05am UTC](https://discuss.elastic.co/t/how-to-use-the-java-api-to-build-an-aggregation-from-json-in-6-2-4/131129/6 "2018-05-25T10:05:57Z")

</div>

@Matt_Chambers1 Can you show me the link about “ES unit tests” if you have it ? Thanks.

---

<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: [June 22, 2018, 10:06am UTC](https://discuss.elastic.co/t/how-to-use-the-java-api-to-build-an-aggregation-from-json-in-6-2-4/131129/7 "2018-06-22T10:06:08Z")

</div>

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