有一个api,它返回一个带有占位符的弹性搜索查询。一旦获得了这个查询字符串,我需要更新占位符,然后创建一个SearchSourceBuilder实例。使用searchInstance,我需要创建一个SearchRequest,并通过restHighLevelClient命中弹性搜索实例。
但是我如何创建SearchSourceBuilder实例,我尝试了以下方法,但出现了以下错误,不确定在哪里,我错了。
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);
错误:此解析程序不支持命名对象
创建JsonXContentParser
而不是SearchSourceBuilder
时会出现错误
试试这个:
XContentParser xContentParser = XContentType.JSON.xContent()
.createParser(XContentParserConfiguration.EMPTY, Strings.toString(s));
SearchSourceBuilder sourceBuilder = SearchSourceBuilder.fromXContent(xContentParser);