Karthikeyan Ramasamy
Karthikeyan Ramasamy

Reputation: 31

Elastic Search Partial Update

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.

Upvotes: 0

Views: 1688

Answers (2)

ChintanShah25
ChintanShah25

Reputation: 12672

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

Upvotes: 0

alazaro
alazaro

Reputation: 442

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.

Upvotes: 2

Related Questions