Nate Nate -4 years ago 207
Node.js Question

How do you pass arguments to elasticsearch inline scripts using the nodejs driver?

The situation I want to solve is to update documents in a titles index with titles matching certain parameters using the nodejs driver for elasticsearch. The current query I'm using is this

client.updateByQuery({
index: "title",
type: "type",
body: {
"query": {
"bool": {
"filter": [
{ "term": { "tvSeriesId": 123} },
{ "term": { "tvSeriesNumber": 456} }
]
}
},
"script": {
"inline": "ctx._source.someRandomField = 'abc'"
}
}
}, function(err, res) {
if (err) {
reportError(err)
}
cb(err, res)
}
)


I want the script to be dynamic where I can pass a value to the script to change the value of a field. For example, the value of someRandomField could be declared in a previous variable before the query call. How can i pass that variable to the script?

Answer Source

You can use the script params to do this:

client.updateByQuery({ 
       index: "title",
       type: "type",
       body: { 
          "query": { 
             "bool": { 
               "filter": [
                 { "term": { "tvSeriesId": 123} },
                 { "term": { "tvSeriesNumber": 456} }
               ]
             } 
          }, 
          "script": { 
             "inline": "ctx._source.someRandomField = newValue"
             "params": {
                "newValue": newRandomFieldValue
             }
          }
       }
    }, function(err, res) { 
        if (err) { 
           reportError(err) 
        } 
        cb(err, res)
    }
)

newRandomFieldValue would be a variable in your Node.js code that is then used as a script parameter.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download