Set source in SearchRequestBuilder with ES5.0 and Java API

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

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.

1 Like

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).

Not that I can see.

Maybe you could look at SearchTemplateRequest. It is part of the mustache module in 5.0 but that comes with the transport client dependency. You'd use it like this. If you declare the template as "inline" and you don't have any mustache in it ({{ or }}) then it should pass through cleanly.

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