Elasticsearch :6.1.2

我有一个通过JSON进行的输入查询,并且想使用high level Java API使用该查询数据来构建搜索请求。

String jsonQuery = "..."
SearchRequest searchRequest = new SearchRequest()
SearchSourceBuilder builder = ?
searchRequest.source(builder);

我试图通过以下方式构造构建器:
SearchSourceBuilder.fromXContent(XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY, query));

但这会产生:

最佳答案

我现在以这种方式生成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);
}

关于elasticsearch - 如何通过JSON将查询设置为Elasticsearch SearchRequest?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48399046/

10-10 11:08