Skip to content

Instantly share code, notes, and snippets.

@asavt92
Created June 25, 2020 07:04
Show Gist options
  • Save asavt92/07d0fdebb0a50040d0d64e946d2b5a92 to your computer and use it in GitHub Desktop.
Save asavt92/07d0fdebb0a50040d0d64e946d2b5a92 to your computer and use it in GitHub Desktop.
org.elasticsearch.common.xcontent deserialize json string example
1. SearchResponse ee = SearchResponse
.fromXContent(
new JsonXContentParser(
NamedXContentRegistry.EMPTY,
new JsonFactory().createParser(content)));
2.
NamedXContentRegistry registry = new NamedXContentRegistry(getDefaultNamedXContents());
XContentParser parser = JsonXContent.jsonXContent.createParser(registry, content);
SearchResponse qq = SearchResponse.fromXContent(parser);
.......................
public static List<NamedXContentRegistry.Entry> getDefaultNamedXContents() {
Map<String, ContextParser<Object, ? extends Aggregation>> map = new HashMap<>();
map.put(TopHitsAggregationBuilder.NAME, (p, c) -> ParsedTopHits.fromXContent(p, (String) c));
map.put(StringTerms.NAME, (p, c) -> ParsedStringTerms.fromXContent(p, (String) c));
List<NamedXContentRegistry.Entry> entries = map.entrySet().stream()
.map(entry -> new NamedXContentRegistry.Entry(Aggregation.class, new ParseField(entry.getKey()), entry.getValue()))
.collect(Collectors.toList());
return entries;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment