title: How to build two nested aggregations in elasticsearch
body: <p>I have a mapping like this:</p>
  <pre><code>&quot;location&quot;: {
     &quot;type&quot;: &quot;nested&quot;,
     &quot;properties&quot;: {
       &quot;address&quot;: {
         &quot;type&quot;: &quot;nested&quot;,
         &quot;properties&quot;: {
           &quot;countryId&quot;: {
             &quot;type&quot;: &quot;long&quot;
           }
         }
       }
      }
  }
  </code></pre>
  <p>How can I write a aggregation to count how many time the countryId have been used. I have tried this query but it's not really work:</p>
  <pre><code>{
    &quot;aggregations&quot;: {
      &quot;addresses&quot;: {
        &quot;nested&quot;: {
          &quot;path&quot;: &quot;location.address&quot;
        },
        &quot;aggregations&quot;: {
          &quot;address.countryId&quot;: {
            &quot;terms&quot;: {
              &quot;field&quot;: &quot;address.countryId.keyword&quot;,
              &quot;size&quot;: 1000,
              &quot;min_doc_count&quot;: 1,
              &quot;shard_min_doc_count&quot;: 0,
              &quot;show_term_doc_count_error&quot;: false,
              &quot;order&quot;: [
                {
                  &quot;_count&quot;: &quot;desc&quot;
                },
                {
                  &quot;_key&quot;: &quot;asc&quot;
                }
              ],
              &quot;include&quot;: [
                &quot;1&quot;,
                &quot;2&quot;,
                &quot;3&quot;,
                &quot;4&quot;,
                &quot;5&quot;,
                &quot;6&quot;,
                &quot;7&quot;,
                &quot;8&quot;,
                &quot;9&quot;
              ],
              &quot;exclude&quot;: []
            }
          }
        }
      }
    }
  }
  </code></pre>
  <p>I think the problem come from the nested path but I can't fix it correctly</p>