title: Elasticsearch: How can I access a nested field from a post_filter using painless
body: <p>I'm facing a challenge using painless in a post filter, what I need is to access a nested field from the painless script, the goal here is to apply the filter base on prices range (these ranges are base on a quoting), and we don't want to lose the aggregation applied, one of the problems is that each index has its own pricing factors and add-ons which means an unique price per index and we want to calculate that price with painless.</p>
  <p>quoting: 30 units
  Index 1 price: 24
  Index 2 price: 35</p>
  <p>quoting 60 units
  Index 1 price: 30
  Index 2 price: 67</p>
  <p><strong>mapping</strong></p>
  <pre><code>{
      &quot;properties&quot;: {
        &quot;studentName&quot;{
            &quot;type&quot;: &quot;text&quot;,          
        },
        &quot;class_room&quot;:{
            &quot;type&quot;:&quot;integer&quot;
        },
        &quot;pricing&quot;:{
            &quot;type&quot;:&quot;nested&quot;,
            &quot;properties&quot;: {
                &quot;name&quot;:{
                    &quot;type&quot;:&quot;text&quot;,
                    &quot;fielddata&quot;:true
                },
                &quot;addons&quot;:{
                    &quot;type&quot;:&quot;double&quot;
                },
                &quot;factor&quot;:{
                    &quot;type&quot;:&quot;double&quot;
                }
            }
         }
      }
  }
  </code></pre>
  <p><strong>Index example (_source)</strong></p>
  <p><strong>Index 1</strong></p>
  <pre><code>{
      &quot;studentName&quot;: &quot;Michael Smith&quot;,
      &quot;class_room&quot;: 12,
      &quot;pricing&quot; : [
          {
              &quot;name&quot;: &quot;A&quot;,
              &quot;addons&quot;: 2.25,
              &quot;factor&quot;: 0.6
          },
          {
              &quot;name&quot;: &quot;B&quot;,
              &quot;addons&quot;: 1.5,
              &quot;factor&quot;: 0.75            
          },
          {
              &quot;name&quot;: &quot;C&quot;,
              &quot;addons&quot;: 2.0,
              &quot;factor&quot;: 0.8            
          }
      ]
  }
  </code></pre>
  <p><strong>Index 2</strong></p>
  <pre><code>{
      &quot;studentName&quot;: &quot;Sara Morales&quot;,
      &quot;class_room&quot;: 12,
      &quot;pricing&quot; : [
          {
              &quot;name&quot;: &quot;A&quot;,
              &quot;addons&quot;: 2.25,
              &quot;factor&quot;: 0.6
          },
          {
              &quot;name&quot;: &quot;B&quot;,
              &quot;addons&quot;: 1.5,
              &quot;factor&quot;: 0.75            
          },
          {
              &quot;name&quot;: &quot;C&quot;,
              &quot;addons&quot;: 2.0,
              &quot;factor&quot;: 0.8            
          },
          {
              &quot;name&quot;: &quot;D&quot;,
              &quot;addons&quot;: 1.0,
              &quot;factor&quot;: 0.8            
          },
          {
              &quot;name&quot;: &quot;E&quot;,
              &quot;addons&quot;: 1.1,
              &quot;factor&quot;: 0.5            
          }                
      ]
  }
  </code></pre>
  <p><strong>post filter</strong></p>
  <pre><code>{
    &quot;post_filter&quot;: {
      &quot;nested&quot;:{
        &quot;path&quot;:&quot;seedData&quot;,
        &quot;query&quot;:{
          &quot;bool&quot;: {
            &quot;must&quot;: [
              {
                &quot;script&quot;: {
                  &quot;script&quot;: {
                    &quot;lang&quot;: &quot;painless&quot;,
                    &quot;inline&quot;: &quot;&quot;&quot;
                        def units = params.units;
                        def price = 0;
                        for(def i =0; i&lt; doc['pricing'].length; i++){
                          price += doc['pricing'][i].addons;
                          price += (doc['pricing'][i].factor * units);
                        }
                                              
                        if( price &gt;= params.minPrice &amp;&amp; price &lt;= 58){
                          return true;
                        } else{
                          return false;
                        }
                    &quot;&quot;&quot;,
                    &quot;params&quot;: {
                      &quot;units&quot;: 30,
                      &quot;minPrice&quot;: 15,
                      &quot;maxPrice&quot;: 58
                    }
                  }
                }
              }            
            ]
          }
        }
      }
    }
  }
  </code></pre>
  <p>Issues:</p>
  <ul>
  <li><p>With the current post-filter script I get this <em><strong>No field found for [seedData] in mapping with types []</strong></em></p>
  </li>
  <li><p>If try to access the value in for-loop like this <strong>doc['pricing.addons'].length</strong>, I always get 0.</p>
  </li>
  </ul>
  <p>Is there an error or I am missing something in the mapping?</p>
  <p>Is there an error in the way that I'm accessing the nested field with painless</p>
  <p><strong>Thank you for your help</strong></p>