title: Elasticsearch aggregation of deep nested type
body: <p>Previously I have asked <a href="https://stackoverflow.com/questions/31841542/elasticsearch-metric-aggregation-number-of-elements-in-array/31846457?noredirect=1#comment51642542_31846457">this</a> question.</p>
  
  <p>The example document there was a simplified document. That was good for me to understand the differences in aggregation over non-nested type versus nested type. However, the simplification was hiding further complexity and so I have to expand on the question here.</p>
  
  <p>So my actual documents are closer to the following:</p>
  
  <pre><code>"_source": {
      "keyword": "my keyword",
      "response": [
          {
              "results": [
                  {
                      "items": [
                          {
                              "prop": [
                                  {
                                      "item_property_1": ["A"],
                                  }
                              ]
                              ( ... other properties )
                          },
                          {
                              "prop": [
                                  {
                                      "item_property_1": ["B"],
                                  }
                              ]
                              ( ... other properties )
                          },
                          ( ... other items )
                      ]
                  }
              ],
              ( ... other properties )
          }
      ]
  }
  </code></pre>
  
  <p>So I kept the crucial properties <code>keyword</code>, <code>items</code>, and <code>item_property_1</code>, but hid lots of other things that complicate the situation. First, notice that compared to the referenced question there is lots of extra nesting: between the root and "items", and between "items" and "item_property_1". Additionally, notice also that the properties <code>response</code> and <code>results</code> are both arrays with a single element. It's weird, but that's how it is :-)</p>
  
  <p>Now, the reason why this question is different from the one cited above is that I tried the accepted answer (which does work for the example there), and it doesn't work here. That is, if I use a mapping with:</p>
  
  <pre><code>"items": {
      "type":"nested",
      "properties": {
          "prop": {
              "properties": {
                  "item_property_1": {
                      "type": "string",
                      "index": "not_analyzed"
                  },
              }
          }
      }
  }
  </code></pre>
  
  <p>then the aggregation doesn't work. It returns zero hits.</p>
  
  <p>I will edit later and provider a ready to use sample bulk insert.</p>
  
  <p>EDIT:
  Alright, below I show three queries which are respectively: mapping, bulk insert and aggregation (with zero hits)</p>
  
  <p>Mapping (with <code>"type":"nested"</code> as indicated in the previous answered question)</p>
  
  <pre><code>PUT /test2/_mapping/test3
  {
      "test3": {
          "properties": {
              "keyword": {
                  "type": "string",
                  "index": "not_analyzed"
              },
              "response": {
                  "properties": {
                      "results": {
                          "properties": {
                              "items": {
                                  "type": "nested",
                                  "properties": {
                                      "prop": {
                                          "properties": {
                                              "item_property_1": {
                                                  "type": "string",
                                                  "index": "not_analyzed"
                                              }
                                          }
                                      }
                                  }
                              }
                          }
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>Bulk put:</p>
  
  <pre><code>PUT /test2/test3/_bulk
  { "index": {}}
  {    "keyword": "my keyword",    "response": [        {            "results": [                {                    "items": [                        {                            "prop": [                                {"item_property_1": ["A"]}                            ]                        },                        {                            "prop": [                                {"item_property_1": ["B"]}                            ]                        },                        {                            "prop": [                                {"item_property_1": ["A"]}                            ]                        }                    ]                }            ]        }    ]}
  { "index": {}}
  {    "keyword": "different keyword",    "response": [        {            "results": [                {                    "items": [                        {                            "prop": [                                {"item_property_1": ["A"]}                            ]                        },                        {                            "prop": [                                {"item_property_1": ["C"]}                            ]                        }                    ]                }            ]        }    ]}
  </code></pre>
  
  <p>Aggregation (zero hits):</p>
  
  <pre><code>POST /test2/test3/_search
  {
      "size":0,
      "aggregations": {
          "item_property_1_count": {
              "terms":{
                  "field":"item_property_1"
              }
          }
      }
  }
  </code></pre>