弹性搜索部分更新

时间:2016-01-07 22:56:01

标签: elasticsearch

我想进行如下的部分更新。添加一些新字段,如Bytes_In和Bytes_Out。并运行脚本以使用脚本更新从其他字段派生的字段。

脚本session-duration-script.groovy位于/ config / scripts路径下。

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

当我运行上面的更新查询时,我收到此错误

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

请让我知道有办法实现这种更新。

2 个答案:

答案 0 :(得分:1)

如错误所述,您不能同时使用docscript。我的建议是修改脚本以添加所需的字段,并使用params地图传递这些字段的值。

答案 1 :(得分:0)

update文档,您可以提供doc a script。此外,您无法像这样使用script_fields

将您的session-duration-script.groovy更改为此

编辑:如果您希望根据新duration计算sessionTerminationDateTime,请将第一行放在最后(感谢@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;

之后,您可以通过以下方式更新文档

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