Nate Nate -4 years ago 88
Node.js Question

How to query documents where the query should have an and clause?

How do I use the nodejs driver for elastic search do achieve the following scenario. The scenario is that I have to update all documents in a "title" index where the tvSeries id is given and where the tvSeriesNumber is given?

I only want to match documents that fulfill both queries. I know that I should be using the client.updateByQuery method but I'm not sure what to put for the query body.

Answer Source

How about this? You probably need to modify the script in order to update whatever fields you want to.

client.updateByQuery({ 
       index: "title",
       type: "type",
       body: { 
          "query": { 
             "bool": { 
               "filter": [
                 { "term": { "tvSeriesId": 123} },
                 { "term": { "tvSeriesNumber": 456} }
               ]
             } 
          }, 
          "script": { 
             "inline": "ctx._source.xyz = 'abc'"
          }
       }
    }, function(err, res) { 
        if (err) { 
           reportError(err) 
        } 
        cb(err, res)
    }
)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download