Created
June 25, 2020 07:04
-
-
Save asavt92/07d0fdebb0a50040d0d64e946d2b5a92 to your computer and use it in GitHub Desktop.
org.elasticsearch.common.xcontent deserialize json string example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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