# Add Json aggregation string to SearchRequestBuilder in java API

**URL:** https://discuss.elastic.co/t/add-json-aggregation-string-to-searchrequestbuilder-in-java-api/91639
**Category:** Elasticsearch
**Created:** [July 3, 2017, 1:34pm UTC](https://discuss.elastic.co/t/add-json-aggregation-string-to-searchrequestbuilder-in-java-api/91639 "2017-07-03T13:34:51Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![philv](https://avatars.discourse-cdn.com/v4/letter/p/ebca7d/32.png) [@philv](https://discuss.elastic.co/u/philv)
#### Post date: [July 3, 2017, 1:34pm UTC](https://discuss.elastic.co/t/add-json-aggregation-string-to-searchrequestbuilder-in-java-api/91639/1 "2017-07-03T13:34:51Z")

</div>

I have a java app that makes use of the SearchRequestBuilder. Usually I perform the operation using this pseudocode:

SearchRequestBuilder sb;  
AggregationBuilder agBuilder;  
//omitted some boiler plate where these are instantiatehow

sb.addAggregation(agBuilder);

I have a requirement whereby the aggregations part of the query is in the form of a json string. I need to add it to the SearchRequestBuilder somehow. Is there a recommended way to do this ?

I thought I might be able to achieve it via the XContentBuilder , but I'm not sure if it's possible to add the results of this to a SearchRequestBuilder anymore, so I'm looking for suggestions. (and I was rather hoping there was a quicker way than me creating the boiler plate to work through the json and turn it into an AggregationBuilder myself) 🙂

Many thanks,  
Phil

---

<div class="post-metadata">

### Author: ![philv](https://avatars.discourse-cdn.com/v4/letter/p/ebca7d/32.png) [@philv](https://discuss.elastic.co/u/philv)
#### Post date: [July 4, 2017, 2:34pm UTC](https://discuss.elastic.co/t/add-json-aggregation-string-to-searchrequestbuilder-in-java-api/91639/2 "2017-07-04T14:34:51Z")

</div>

My question is essentially the same as this one:

> [@How to use the ElasticSearch Java API to build an Aggregation from JSON](https://discuss.elastic.co/t/how-to-use-the-elasticsearch-java-api-to-build-an-aggregation-from-json/70773):
>
> 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) .setIndi…

I presume the answer is the same ?

---

<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: [August 1, 2017, 2:35pm UTC](https://discuss.elastic.co/t/add-json-aggregation-string-to-searchrequestbuilder-in-java-api/91639/3 "2017-08-01T14:35:18Z")

</div>

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