title: Elastic Search Results to POJO
body: <p>I am using spring-data-elasticsearch (version 4.0). Now I need to convert search results from rest high level client to POJO objects.</p>
  
  <p>I would use ObjectMapper from Jackson library. I am sure there are better ways to do it. </p>
  
  <p>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.</p>
  
  <p><strong>Entity</strong></p>
  
  <pre><code>@Document(indexName="addresses")
  public class Address {
  
      @Id
      private String uam;
  
      @Field
      private String street;
  
      ....
  }
  </code></pre>
  
  <p>I just need to convert SearchHits to Address entity</p>
  
  <pre><code>@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 -&gt; hit.toString()).forEach(System.out::println);
      }
  </code></pre>