# Set source in SearchRequestBuilder with ES5.0 and Java API

**URL:** <https://discuss.elastic.co/t/set-source-in-searchrequestbuilder-with-es5-0-and-java-api/66870>\
**Category:** Elasticsearch\
**Created:** [November 22, 2016, 2:52pm UTC](https://discuss.elastic.co/t/set-source-in-searchrequestbuilder-with-es5-0-and-java-api/66870 "2016-11-22T14:52:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gquintana](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gquintana/32/48441_2.png) [@gquintana](https://discuss.elastic.co/u/gquintana)\
**Post date:** [November 22, 2016, 2:52pm UTC](https://discuss.elastic.co/t/set-source-in-searchrequestbuilder-with-es5-0-and-java-api/66870/1 "2016-11-22T14:52:25Z")

</div>

Hello,

I am porting and existing ES2 client to ES5 Java API.

How can I inject JSON into the SearchRequestBuilder object?

```
SearchRequestBuilder builder = client.prepareSearch().setIndices(indices);
builder.setSource(searchSourceBuilder);

```

How do I create the searchSourceBuilder object from a simple JSON string (containing the query)?  
I tried

```
searchSourceBuilder = new SearchSourceBuilder(StreamInput.wrap(paramSource.getBytes("UTF-8")));

```

But it raises UnsupportedOperationException: can't read named writeable from StreamInput

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [November 22, 2016, 3:16pm UTC](https://discuss.elastic.co/t/set-source-in-searchrequestbuilder-with-es5-0-and-java-api/66870/2 "2016-11-22T15:16:28Z")

</div>

> [@gquintana](#):
>
> ```auto
> searchSourceBuilder = new SearchSourceBuilder(StreamInput.wrap(paramSource.getBytes("UTF-8")));
> 
> ```

That one is for the internal serialization protocol. It is how nodes talk to one another but isn't super useful to you. It leaks into the Transport client because the Transport client uses the same code base as the rest of Elasticsearch.

I think you want `QueryBuilders#wrapperQuery`. But it doesn't take the whole search request body, only the stuff in the `query` clause. The rest of the search request body there isn't a thing for. Inside Elasticsearch we use `RestSearchAction#parseSearchRequest` to do the parsing but takes a bunch of parameters that aren't built for the transport client.

This change came about because we parse the search request on the coordinating node which means the guts of `SearchSourceBuilder` had to change totally. Again, this leaks into the transport client because the transport client is the same code as the Elasticsearch server.

---

<div class="post-metadata">

**Author:** ![gquintana](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gquintana/32/48441_2.png) [@gquintana](https://discuss.elastic.co/u/gquintana)\
**Post date:** [November 22, 2016, 3:53pm UTC](https://discuss.elastic.co/t/set-source-in-searchrequestbuilder-with-es5-0-and-java-api/66870/3 "2016-11-22T15:53:43Z")

</div>

Thanks Nik,

wrapperQuery solves half of the problem (better than nothing).  
The other half being aggregations, but there isn't any wrapperAgg, is it?

This setSource was pretty handy in previous releases, to do query templating (queries stored in the app as JSON text files).

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [November 22, 2016, 4:20pm UTC](https://discuss.elastic.co/t/set-source-in-searchrequestbuilder-with-es5-0-and-java-api/66870/4 "2016-11-22T16:20:17Z")

</div>

> [@gquintana](#):
>
> The other half being aggregations, but there isn't any wrapperAgg, is it?

Not that I can see.

> [@gquintana](#):
>
> This setSource was pretty handy in previous releases, to do query templating (queries stored in the app as JSON text files).

Maybe you could look at `SearchTemplateRequest`. It is part of the mustache module in 5.0 but that comes with the transport client [dependency](http://search.maven.org/#artifactdetails%7Corg.elasticsearch.client%7Ctransport%7C5.0.1%7Cjar). You'd use it like [this](https://github.com/elastic/elasticsearch/blob/5.x/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/SearchTemplateIT.java#L79-L85). If you declare the template as "inline" and you don't have any mustache in it (`{{` or `}}`) then it should pass through cleanly.

---

<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:** [December 20, 2016, 4:20pm UTC](https://discuss.elastic.co/t/set-source-in-searchrequestbuilder-with-es5-0-and-java-api/66870/5 "2016-12-20T16:20:23Z")

</div>

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