elasticsearch :6.1.2
我有一个通过JSON进行的输入查询,并且想使用高级Java API使用该查询数据来构建搜索请求。
String jsonQuery = "..." SearchRequest searchRequest = new SearchRequest() SearchSourceBuilder builder = ? searchRequest.source(builder);
我试图通过以下方式构造构建器:
SearchSourceBuilder.fromXContent(XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY, query));
但这会产生:
由以下原因引起:org.elasticsearch.ElasticsearchException:org.elasticsearch.common.xcontent.NamedXContentRegistry.parseNamedObject(NamedXContentRegistry.java:129)〜[elasticsearch-6.1.2.jar:6.1.2]处的解析器不支持namedObject org.elasticsearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder(AbstractQueryBuilder.java)上的org.elasticsearch.common.xcontent.support.AbstractXContentParser.namedObject(AbstractXContentParser.java:402)〜[elasticsearch-6.1.2.jar:6.1.2] :313)〜[elasticsearch-6.1.2.jar:6.1.2],位于org.elasticsearch.search.builder.SearchSourceBuilder.parseXContent(SearchSourceBuilder.java:1003)〜[elasticsearch-6.1.2.jar:6.1.2]在org.elasticsearch.search.builder.SearchSourceBuilder.fromXContent(SearchSourceBuilder.java:115)〜[elasticsearch-6.1.2.jar:6.1.2]
我现在以这种方式生成SearchSourceBuilder:
String query = "..." SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); SearchModule searchModule = new SearchModule(Settings.EMPTY, false, Collections.emptyList()); try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(new NamedXContentRegistry(searchModule .getNamedXContents()), query)) { searchSourceBuilder.parseXContent(parser); }