title: Updating a nested object in elasticsearch
body: <p>I have the below mapping for an index</p>
  
  <pre><code>{
    "mappings": {
      "xxxxx": {
        "properties": {
          "ID": {
            "type": "text"
          },
          "pairs": {
            "type": "nested"
          },
          "xxxxx": {
            "type": "text"
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>the <code>pairs</code> field is an array of objects with the below structure</p>
  
  <pre><code>{
      "id": "",
      "Answer": "",
      "Question": []
  }
  </code></pre>
  
  <p>What i'm trying to do is retrieve a particular nested object and update it. I've tried the <code>updateByQuery</code> method with both partial doc/ script but i'm unable to update</p>
  
  <p><strong>script</strong></p>
  
  <pre><code>var theScript = {
      "inline": "ctx._source.Answer = 'Elastic search update Test'"
  }
  
  client.updateByQuery({
      index: 'sample',
      _source: false,
      body: {
          query: {
              bool: {
                  must: [
                      {
                          "match": {
                              "ID": '2rXdCf5OM9g1ebPNFdZNqW'
                          }
                      },
                      {
                          "nested": {
                              "path": "pairs",
                              "query": {
                                  "match": {
                                      "pairs.id": "c1vNGnnQLuk"
                                  }
                              },
                              "inner_hits": {}
                          }
                      }
                  ]
              }
          },
          "script": theScript
      }
  },function(err, body){
      if(err){
          throw err;
      }
      console.log('body: ', body)
      console.log('data: ', body.hits.hits)
  })
  </code></pre>
  
  <p><strong>partial doc</strong></p>
  
  <pre><code>client.updateByQuery({
      index: 'sample',
      _source: false,
      body: {
          query: {
              bool: {
                  must: [
                      {
                          "match": {
                              "ID": '2rXdCf5OM9g1ebPNFdZNqW'
                          }
                      },
                      {
                          "nested": {
                              "path": "pairs",
                              "query": {
                                  "match": {
                                      "pairs.id": "c1vNGnnQLuk"
                                  }
                              },
                              "inner_hits": {}
                          }
                      }
                  ]
              }
          },
         "doc": {
          "Answer": 'Elastic search update Test'
         }
      }
  },function(err, body){
      if(err){
          throw err;
      }
      console.log('body: ', body)
      console.log('data: ', body.hits.hits)
  })
  </code></pre>
  
  <p>I'm getting the below error:</p>
  
  <p><strong>partial update</strong></p>
  
  <pre><code>Error: [parsing_exception] Unknown key for a START_OBJECT in [doc]., with { line=1 &amp; col=191 }
  </code></pre>
  
  <p><strong>script</strong></p>
  
  <pre><code>Error: [illegal_argument_exception] [sample][conversation][AWa-p9zBTJHq-_gvo-Af] didn't store _source
  </code></pre>
  
  <p><strong>NOTE</strong> I would ideally like to use a partial doc update for this because the nested object is a bit complex and it wouldnt be possible to write an inline script</p>
  
  <p>Elasticsearch version - 5.6</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>doing something like this works</p>
  
  <pre><code>{
      "script" : {
          "inline": "ctx._source.pairs= params.pairs",
          "lang": "painless",
          "params" : {
              "pairs" : [{...}, {...}, ...]
          }
      }
  }
  </code></pre>
  
  <p>but this essentially means that everytime i update , i'm rewriting the entire <code>pairs</code> field (even though i only update one object in array) - this doesnt look ideal to me or is it fine?</p>