Hello everyone,
I'm working with the new Elasticsearch Java Client and need to convert a Hit
's _source
(provided as JsonData
) into a Map<String, Object>
. The previous Rest High-Level Client offered SearchHit.getSourceAsMap()
, but I require a reliable equivalent in the new client for dynamic field access without POJOs.
Here's the problem:
Using hit.source().to(Map.class)
within a loop processing TopHitsAggregate
results, like:
// ... inside a loop processing TopHitsAggregate results
List<Map<String, Object>> hits = new ArrayList<>();
for (Hit<JsonData> hit : topHitsResult.hits().hits()) {
assert hit.source() != null;
Map<String, Object> mappingJson = hit.source().to(Map.class); // This line
hits.add(populateProtectionField(mappingJson)); // populateProtectionField is a custom method
}
This approach causes unexpected type issues or incorrect deserialization for nested fields in the _source
document. While hit.source().toMap()
correctly provides a Map<String, Object>
with nested structures, my ultimate goal is to simplify certain fields.
Sample Response :
{
"display_name": {
"chars": "Default Layer3 Section",
"string": "Default Layer3 Section",
"valueType": "STRING"
}
}
Desired Final Output (Transformed Map<String, Object>
):
{
"display_name": "Default Layer3 Section"
}
Could you confirm hit.source().toMap()
is the correct and robust approach for the initial deserialization, and then advise on best practices for subsequently transforming a nested Map<String, Object>
(like the one for display_name
) into a flattened structure as shown in my desired output? Insights into why hit.source().to(Map.class)
is problematic for complex JSON are also appreciated! Thank you.