0

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

User27854
  • 824
  • 1
  • 16
  • 40

1 Answers1

0

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);
Val
  • 207,596
  • 13
  • 358
  • 360
  • what is the Strings.toStrings(s)? – User27854 Apr 06 '22 at 11:27
  • also, I am using ES version 7.6 on going through the java docs for elasticserach-x-content, I observed that XContentParserConfiguration is introduced in 8.1.2. I am going ahead and using this jar, I hope it does not throws any problem. It took me sometime to figure out these. – User27854 Apr 06 '22 at 11:31
  • 1
    If you're using 7.6, you should not use JARs from 8.1.2... Just a sec – Val Apr 06 '22 at 11:45