ES更新資料
阿新 • • 發佈:2018-12-09
最近專案中用到了ES,有這麼一個場景:索引中有個nested結構,要更新nested中的值,如果document沒有,需要建立document的,並且要批量,這裡的批量指兩個: 1,一次更新(upinsert)多個document 2、一次更新nested中的多個值 這是公司一個ES的專家給我寫的,特此記錄一下:
POST ptp_tags/item/_bulk
{ "update" : { "_id" : "70000004"}, "retry_on_conflict":3 }
{ "script" : { "inline": "ctx._source.object_list.removeIf((item) -> params.objects.stream().anyMatch((object)->object.object_id.equals(item.object_id)) );if (params.deleted != true) {ctx._source.object_list.addAll(params.objects);}" , "lang" : "painless", "params" : {"deleted":false, "objects" : [{"object_id":"p70007","type":1,"object_value":["2005"],"expire_time":1535804643718,"object_value_num":2005.0},{"object_id":"p70008","type":1,"object_value":["2005"],"expire_time":1535804643718,"object_value_num":2006.0}] } }, "upsert":{"product_type":0,"object_list" :[{"object_id":"p70007","type":1,"object_value":["2005"],"expire_time":1535804643718,"object_value_num":2005.0},{"object_id":"p70008","type":1,"object_value":["2005"],"expire_time":1535804643718,"object_value_num":2006.0}]}}
如果有多行,可以繼續新增。。。。
mapping 結構如下:
"mappings" : {
"item" : {
"dynamic" : "false" ,
"include_in_all" : false,
"properties" : {
"_source_" : {
"type" : "keyword"
},
"id" : {
"type" : "long"
},
"object_list" : {
"type" : "nested",
"properties" : {
"expire_time" : {
"type" : "date",
"format" : "yyyy-MM-dd'T'HH:mm:ssZ||yyyy-MM-dd HH:mm:ss||yyyy-MM-dd HH:mm:ss'.0'||yyyy-MM-dd||epoch_millis"
},
"object_id" : {
"type" : "keyword"
},
"object_type" : {
"type" : "keyword"
},
"object_value" : {
"type" : "keyword"
},
"object_value_num" : {
"type" : "double"
}
}
},
"product_type" : {
"type" : "keyword"
}
}
}
}