简体   繁体   中英

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?

The _id for tId = 28 is 2812 . So:

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

Source: Elasticsearch Reference Documentation

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM