如何更新Elasticsearch索引中的特定字段?

时间:2017-10-20 09:36:57

标签: elasticsearch

在Elasticsearch中我有以下数据:

narrow_cast

如果"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" } } 等于Intensity,我如何设置tId等于5?我应该写哪个更新查询?

2 个答案:

答案 0 :(得分:1)

tId = 28的 _id 2812 。所以:

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

来源:Elasticsearch Reference Documentation

答案 1 :(得分:0)

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);
      }
    });

对于启用脚本,您应该在elasticsearch.yaml文件中包含script.inline: true