Reputation: 696
I am using spring-data-elasticsearch (version 4.0). Now I need to convert search results from rest high level client to POJO objects.
I would use ObjectMapper from Jackson library. I am sure there are better ways to do it.
Spring-data-elasticsearch now (from 4.0) uses MappingElasticsearchConverter. Unfortunately, I don't have an idea how to accomplish this - I don't see any relevant documentation.
Entity
@Document(indexName="addresses")
public class Address {
@Id
private String uam;
@Field
private String street;
....
}
I just need to convert SearchHits to Address entity
@SpringBootTest
@RunWith(SpringRunner.class)
@ActiveProfiles(profiles = "dev")
public class TestElasticSearch {
@Autowired
private RestHighLevelClient highLevelClient;
@Test
void convertToPojo() throws Exception {
SearchRequest searchRequest = new SearchRequest();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHit[] hits = searchResponse.getHits().getHits();
Arrays.stream(hits).map(hit -> hit.toString()).forEach(System.out::println);
}
Upvotes: 2
Views: 2548
Reputation: 2373
I have another solution to this that is working well for me, and can work when you need a complex query that you can only get hits from, you can use
template.getElasticsearchConverter().read(YourPOJOClass.class, DocumentAdapters.from(hit));
where template is a org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate
you can get this injected into your class.
Upvotes: 0
Reputation: 33
The SearchHits class extends Streamable class, so if you use ElasticsearchOperations like leiblix said, you don't even need to call stream()
on it
public <T> List<T> search(Query query, Class<T> clazz) {
return elasticsearchOperations.search(query, clazz).map(SearchHit::getContent).toList();
}
Upvotes: 3
Reputation: 696
As P.J.Meisch mentioned, is much better to use ElasticsearchOperations
void convertToPojo() {
SearchHits<Address> hits = operations.search(Query.findAll(), Address.class);
List<Address> addresses = hits.stream().map(hit -> hit.getContent()).collect(Collectors.toList());
}
Upvotes: 2