我想将 json 字符串序列化为 Elasticsearch SearchResponse 对象。如果 json 字符串不包含聚合,它可以正常工作。
如果 json 字符串包含聚合,则 XContentParser 会抛出 ParsingException[无法解析键控为 [target_field] 的聚合 异常。
我用来将 json 字符串序列化为 Elasticsearch SearchResponse 对象的代码:
Settings settings = Settings.builder().build();
SearchModule searchModule = new SearchModule(settings, false, new ArrayList<>());
NamedXContentRegistry xContentRegistry = new NamedXContentRegistry(searchModule.getNamedXContents());
JsonXContentParser xContentParser = new JsonXContentParser(xContentRegistry,
new JsonFactory().createParser(json));
SearchResponse response = SearchResponse.fromXContent(xContentParser);
Run Code Online (Sandbox Code Playgroud)
似乎我必须向 NamedXContentRegistry 注册聚合,但我不知道如何注册。