title: Elasticsearch return unique values for a field
body: <p>I am trying to build an Elasticsearch query that will return only unique values for a particular field.</p>
  
  <p><strong>I do not want to return all the values for that field nor count them.</strong></p>
  
  <p>For example, if there are 50 different values currently contained by the field, and I do a search to return only 20 hits (<code>size=20</code>). I want each of the 20 results to have a unique result for that field, but I don't care about the 30 other values not represented in the result.</p>
  
  <p>For example with the following search (pseudo code - not checked):</p>
  
  <pre class="lang-js prettyprint-override"><code>{
      from: 0,
      size: 20,
      query: {
          bool: {
              must: {
                  range: { field1: { gte: 50 }},
                  term: { field2: 'salt' },
  
                  /**
                  * I want to return only unique values for "field3", but I
                  * don't want to return all of them or count them.
                  *
                  * How do I specify this in my query?
                  **/
                  unique: 'field3',
              },
              mustnot: {
                  match: { field4: 'pepper'},
              }
          }
      }
  }
  </code></pre>