title: Elasticsearch upserting and appending to array
body: <p>I'm trying to write a script that will upsert a new user record to ElasticSearch, updating any information if the user already exists, and appending a new PaymentInfo object to the user's Payments array if it exists in the update object. Here's a simplified version of what I'm working with so far:</p>
  
  <pre><code>curl -XPOST 'http://localhost:9200/usrtest/usr/1/_update' -d '
  {
      "doc_as_upsert": true, 
      "doc": {
          "customerId": "1",
          "firstName": "Mark",
          "lastName": "Z",
          "emailAddress": "foo.bar@gmail.com",
          "paymentInfo": {
              "pid": "1",
              "amt": "10"
          }
      }
  }'
  </code></pre>
  
  <p>This almost does what I want in that it inserts the doc properly, or updates the doc if a user exists with the same ID, but it's missing the aspect to add this paymentInfo to the user's paymentInfos array if the user exists already. As it is right now, it just overrides the paymentInfo object. I've tried adding this script to the update JSON:</p>
  
  <pre><code>"script": "if (ctx._source.containsKey(\"paymentInfos\")) {ctx._source.paymentInfos += paymentInfo;} else {ctx._source.paymentInfos = paymentInfo}"
  </code></pre>
  
  <p>but elasticsearch ignores <code>doc</code> elements when the <code>script</code> element is specified. </p>
  
  <p>I feel like I'm missing something silly here, but I'm not sure. Can anyone here help me out?</p>
  
  <p><strong>Edit:</strong>
  I've tried the following as well:</p>
  
  <pre><code>curl -XPOST 'http://localhost:9200/usrtest/usr/1/_update' -d '
  {    
      "script": "if (ctx._source.containsKey(\"paymentInfos\")) {ctx._source.paymentInfos += paymentInfo;} else {ctx._source.paymentInfos = paymentInfo}",
      "upsert": {
          "customerId": "1",
          "firstName": "Mark",
          "lastName": "Z",
          "emailAddress": "foo.bar@gmail.com",
          "paymentInfo": {
              "pid": "1",
              "amt": "10"
           }
      },
      "params": {
           "paymentInfo": {
              "pid": "1",
                  "amt": "10"
           }
      }
  }'
  </code></pre>
  
  <p>Which also <em>almost</em> does what I want it to, in that it appends the paymentInfo objects when I run the script several times, but otherwise it doesn't update the document itself (i.e. if I run the script again, changing Mark to Mindy, it doesn't update since <code>upsert</code> elements are only used if the doc doesn't exist already).</p>