There is an api which returns a elastic search query with placeholders. Once this query string is obtained I need to update the placeholders and then create a SearchSourceBuilder instance. Using the searchInstance, I need to create a SearchRequest and hit the elastic search instance through restHighLevelClient.

But how do I create the SearchSourceBuilder instance, I have tried this following approach but getting the below error, not sure where, I am going wrong.

String queryString = // 
JsonXContentParser xContentParser = new JsonXContentParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, new JsonFactory().createParser(s));
//below line throws the error
SearchSourceBuilder sourceBuilder = SearchSourceBuilder.fromXContent(xContentParser);
searchRequest.source(soruceBuilder)
restHighLevelClient.search(source,RequestOptions.DEFAULT);

Error: named objects are not supported for this parser

1

There are 1 answers

3
Val On

The error you get is when creating the JsonXContentParser not the SearchSourceBuilder

Try this:

XContentParser xContentParser = XContentType.JSON.xContent()
                .createParser(XContentParserConfiguration.EMPTY, Strings.toString(s));
SearchSourceBuilder sourceBuilder = SearchSourceBuilder.fromXContent(xContentParser);