2017-10-20 93 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-沒有削弱 –

相關問題