title: Aggregating a Key/Value list in ElasticSearch
body: <p>General problem is that I've created a Name/Value mapping in elastic search to deal with a potentially huge user input of tags - as opposed to allowing an open schema where people can just create documents with new properties.</p>
  
  <p>I've got an elastic search mapping that looks like this:</p>
  
  <pre><code>           "Tags" : {
                "properties" : {
                   "Value" : {
                      "analyzer" : "keyword",
                      "type" : "string"
                   },
                   "Name" : {
                      "analyzer" : "keyword",
                      "type" : "string"
                   }
                }
             },
  </code></pre>
  
  <p>With records that look like this</p>
  
  <pre><code>           "Tags" : [
                {
                   "Name" : "group",
                   "Value" : "foobar"
                },
                {
                   "Name" : "season",
                   "Value" : "winter"
                }
             ],
  </code></pre>
  
  <p>What I'm trying to do with an elastic search query is to write a script that will aggregate only the season entries.</p>
  
  <pre><code>...  
  "script" : "for (int i = 0; i &lt; doc['Tags.Value'].values.length; i++) { 
           if (doc['Tags.Value'].values[i] == 'season') {
                return doc['Tags.Names'].values[i]
            } }"
  ...
  </code></pre>
  
  <p>I've gone through about 200 permutations of the above script and it's not quite returning the results that I would like to see.</p>