title: Elasticsearch filter by terms aggregation
body: <p>I'm collecting a test runtime execution, so I have an index with these fields:</p>
  <ul>
  <li><code>testName</code>: name of the test. There are multiple tests.</li>
  <li><code>testDurationSeconds</code>: how much time the test run, in seconds.</li>
  <li><code>buildNumber</code>: <em>Jenkins</em> job number, for example.</li>
  </ul>
  <p>Now I want to calculate the <em>Average</em> of the <code>testDurationSeconds</code>, per <code>testName</code>. <strong>But only if the test run in the last <code>buildNumber</code></strong>.</p>
  <p>(For simplicity, let's say I have the last <code>buildNumber</code>.)</p>
  <p>My aggregation right now looks like this:</p>
  <pre class="lang-json prettyprint-override"><code>&quot;aggs&quot;: {
      &quot;byTestName&quot;: {
        &quot;terms&quot;: {
          &quot;field&quot;: &quot;testName.keyword&quot;
        },
        &quot;aggs&quot;: {
          &quot;byBuildNumber&quot;: {
            &quot;terms&quot;: {
              &quot;field&quot;: &quot;buildNumber&quot;
            },
            &quot;aggs&quot;: {
              &quot;avg&quot;: {
                &quot;avg&quot;: {
                  &quot;field&quot;: &quot;testDurationSeconds&quot;
                }
              }
            }
          }
        }
      }
    }
  </code></pre>
  <p>How can I filter it to eventually <strong>get the <code>average</code> of <code>testName&gt;testDurationSeconds</code> for all <code>buildNumber</code>s, only if the <code>testName</code> run on the specific <code>buildNumber</code></strong>?</p>
  <p>(Worst case, I accept to get the list of <code>testNames</code> that meets the requirement above by a different aggregation, and compare the lists in an applicative way).</p>
  <hr />
  <p><strong>Mapping:</strong></p>
  <pre class="lang-json prettyprint-override"><code>{
    &quot;mapping&quot;: {
      &quot;properties&quot;: {
        &quot;buildNumber&quot;: {
          &quot;type&quot;: &quot;integer&quot;
        },
        &quot;testDurationSeconds&quot;: {
          &quot;type&quot;: &quot;long&quot;
        },
        &quot;testName&quot;: {
          &quot;type&quot;: &quot;text&quot;,
          &quot;fields&quot;: {
            &quot;keyword&quot;: {
              &quot;type&quot;: &quot;keyword&quot;,
              &quot;ignore_above&quot;: 256
            }
          }
        }
      }
    }
  }
  </code></pre>
  <p><strong>Example:</strong></p>
  <pre class="lang-json prettyprint-override"><code>[
      {
          &quot;testName&quot;: &quot;Test1&quot;,
          &quot;testDurationSeconds&quot;: 10,
          &quot;buildNumber&quot;: 1
      },
      {
          &quot;testName&quot;: &quot;Test1&quot;,
          &quot;testDurationSeconds&quot;: 20,
          &quot;buildNumber&quot;: 2
      },
      {
          &quot;testClass&quot;: &quot;Test1&quot;,
          &quot;testDurationSeconds&quot;: 30,
          &quot;buildNumber&quot;: 3
      },
      {
          &quot;testClass&quot;: &quot;Test2&quot;,
          &quot;testDurationSeconds&quot;: 10,
          &quot;buildNumber&quot;: 1
      },
      {
          &quot;testClass&quot;: &quot;Test2&quot;,
          &quot;testDurationSeconds&quot;: 20,
          &quot;buildNumber&quot;: 2
      }
  ]
  </code></pre>
  <p>Let's say the last <code>buildNumber</code> was <code>3</code>. I want to get the average of <code>Test1&gt;testDurationSeconds</code> (it should be <code>20</code>), but since the <code>Test2</code> is not run in the last run, I want to ignore it.</p>
  <p>The best result will return the average of <code>Test1</code> only, but another result can return a list of tests that run in the last build (<code>3</code>) in another aggregation, in order to use that list to take the only relevant averages in the app level.</p>