title: How to calculate difference between metrics in different aggregations in elasticsearch
body: <p>I want to calculate the <strong>difference of nested aggregations</strong> between two dates.</p>
  
  <p>To be more concrete is it possible to calculate the difference between <code>date_1.buckets.field_1.buckets.field_2.buckets.field_3.value -   date_2.buckets.field_1.buckets.field_2.buckets.field_3.value</code> given the below request/response. Is that possible with elasticsearch v.1.0.1?</p>
  
  <p>The aggregation query request looks like this:</p>
  
  <pre><code> {
    "query": {
      "filtered": {
        "query": {
          "match_all": {}
        },
        "filter": {
          "bool": {
            "must": [
              {
                "terms": {
                  "date": [
                    "2014-08-18 00:00:00.0",
                    "2014-08-15 00:00:00.0"
                  ]
                }
              }
            ]
          }
        }
      }
    },
    "aggs": {
      "date_1": {
        "filter": {
          "terms": {
            "date": [
              "2014-08-18 00:00:00.0"
            ]
          }
        },
        "aggs": {
          "my_agg_1": {
            "terms": {
              "field": "field_1",
              "size": 2147483647,
              "order": {
                "_term": "desc"
              }
            },
            "aggs": {
              "my_agg_2": {
                "terms": {
                  "field": "field_2",
                  "size": 2147483647,
                  "order": {
                    "_term": "desc"
                  }
                },
                "aggs": {
                  "my_agg_3": {
                    "sum": {
                      "field": "field_3"
                    }
                  }
                }
              }
            }
          }
        }
      },
      "date_2": {
        "filter": {
          "terms": {
            "date": [
              "2014-08-15 00:00:00.0"
            ]
          }
        },
        "aggs": {
          "my_agg_1": {
            "terms": {
              "field": "field_1",
              "size": 2147483647,
              "order": {
                "_term": "desc"
              }
            },
            "aggs": {
              "my_agg_1": {
                "terms": {
                  "field": "field_2",
                  "size": 2147483647,
                  "order": {
                    "_term": "desc"
                  }
                },
                "aggs": {
                  "my_agg_3": {
                    "sum": {
                      "field": "field_3"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>And the response looks like this: </p>
  
  <pre><code>{
    "took": 236,
    "timed_out": false,
    "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
    },
    "hits": {
      "total": 1646,
      "max_score": 0,
      "hits": []
    },
    "aggregations": {
      "date_1": {
        "doc_count": 823,
        "field_1": {
          "buckets": [
            {
              "key": "field_1_key_1",
              "doc_count": 719,
              "field_2": {
                "buckets": [
                  {
                    "key": "key_1",
                    "doc_count": 275,
                    "field_3": {
                      "value": 100
                    }
                  }
                ]
              }
            }
          ]
        }
      },
      "date_2": {
        "doc_count": 823,
        "field_1": {
          "buckets": [
            {
              "key": "field_1_key_1",
              "doc_count": 719,
              "field_2": {
                "buckets": [
                  {
                    "key": "key_1",
                    "doc_count": 275,
                    "field_3": {
                      "value": 80
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
  </code></pre>
  
  <p>Thank you.</p>