title: Retrieving specific fields using the Elasticsearch Java API
body: <p>I am using the Java API for Elasticsearch.
  Having saved entities into indexes, it is possible to retrieve them together with the complete source. However, I only want to retrieve selected fields, and that is not working.</p>
  
  <p>The folowing sample code:</p>
  
  <pre><code>SearchResponse response = client.prepareSearch("my-index")
     .setTypes("my-type")
     .setSearchType(SearchType.QUERY_AND_FETCH)
     .setFetchSource(true)
     .setQuery(QueryBuilders.termsQuery("field1", "1234"))
     .addFields("field1")
     .execute()
     .actionGet();
  
  for (SearchHit hit : response.getHits()){
     Map&lt;String, SearchHitField&gt; fields = hit.getFields();
     System.out.println(fields.size());
     Map map = hit.getSource();
     map.toString();
  }
  </code></pre>
  
  <p>will retrieve the correct entities from the index, including the complete source.</p>
  
  <p>For example, this is a snippet of the response :</p>
  
  <pre><code>"hits" : {
    "total" : 1301,
    "max_score" : 0.99614644,
    "hits" : [ {
    "_index" : "my-index",
    "_type" : "my-type",
    "_id" : "AU2P68COpzIypBTd80np",
    "_score" : 0.99614644,
    "_source":{"field1":"1234", ...}]}
  }, {
  </code></pre>
  
  <p>However, while <code>response.getHits()</code> returns the expected number of hits, the <code>fields</code> and <code>source</code> within each hit is empty.</p>
  
  <p>I am expecting each hit to contain the field specified in the line:</p>
  
  <pre><code>.addFields("field1")
  </code></pre>
  
  <p>Commenting out the line</p>
  
  <pre><code>.setFetchSource(true)
  </code></pre>
  
  <p>will cause the response not to include the source at all.</p>
  
  <p>The version of Elasticsearch is 1.5.0</p>
  
  <p>The following is the maven dependency the Java API:</p>
  
  <pre><code>&lt;dependency&gt;
     &lt;groupId&gt;com.sksamuel.elastic4s&lt;/groupId&gt;
     &lt;artifactId&gt;elastic4s_2.11&lt;/artifactId&gt;
     &lt;version&gt;1.5.5&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>Obiously, for performance reasons, I don't want to have to retrieve the complete entity.
  Does anyone know how to limit the retrieval to selected fields?
  Thanks</p>