title: Improve performance of a nested term aggregation?
body: <p>Is there a way to improve performance of a nested term aggregation without sampling?</p>
  <p>Terms query:</p>
  <pre><code>GET &lt;INDEX&gt;/_search?pretty&amp;request_cache=false
  {
      &quot;_source&quot;: false,
      &quot;sort&quot;: [
          &quot;_doc&quot;
      ],
      &quot;size&quot;: 0,
      &quot;track_total_hits&quot;: false,
      &quot;aggregations&quot;: {
          &quot;nested_suggestions&quot;: {
              &quot;nested&quot;: {
                  &quot;path&quot;: &quot;measurement&quot;
              },
              &quot;aggs&quot;: {
                  &quot;suggestions&quot;: {
                      &quot;terms&quot;: {
                          &quot;field&quot;: &quot;measurement.description.label&quot;,
                          &quot;size&quot;: 1
                      }
                  }
              }
          }
      }
  }
  ...
  {
    &quot;took&quot; : 8239,
    &quot;timed_out&quot; : false,
    ...
    &quot;aggregations&quot; : {
      &quot;nested_suggestions&quot; : {
        &quot;doc_count&quot; : 226139234,
        &quot;suggestions&quot; : {
          &quot;doc_count_error_upper_bound&quot; : 7445607,
          &quot;sum_other_doc_count&quot; : 214543500,
          &quot;buckets&quot; : [
            {
              &quot;key&quot; : &quot;xxx&quot;,
              &quot;doc_count&quot; : 11635382
            }
          ]
        }
      }
    }
  }
  </code></pre>
  <p>Cardinality query:</p>
  <pre><code>GET &lt;INDEX&gt;/_search?pretty&amp;request_cache=false
  {
      &quot;_source&quot;: false,
      &quot;sort&quot;: [
          &quot;_doc&quot;
      ],
      &quot;size&quot;: 0,
      &quot;track_total_hits&quot;: false,
      &quot;aggregations&quot;: {
          &quot;nested_suggestions&quot;: {
              &quot;nested&quot;: {
                  &quot;path&quot;: &quot;measurement&quot;
              },
              &quot;aggs&quot;: {
                  &quot;suggestions&quot;: {
                      &quot;cardinality&quot;: {
                          &quot;field&quot;: &quot;measurement.description.label&quot;
                      }
                  }
              }
          }
      }
  }
  ...
  {
    &quot;took&quot; : 5688,
    &quot;timed_out&quot; : false,
    ...
    &quot;aggregations&quot; : {
      &quot;nested_suggestions&quot; : {
        &quot;doc_count&quot; : 226139234,
        &quot;suggestions&quot; : {
          &quot;value&quot; : 1379
        }
      }
    }
  }
  </code></pre>
  <p>Minimal mapping:</p>
  <pre class="lang-json prettyprint-override"><code>{
      &quot;settings&quot;: {
          &quot;number_of_replicas&quot;: &quot;0&quot;,
          &quot;number_of_shards&quot;: &quot;10&quot;,
          &quot;analysis&quot;: {
              &quot;normalizer&quot;: {
                  &quot;raw_clean&quot;: {
                      &quot;type&quot;: &quot;custom&quot;,
                      &quot;filter&quot;: [
                          &quot;asciifolding&quot;
                      ]
                  }
              }
          }
      },
      &quot;mappings&quot;: {
          &quot;_doc&quot;: {
              &quot;dynamic&quot;: &quot;strict&quot;,
              &quot;properties&quot;: {
                  &quot;id&quot;: {
                      &quot;type&quot;: &quot;keyword&quot;
                  },
                  &quot;measurement&quot;: {
                      &quot;type&quot;: &quot;nested&quot;,
                      &quot;dynamic&quot;: &quot;strict&quot;,
                      &quot;properties&quot;: {
                          &quot;id&quot;: {
                              &quot;type&quot;: &quot;keyword&quot;
                          },
                          &quot;description&quot;: {
                              &quot;type&quot;: &quot;text&quot;,
                              &quot;norms&quot;: false,
                              &quot;fields&quot;: {
                                  &quot;label&quot;: {
                                      &quot;type&quot;: &quot;keyword&quot;,
                                      &quot;normalizer&quot;: &quot;raw_clean&quot;,
                                      &quot;ignore_above&quot;: 255,
                                      &quot;eager_global_ordinals&quot;: true
                                  }
                              }
                          }
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  <p>I've verified that the global ordinals have data via <code>/_cat/fielddata?v</code>.</p>
  <p>Is this kind of performance expected with nested terms aggregations?</p>
  <p>Environment:</p>
  <ul>
  <li>elasticsearch 6.8.3</li>
  <li>index size ~200GB (with the full mapping)</li>
  <li>documents ~1million</li>
  <li>nested documents ~225million</li>
  <li>4CPU 16GB RAM 500GB SSD</li>
  </ul>