title: elasticsearch:use script to update nested field?
body: <p>I want to add a object into the <code>nested</code> field every update time.</p>
  
  <p>For example,I have a doc:</p>
  
  <pre><code>{
      "test":[{"remark":"remark1"}]
  }
  </code></pre>
  
  <p>Next time,i want to add a <code>remark</code> object into test field and save the old <code>remark</code> objects.And the result is :</p>
  
  <pre><code>{
      "test":[{"remark":"remark1"},{"remark":"remark2"}]
  }
  </code></pre>
  
  <p>How to achieve it？</p>
  
  <p><strong>Edit</strong>
  I use the script:</p>
  
  <pre><code>{
      "script": "ctx._source.test= ((ctx._source.test?: []) += remarkItem)",
      "params": {
          "remarkItem": {
              "remark": "addd"
          }
      }
  }
  </code></pre>
  
  <p>But,i get the exception:</p>
  
  <pre><code>{
  "error": {
      "root_cause": [
          {
              "type": "remote_transport_exception",
              "reason": "[es77][10.14.84.77:9300][indices:data/write/update[s]]"
          }
      ],
      "type": "illegal_argument_exception",
      "reason": "failed to execute script",
      "caused_by": {
          "type": "script_exception",
          "reason": "Failed to compile inline script [ctx._source.test= ((ctx._source.test?: []) += remarkItem)] using lang [groovy]",
          "caused_by": {
              "type": "script_exception",
              "reason": "failed to compile groovy script",
              "caused_by": {
                  "type": "multiple_compilation_errors_exception",
                  "reason": "startup failed:\na8220b2cf14b8b7ebeead7f068416882d04fa25d: 1: \nclass org.codehaus.groovy.ast.expr.ElvisOperatorExpression, with its value '(ctx._source.test) ? ctx._source.test: []', is a bad expression as the left hand side of an assignment operator at line: 1 column: 82. File: a8220b2cf14b8b7ebeead7f068416882d04fa25d @ line 1, column 82.\n   CILastCallResultRemark ?: []) += remarkI\n                                 ^\n\n1 error\n"
              }
          }
      }
  },
  "status": 400
  }
  </code></pre>
  
  <p><strong>edit</strong></p>
  
  <p>Now,i want to add a field to ensure update or insert the object.
  For example:</p>
  
  <pre><code>{
      "test":[{"remark":"remark1","id":"1"}]
  }
  </code></pre>
  
  <p>When i update the field,when the id exist,i will update the object.On the contrary,i will insert the object.</p>