title: Elasticsearch - set the _id inside the java response object
body: <p>I'm looking for a way to get <strong>_id</strong> set inside my java response object just like Spring Data does when we use <strong>@Id</strong> annotation.</p>
  <p>I'm using <strong>ES 7.17.3</strong></p>
  <p>Elasticsearch JSON response:</p>
  <pre><code>{
    &quot;_scroll_id&quot;: null,
    &quot;took&quot;: 3,
    &quot;timed_out&quot;: false,
    &quot;_shards&quot;: {
      &quot;total&quot;: 1,
      &quot;successful&quot;: 1,
      &quot;skipped&quot;: 0,
      &quot;failed&quot;: 0
    },
    &quot;hits&quot;: {
      &quot;total&quot;: {
        &quot;value&quot;: 7,
        &quot;relation&quot;: &quot;eq&quot;
      },
      &quot;max_score&quot;: null,
      &quot;hits&quot;: [
        {
          &quot;_index&quot;: &quot;test_data&quot;,
          &quot;_type&quot;: &quot;_doc&quot;,
          &quot;_id&quot;: &quot;aaaaaaaa&quot;,
          &quot;_score&quot;: null,
          &quot;_source&quot;: {
            &quot;savedBy&quot;: &quot;John&quot;,
            &quot;savedAt&quot;: &quot;2022-01-01T19:09:27.527+02:00&quot;
          }
        }
      ]
    }
  }
  </code></pre>
  <p>Response type:</p>
  <pre><code>class DataLoad 
  {
      @Id
      private String id;
  
      private String savedBy;
  
      private String savedAt;
  
      ...
  }
  </code></pre>
  <p>What I'm doing is to iterate over the response and set the id manually, but IMO it stinks :-\</p>
  <pre><code>for (Hit&lt;DataLoad&gt; hit: response.getHits().hits()) {
      DataLoad dataLoad = hit.source();
      dataLoad.setId(hit.id());
      // add dataLoad to a new list
  }
  </code></pre>