'Elastic Search how to form a SourceBuilder instance from a string. gives named objects are not supported for this parser

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



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Val