0

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.

2 Answers2

2

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.

alazaro
  • 442
  • 1
  • 6
  • 17
0

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"
    }
  }
}
ChintanShah25
  • 12,366
  • 3
  • 43
  • 44
  • You probably need to set the `ctx._source.sessionTerminationDateTime` variable **before** computing `ctx._source.duration`, i.e. move the fifth line of the script to the first position, since the computation of the duration uses that value. – Val Jan 08 '16 at 04:49
  • Thanks @Val, did not realize `duration` is using that particular field, I will edit the answer – ChintanShah25 Jan 08 '16 at 14:33