Dinosaurius
Dinosaurius

Reputation: 8628

How to update particular field in Elasticsearch index?

In Elasticsearch I have the following data:

"hits": [
      {
        "_index": "traffic",
        "_type": "entry",
        "_id": "922",
        "_score": 1,
        "_source": {
          "Intensity": 0,
          "tId": "9"
        }
      },
      {
        "_index": "traffic",
        "_type": "entry",
        "_id": "2812",
        "_score": 1,
        "_source": {
          "Intensity": 0,
          "tId": "28"
        }
      }

How can I set Intensity ea¡qual to 5 for tId equal to 28? Which update query should I write?

Upvotes: 0

Views: 120

Answers (2)

Pratheesh M
Pratheesh M

Reputation: 1078

client.updateByQuery({
      index : 'traffic',
      type : 'entry',
      body : {
        query : {
          match : {
            tId : 28
          }
        },
        script :
        {'inline':
        'ctx._source.Intensity = params["Intensity"];',
        lang   : 'painless',
        params : {Intensity: 5 }
        }
      }
    }, (err, res) => {
      if (err) {
        reject(err);
      } else {
        resolve(res);
      }
    });

For enabling script, you should include script.inline: true in elasticsearch.yaml file

Upvotes: 0

Erdal G.
Erdal G.

Reputation: 2980

The _id for tId = 28 is 2812. So:

POST traffic/entry/2812/_update
{
    "doc" : {
        "Intensity" : 5
    }
}

Source: Elasticsearch Reference Documentation

Upvotes: 1

Related Questions