title: Return field from first and last document in Elasticsearch aggregation
body: <p>I have a sorted elasticsearch histogram aggregation that works fine as is.</p>
  
  <p>I need to return a field from the first and last document of each aggregation bucket.</p>
  
  <p>Current query (ruby-style syntax):</p>
  
  <pre><code>{
    query: {
      filtered: {
        filter: {
          bool: {
            must: [
              { term: { some_id: 'something' } },
              { range: {
                  completed_at: {
                    gte: start,
                    lte: end
                  }
                }
              }
            ]
          }
        },
        _cache: true,
        _cache_key: "special-query"
      }
    },
    aggs: {
      intervals: {
        histogram: {
          field: 'completed_at',
          interval: 24.hours.to_i,
          min_doc_count: 0,
          order: { _key: 'asc' }
        },
        aggs: {
          start_at: { min: { field: 'completed_at' } },
          end_at: { max: { field: 'completed_at' } },
          price_stats: { extended_stats: { field: 'rate' } },
        }
      }
    }
  }
  </code></pre>
  
  <p>I have searched through the documentation and googled extensively but can't find any solutions. Currently I have to send a second query to the database to explicitly fetch all the values for this field but I would like to roll that functionality in to my elasticsearch query.</p>