ElasticSearch Painless script: How to iterate in an array of Nested Objects : <p>I am trying to create a script using the <code>script_score</code> of the <code>function_score</code>.
I have several documents whose <code>rankings</code> field is <code>type="nested"</code>.
The mapping for the field is:</p>

<pre><code>"rankings": {
        "type": "nested",
        "properties": {
          "rank1": {
            "type": "long"
          },
          "rank2": {
            "type": "float"
          },
          "subject": {
            "type": "text"
          }
        }
      }
</code></pre>

<p>A sample document is:</p>

<pre><code>"rankings": [
{
    "rank1": 1051,
    "rank2": 78.5,
    "subject": "s1"
},
{
    "rank1": 45,
    "rank2": 34.7,
    "subject": "s2"
}]
</code></pre>

<p>What I want to achieve is to iterate over the nested objects of rankings. Actually, I need to use i.e. a for loop in order to find a particular <code>subject</code> and use the <code>rank1, rank2</code> to compute something.
So far, I use something like this but it does not seem to work (throwing a Compile error):</p>

<pre><code>"function_score": {
"script_score": {
    "script": {
        "lang": "painless",
        "inline": 
                 "sum = 0;"
                 "for (item in doc['rankings_cug']) {"
                     "sum = sum + doc['rankings_cug.rank1'].value;"
                 "}"
         }
    }
}
</code></pre>

<p>I have also tried the following options:</p>

<ol>
<li><code>for</code> loop using <code>:</code> instead of <code>in</code>: <code>for (item:doc['rankings'])</code> with no success.</li>
<li><code>for</code> loop using <code>in</code> but trying to iterate over a specific element of the object, i.e. the <code>rank1</code>: <code>for (item in doc['rankings.rank1'].values)</code>, which actually compile but it seems that it finds a zero-length array of <code>rank1</code>.</li>
</ol>

<p>I have read that <code>_source</code> element is the one which can return JSON-like objects, but as far as I found out it is not supported in Search queries.</p>

<p>Can you please give me some ideas of how to proceed with that?</p>

<p>Thanks a lot.</p>
