title: How to use aggregation value on document update using script in Elasticsearch
body: <p>I am trying to update a document field based on id using script. The value of that field should be MAX(field) * 2. For example consider the following index</p>
  <pre><code>PUT /my-index
  {
    &quot;mappings&quot;: {
      &quot;properties&quot;: {
        &quot;name&quot;: {
          &quot;type&quot;: &quot;text&quot;
        },
        &quot;cost&quot;: {
          &quot;type&quot;: &quot;integer&quot;
        }
      }
    }
  }
  </code></pre>
  <p>Document will be created with only <strong>name</strong> field value</p>
  <pre><code>POST /my-index/_doc/sp1
  {
    &quot;name&quot;: &quot;Shirt&quot;
  }
  </code></pre>
  <p>Once this document was created, I want to update this document with <strong>cost</strong> value as maximum value of cost in that index (max(cost) * 2). I tried this logic using update API as follows</p>
  <pre><code>POST /my-index/_doc/sp1
  {
    &quot;script&quot; : {
      &quot;source&quot;: &quot;ctx._source.cost = Math.max(doc['cost'].value) * 2&quot;
    }
  }
  </code></pre>
  <p>But I couldn't able to achieve this. Encountered the following error</p>
  <pre><code>&quot;caused_by&quot; : {
          &quot;type&quot; : &quot;illegal_argument_exception&quot;,
          &quot;reason&quot; : &quot;static method [java.lang.Math, max/1] not found&quot;
        }
  
   
  </code></pre>
  <p>How to achieve this scenario</p>