title: Loop through items with painless but 'No field found'
body: <p>I'm trying to make a select query with a Painless script, but I keep getting an error in the response that the script is not valid.</p>
  
  <p>What I'm trying to do in this simplified script is to check if my given param ages are both adult following the age in 'unit.info'.</p>
  
  <p><strong>Error:</strong></p>
  
  <pre><code>    "if(containsAge(doc['unitHolder.units'], params.ages)){",
      "                   ^---- HERE",...
  "caused_by": {
        "type": "illegal_argument_exception",
        "reason": "No field found for [unitHolder.units] in mapping with types [pack]"
        }
  </code></pre>
  
  <p><strong>Request query:</strong></p>
  
  <pre><code>    {
          "query": {
              "bool": {
                  "must": [
                      {
                          "script": {
                              "script": {
                                  "source": "boolean containsAge(def unit, def ages) 
                                  {
                                      if(unit.info.children.minAge != null)
                                      {
                                          int nrAdults = 0;
                                          int nrChildren = 0;
                                          int nrInfants = 0;
  
                                          for (age in ages)
                                          {
                                              if (age &lt; unit.info.children.maxAge.value)
                                              {
                                                  nrAdults++;
                                              }else if(age &gt; unit.info.children.minAge.value)
                                              {
                                                  nrInfants++;
                                              }else{
                                                  nrChildren++;
                                              }
                                          }
                                          if (nrAdults &gt; 2)
                                          {
                                              return true;
                                          }
                                      }
                                      return false;
                                  }
                                  if(containsAge(doc['unitHolder.units'], params.ages))
                                  {
                                      return true;
                                  }
                                  return false;",
                                  "lang": "painless",
                                  "params": {
                                      "ages": [
                                          50,
                                          35
                                      ]
                                  }
                              }
                          }
                      }
                  ]
              }
          },
          "size": 10
      }
  </code></pre>
  
  <p><strong>Mapping:</strong></p>
  
  <pre><code>"mappings": {
      "pack": {
          "properties": {
              "unitHolder": {
                  "properties": {
                      "createDate": {
                          "type": "date"
                      },
                      "units": {
                          "properties": {
                              "info": {
                                  "properties": {
                                      "children": {
                                          "properties": {
                                              "maxAge": {
                                                  "type": "long"
                                              },
                                              "minAge": {
                                                  "type": "long"
                                              }
                                          }
                                      }
                                  }
                              }
                          }
                      }
                  }
              }
          }
      }
  }
  </code></pre>