'Spring Data ElasticSearch - Mapping _source to java object

I am trying to map _source to java pojo as below but I am getting null in response

public class ESDocument {
    
    @Id
    @Field(name = "_id")
    private String id;
    
    @Field(name = "_source", type = FieldType.Nested)
    private Object source;
    
}

Response says source filed is empty

SearchHit{id='12345_11', score=3.0, sortValues=[], content=ESDocument(id=12345_11, source=null), highlightFields={}}

Can someone advice. Thanks in advance.



Solution 1:[1]

You can use ObjectMapper to convert your response to a POJO/List<POJO> Something like this:

Search Response response = repository.search(...);
ObjectMapper mapper = new ObjectMapper();
for (SearchHit hit : response.getHits().getHits()) {
    POJO pojo = mapper.convertValue(hit.getSourceAsMap(), POJO.class);
    // process pojo here...
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 hitesh kaushik