3

I am using the below code to update a document in elasticsearch

client.update({
   index: 'myindex',
   type: 'mytype',
   id: '1',
   body: {
     script: 'ctx._source.tags += tag',
     params: { tag: 'some new tag' }
   }
}, function (error, response) {

});

However, this throws a compile error. When I replace tag by params.tag above, it appends null to the present field i.e. identifies params.tag as null.

blo0p3r
  • 6,790
  • 8
  • 49
  • 68

1 Answers1

0

I encountered a similar confusion. This document shares the example you have in your question. On the other hand, this document shares a different example (which I got to work).

Your update statement would be something like this :

client.update({
    index: 'myindex',
    type: 'mytype',
    id: '1',
    body: {
        script: {
            inline: 'ctx._source.tags.add(params.tag)',
            lang: 'painless',
            params: { 
                tag: 'some new tag'
            }
        }
    }
}, function (error, response) {
    // handling of error/response
});

** Notice the .add instead of the += for the case when adding to an array.

blo0p3r
  • 6,790
  • 8
  • 49
  • 68