2017-10-20 56 views
0

在Elasticsearch,我有以下数据:如何更新Elasticsearch索引中的特定字段?

"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" 
     } 
     } 

如何设置Intensityea¡qual至5 tId等于28?我应该写哪个更新查询?

回答

1

对于tId = 28,_id是。所以:

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

来源:Elasticsearch Reference Documentation

+0

但有可能根据'其中TID = 28'更新吗? – Dinosaurius

+0

不,不是开箱即用的。你可以使用这个插件:https://github.com/yakaz/elasticsearch-action-updatebyquery –

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

为使脚本,你应该包括script.inline: true在elasticsearch.yaml文件

+0

启用脚本时要小心:https://www.elastic.co/guide/en/elasticsearch/reference/current/modules -scripting-security.html安全#模块脚本安全-DO-没有削弱 –

相关问题