title: Elasticsearch java query optimization
body: <p>I have a document with the following fields A, B, C, D, E.</p>
  
  <p>I can query to get the values of A and B in the following 2 ways</p>
  
  <p>Method 1:</p>
  
  <pre><code>    SearchResponse response1 = client
              .prepareSearch("indexname")
              .setTypes("all")
              .setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), FilterBuilders.termFilter("C", "SOME_VALUE")))
              .execute().actionGet();
  </code></pre>
  
  <p>Then parse the source to get Field A and B.</p>
  
  <p>Method 2</p>
  
  <pre><code>    String[] fields = {"A", "B"};
      SearchResponse response1 = client
              .prepareSearch("indexname")
              .setTypes("all")
              .setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), FilterBuilders.termFilter("C", "SOME_VALUE")))
              .addFields(fields)
              .execute().actionGet();
  </code></pre>
  
  <p>Now parse the fields itself directly instead of source.</p>
  
  <p>I compared the time took by both queries. The results were inconsistent. However, the response times were skewed to be lower towards Method 2. But it was not definite. So can anyone tell me for sure, which method is faster and why?</p>