title: Aggregating all fields for an object in a search query, without manually specifying the fields
body: <p>I have an index <code>products</code> which has an internal object <code>attributes</code> which looks like:</p>
  <pre><code>{
    properties: {
      id: {...},
      name: {...},
      colors: {...},
     // remaining fields
  }
  }
  </code></pre>
  <p>I'm trying to produce a search query with this form and I need to figure out how to write the aggs object.</p>
  <pre><code>{ query: {...}, aggs: {...} }
  </code></pre>
  <p>I can write this out manually for two fields to get the desired result, however the object contains 50+ fields so I need it to be able to handle it automatically</p>
  <pre><code>  &quot;aggs&quot;: {
      &quot;attributes.color_group.id&quot;: {
        &quot;terms&quot;: {
          &quot;field&quot;: &quot;attributes.color_group.id.keyword&quot;
        }
      },
      &quot;attributes.product_type.id&quot;: {
        &quot;terms&quot;: {
          &quot;field&quot;: &quot;attributes.product_type.id.keyword&quot;
        }
      }
    }
  </code></pre>
  <p>Gives me the result:</p>
  <pre><code>  &quot;aggregations&quot; : {
      &quot;attributes.product_type.id&quot; : {
        &quot;doc_count_error_upper_bound&quot; : 0,
        &quot;sum_other_doc_count&quot; : 34,
        &quot;buckets&quot; : [
          {
            &quot;key&quot; : &quot;374&quot;,
            &quot;doc_count&quot; : 203
          },
          {
            &quot;key&quot; : &quot;439&quot;,
            &quot;doc_count&quot; : 79
          },
          {
            &quot;key&quot; : &quot;460&quot;,
            &quot;doc_count&quot; : 28
          },
          {
            &quot;key&quot; : &quot;451&quot;,
            &quot;doc_count&quot; : 24
          },
          {
            &quot;key&quot; : &quot;558&quot;,
            &quot;doc_count&quot; : 18
          },
          {
            &quot;key&quot; : &quot;500&quot;,
            &quot;doc_count&quot; : 10
          },
          {
            &quot;key&quot; : &quot;1559&quot;,
            &quot;doc_count&quot; : 9
          },
          {
            &quot;key&quot; : &quot;1560&quot;,
            &quot;doc_count&quot; : 9
          },
          {
            &quot;key&quot; : &quot;455&quot;,
            &quot;doc_count&quot; : 7
          },
          {
            &quot;key&quot; : &quot;501&quot;,
            &quot;doc_count&quot; : 6
          }
        ]
      },
      &quot;attributes.color_group.id&quot; : {
        &quot;doc_count_error_upper_bound&quot; : 0,
        &quot;sum_other_doc_count&quot; : 35,
        &quot;buckets&quot; : [
          {
            &quot;key&quot; : &quot;12&quot;,
            &quot;doc_count&quot; : 98
          },
          {
            &quot;key&quot; : &quot;54&quot;,
            &quot;doc_count&quot; : 48
          },
          {
            &quot;key&quot; : &quot;118&quot;,
            &quot;doc_count&quot; : 43
          },
          {
            &quot;key&quot; : &quot;110&quot;,
            &quot;doc_count&quot; : 41
          },
          {
            &quot;key&quot; : &quot;111&quot;,
            &quot;doc_count&quot; : 35
          },
          {
            &quot;key&quot; : &quot;71&quot;,
            &quot;doc_count&quot; : 35
          },
          {
            &quot;key&quot; : &quot;119&quot;,
            &quot;doc_count&quot; : 24
          },
          {
            &quot;key&quot; : &quot;62&quot;,
            &quot;doc_count&quot; : 21
          },
          {
            &quot;key&quot; : &quot;115&quot;,
            &quot;doc_count&quot; : 20
          },
          {
            &quot;key&quot; : &quot;113&quot;,
            &quot;doc_count&quot; : 15
          }
        ]
      }
    }
  </code></pre>
  <p>Which is exactly what I want. After some research I found that you can use <code>query_string</code> which would allow me to find everything starting with <code>attributes.</code>, however it does not seem to work inside aggregations.</p>