Skip to content

Instantly share code, notes, and snippets.

@dehidehidehi
Created October 23, 2024 08:46
Show Gist options
  • Save dehidehidehi/af2b9e66f482996f333d0bb2e0ad1dba to your computer and use it in GitHub Desktop.
Save dehidehidehi/af2b9e66f482996f333d0bb2e0ad1dba to your computer and use it in GitHub Desktop.
ElasticSearch Json to SearchResponse
public SearchResponse getSearchResponseFromJson(String jsonResponse) throws IOException {
var parser = JsonXContent.jsonXContent.createParser(
getDefaultNamedXContentRegistry(),
LoggingDeprecationHandler.INSTANCE,
jsonResponse);
return SearchResponse.fromXContent(parser);
}
private NamedXContentRegistry getDefaultNamedXContentRegistry() {
List<NamedXContentRegistry.Entry> entries = new ArrayList<>();
entries.addAll(getDefaultNamedXContents());
entries.addAll(getProvidedNamedXContents());
return new NamedXContentRegistry(entries);
}
private List<NamedXContentRegistry.Entry> getProvidedNamedXContents() {
List<NamedXContentRegistry.Entry> entries = new ArrayList<>();
for (var service : ServiceLoader.load(NamedXContentProvider.class))
entries.addAll(service.getNamedXContentParsers());
return entries;
}
private List<NamedXContentRegistry.Entry> getDefaultNamedXContents() {
Map<String, ContextParser<Object, ? extends org.elasticsearch.search.aggregations.Aggregation>> map = new HashMap<>();
map.put("cardinality", (p, c) -> ParsedCardinality.fromXContent(p, (String) c));
map.put("hdr_percentiles", (p, c) -> ParsedHDRPercentiles.fromXContent(p, (String) c));
map.put("hdr_percentile_ranks", (p, c) -> ParsedHDRPercentileRanks.fromXContent(p, (String) c));
map.put("tdigest_percentiles", (p, c) -> ParsedTDigestPercentiles.fromXContent(p, (String) c));
map.put("tdigest_percentile_ranks", (p, c) -> ParsedTDigestPercentileRanks.fromXContent(p, (String) c));
map.put("percentiles_bucket", (p, c) -> ParsedPercentilesBucket.fromXContent(p, (String) c));
map.put("min", (p, c) -> ParsedMin.fromXContent(p, (String) c));
map.put("max", (p, c) -> ParsedMax.fromXContent(p, (String) c));
map.put("sum", (p, c) -> ParsedSum.fromXContent(p, (String) c));
map.put("avg", (p, c) -> ParsedAvg.fromXContent(p, (String) c));
map.put("value_count", (p, c) -> ParsedValueCount.fromXContent(p, (String) c));
map.put("simple_value", (p, c) -> ParsedSimpleValue.fromXContent(p, (String) c));
map.put("derivative", (p, c) -> ParsedDerivative.fromXContent(p, (String) c));
map.put("bucket_metric_value", (p, c) -> ParsedBucketMetricValue.fromXContent(p, (String) c));
map.put("stats", (p, c) -> ParsedStats.fromXContent(p, (String) c));
map.put("stats_bucket", (p, c) -> ParsedStatsBucket.fromXContent(p, (String) c));
map.put("extended_stats", (p, c) -> ParsedExtendedStats.fromXContent(p, (String) c));
map.put("extended_stats_bucket", (p, c) -> ParsedExtendedStatsBucket.fromXContent(p, (String) c));
map.put("geo_bounds", (p, c) -> ParsedGeoBounds.fromXContent(p, (String) c));
map.put("geo_centroid", (p, c) -> ParsedGeoCentroid.fromXContent(p, (String) c));
map.put("histogram", (p, c) -> ParsedHistogram.fromXContent(p, (String) c));
map.put("date_histogram", (p, c) -> ParsedDateHistogram.fromXContent(p, (String) c));
map.put("sterms", (p, c) -> ParsedStringTerms.fromXContent(p, (String) c));
map.put("lterms", (p, c) -> ParsedLongTerms.fromXContent(p, (String) c));
map.put("dterms", (p, c) -> ParsedDoubleTerms.fromXContent(p, (String) c));
map.put("missing", (p, c) -> ParsedMissing.fromXContent(p, (String) c));
map.put("nested", (p, c) -> ParsedNested.fromXContent(p, (String) c));
map.put("reverse_nested", (p, c) -> ParsedReverseNested.fromXContent(p, (String) c));
map.put("global", (p, c) -> ParsedGlobal.fromXContent(p, (String) c));
map.put("filter", (p, c) -> ParsedFilter.fromXContent(p, (String) c));
map.put("sampler", (p, c) -> ParsedSampler.fromXContent(p, (String) c));
map.put("geohash_grid", (p, c) -> ParsedGeoHashGrid.fromXContent(p, (String) c));
map.put("range", (p, c) -> ParsedRange.fromXContent(p, (String) c));
map.put("date_range", (p, c) -> ParsedDateRange.fromXContent(p, (String) c));
map.put("geo_distance", (p, c) -> ParsedGeoDistance.fromXContent(p, (String) c));
map.put("filters", (p, c) -> ParsedFilters.fromXContent(p, (String) c));
map.put("adjacency_matrix", (p, c) -> ParsedAdjacencyMatrix.fromXContent(p, (String) c));
map.put("siglterms", (p, c) -> ParsedSignificantLongTerms.fromXContent(p, (String) c));
map.put("sigsterms", (p, c) -> ParsedSignificantStringTerms.fromXContent(p, (String) c));
map.put("scripted_metric", (p, c) -> ParsedScriptedMetric.fromXContent(p, (String) c));
map.put("ip_range", (p, c) -> ParsedBinaryRange.fromXContent(p, (String) c));
map.put("top_hits", (p, c) -> ParsedTopHits.fromXContent(p, (String) c));
map.put("composite", (p, c) -> ParsedComposite.fromXContent(p, (String) c));
final var entries = map.entrySet().stream()
.map((entry) -> new NamedXContentRegistry.Entry(org.elasticsearch.search.aggregations.Aggregation.class, new ParseField(entry.getKey()), entry.getValue()))
.collect(Collectors.toList());
entries.add(new NamedXContentRegistry.Entry(Suggest.Suggestion.class, new ParseField("term"), (parser, context) -> TermSuggestion.fromXContent(parser, (String) context)));
entries.add(new NamedXContentRegistry.Entry(Suggest.Suggestion.class, new ParseField("phrase"), (parser, context) -> PhraseSuggestion.fromXContent(parser, (String) context)));
entries.add(new NamedXContentRegistry.Entry(Suggest.Suggestion.class, new ParseField("completion"), (parser, context) -> CompletionSuggestion.fromXContent(parser, (String) context)));
return entries;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment