Elastic Search Partial Update

匿名 (未验证) 提交于 2019-12-03 01:23:02

问题:

I want to do a partial update like below . Add some new fields like Bytes_In and Bytes_Out. And also run a script to update a field that is derived from other fields using a script.

Script session-duration-script.groovy is under /config/scripts path.

ctx._source.duration= (new Date().parse("yyyy-MM-dd HH:mm:ss.SSS",ctx._source.sessionTerminationDateTime.replace("T", " ").substring(0,23)).getTime() - new Date().parse("yyyy-MM-dd HH:mm:ss.SSS",ctx._source.eventConversionDateTime.replace("T", " ").substring(0,23)).getTime())   access/access-event-logs/session-summary/0a30fd59karabip1new.lab.fp.f5net.com/_update    {        "doc" : {           "active" : false,           "Bytes_In": "100",           "Bytes_Out": "100",           "sessionTerminationDateTime": "2015-10-30T02:50:39.237Z"        },        "script_fields": {                "my_field": {                    "script_file": "session-duration-script"                  }         }     } 

When i run the above update query ,I get this error

{   "code": 400,   "message": "status:400, body:{\"error\":{\"root_cause\":[{\"type\":\"action_request_validation_exception\",\"reason\":\"Validation Failed: 1: can't provide both script and doc;\"}],\"type\":\"action_request_validation_exception\",\"reason\":\"Validation Failed: 1: can't provide both script and doc;\"},\"status\":400}",   "originalRequestBody": "{\"error\":{\"root_cause\":[{\"type\":\"action_request_validation_exception\",\"reason\":\"Validation Failed: 1: can't provide both script and doc;\"}],\"type\":\"action_request_validation_exception\",\"reason\":\"Validation Failed: 1: can't provide both script and doc;\"},\"status\":400}",   "referer": "172.17.86.67",   "restOperationId": 6555035,   "kind": ":resterrorresponse" } 

Please let me know of there is way to achieve this kind of update.

回答1:

As the error states, you cannot use both doc and script. My suggestion is modifying the script to also add the fields you want, and pass the values of these fields using the params map.



回答2:

To update a document, you can either provide a doc or a script. Also you can't use script_fields like this.

Change your session-duration-script.groovy to this

EDIT : If you want duration to be calculated based on new sessionTerminationDateTime then put the first line at the end (Thanks to @Val)

ctx._source.duration= (new Date().parse("yyyy-MM-dd HH:mm:ss.SSS",ctx._source.sessionTerminationDateTime.replace("T", " ").substring(0,23)).getTime() - new Date().parse("yyyy-MM-dd HH:mm:ss.SSS",ctx._source.eventConversionDateTime.replace("T", " ").substring(0,23)).getTime()); ctx._source.active = active; ctx._source.Bytes_In = Bytes_In;  ctx._source.Bytes_Out = Bytes_Out; ctx._source.sessionTerminationDateTime = sessionTerminationDateTime; 

After that, this is how you can update the doc

POST access-event-logs/session-summary/0a30fd59karabip1new.lab.fp.f5net.com/_update {   "script": {     "file": "session-duration-script",     "params": {       "active": false,       "Bytes_In": "100",       "Bytes_Out": "100",       "sessionTerminationDateTime": "2015-10-30T02:50:39.237Z"     }   } } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!