title: Elasticsearch: Aggregation on filtered nested objects to find unique values
body: <p>I have an array of objects (tags) in each document in Elasticsearch 5:</p>
  
  <pre><code>{
      "tags": [
          { "key": "tag1", "value": "val1" },
          { "key": "tag2", "value": "val2" },
          ...
      ]
  }
  </code></pre>
  
  <p>Now I want to find unique tag values for a certain tag key. Something similiar to this SQL query:</p>
  
  <p><code>SELECT DISTINCT(tags.value) FROM tags WHERE tags.key='some-key'</code></p>
  
  <p>I have came to this DSL so far:</p>
  
  <pre><code>{
      "size": 0,
      "aggs": {
          "my_tags": {
              "nested": {
                  "path": "tags"
              },
              "aggs": {
                  "filter" : { "terms": { "tags.key": "tag1" } },
                  "aggs": {
                      "my_tags_values": {
                          "terms" : {
                              "field" : "tags.value",
                              "size": 9999
                           }
                      }                   
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>But It is showing me this error:</p>
  
  <p><code>[terms] unknown field [tags.key], parser not found</code>.</p>
  
  <p>Is this the right approach to solve the problem? Thanks for your help.</p>
  
  <p><strong>Note:</strong> I have declared the <code>tags</code> field as a nested field in my mapping.</p>