Search elasticsearch with java client using JSON query

Unfortunately NamedXContentRegistry.EMPTY doesn't work for parsing QueryBuilders without registering the built-in queries first, but they should be available through searchModule.getNamedXContents(). So to create the parser in the above example you can use

try (XContentParser parser = XContentFactory.xContent(XContentType.JSON)
                        .createParser(new NamedXContentRegistry(searchModule.getNamedXContents()), content)) {
            searchSourceBuilder.parseXContent(new QueryParseContext(parser));
}
5 Likes