title: ElasticSearch/Painless: How do I access/sum all values in an object
body: <p>I've been looking at aggregations, and at scripting using <code>painless</code>, and I am not able to figure out how to iterate/sum over all values in an object.</p>
  
  <p>Example:</p>
  
  <p>My mapping looks like</p>
  
  <pre><code>"field1": {
    "properties": {
      "subfield1": {
        "type": "float"
      },
      "subfield2": {
        "type": "float"
      },
      "subfield3": {
        "type": "float"
      }
    }
  }
  </code></pre>
  
  <p>Let's assume my data looks like this:</p>
  
  <pre><code>{
    "field1" : {
      "subfield1": 50.0,
      "subfield2": 20.5,
      "subfield3": 30.5
    }
  }
  </code></pre>
  
  <p>I want to perform a range query on <code>50.0 + 20.5 + 30.5</code>, or, basically, access all the values within the <code>field1</code> object in some way.</p>
  
  <p>Aggregations do not allow me to use wild-cards in fields.
  I was <a href="https://github.com/elastic/elasticsearch/blob/master/core/src/main/java/org/elasticsearch/search/lookup/LeafDocLookup.java#L149" rel="nofollow noreferrer">looking at the code for <code>LeafDocLookup</code></a> (used internally for painless), and I see that the relevant methods are disabled.</p>
  
  <p>I've managed to write the script like this:</p>
  
  <pre><code>"query": {
    "script": {
      "script": {
        "inline": "return (doc['field1.subfield1'].value + doc['field1.subfield2'].value + doc['field1.subfield3'].value &gt; 50);",
        "lang": "painless"
      }
    }
  }
  </code></pre>
  
  <p>but this is obviously sub-optimal, and doesn't solve the main issue of dynamic keys.</p>