title: elasticsearch min and max aggs on other aggregation results
body: <p>I am trying to find min and max of an aggregation result in elasticsearch. what query will allow it to first aggregate by a field and then perform monthy sub aggregation to get monthly data for that field and then find min and max among those monthly calculated metric. <strong>i am stuck at finding <code>min</code> and <code>max</code></strong>. for rest of the requirement below is my query </p>
  
  <p>The question can be phrased as : <strong><em>what is the max and min transaction (considering all months) in any country ?</em></strong>
  here is the query that i tried:</p>
  
  <pre><code>{
    "query": {
      "bool": {
        "must": [
          {
            "term": {
              "Id": "7466swy7893jgs225"
            }
          }
        ]
      }
    },
    "aggs": {
      "dimension": {
        "terms": {
          "field": "country"
        },
        "aggs": {
          "MoM": {
            "date_histogram": {
              "field": "date",
              "interval": "month",
              "format": "MMM YY"
            },
            "aggs": {
              "totalValue": {
                "sum": {
                  "field": "Transactions"
                }
              }
            }
          }
        }
      }
    },
    "size": 0
  </code></pre>
  
  <p>}</p>
  
  <p>This is the output that i get:</p>
  
  <pre><code>   {
      "aggregations": {
      "dimension": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
      {
          "key": "USA",
          "doc_count": 392,
          "MoM": {
              "buckets": [
              {
              "key_as_string": "jan 16",
              "key": 1462060800000,
              "doc_count": 31,
              "totalValue": { "value": 352 }
              },
              {
              "key_as_string": "Feb 16",
              "key": 1462060800000,
              "doc_count": 31,
              "totalValue": { "value": 429 }
              }
           ??????????????/MIN and MAX of 352 AND 429
            ]
          }
      },
      {
          "key": "Bhutan",
          "doc_count": 392,
          "MoM": {
              "buckets": [
              {
              "key_as_string": "Jan 16",
              "key": 1462060800000,
              "doc_count": 31,
              "totalValue": {"value": 750 }
              }
              ,
              {
              "key_as_string": "Feb 16",
              "key": 1464739200000,
              "doc_count": 30,
              "totalValue": {"value": 827 }
          }
            ??????????????/MIN and MAX of 750 AND 827
  
        ]
       }
    }
   ]
   }
   }}
  </code></pre>