欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Elastic Search Update报错Validation Failed: 1: can't provide both script and doc,已解决

程序员文章站 2022-07-05 07:58:46
...

报错信息:

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

解决方案:

方案1:

POST /index/type/id/_update
{
  "script" :{
"source":"ctx._source.liked_count+=params.param1
;ctx._source.update_date=params.param2
;ctx._source.update_timestamp=params.param3",
   "lang": "painless",
   "params": {
    "param1":1,
    "param2":"2020-04-23 15:21:36",
    "param3":"1587626496842"
    }
  } 
}

方案2:

POST /index/type/id/_update
{
  "script": 
    "ctx._source.param1+=1
    ;ctx._source.param2='2020-04-23 15:21:36'
    ;ctx._source.param3='1587626496842'"
}

java代码:

UpdateRequest updateRequest = new UpdateRequest(index, type, id);
//冲突重试设置
updateRequest.retryOnConflict(3);
//拼接script脚本信息,注意:字符串信息需要加引号
StringBuffer script = new StringBuffer();
script.append("ctx._source.param1 +=" + value1);//局部更新字段
script.append(";ctx._source.param1 = '" + value2 + "'"); //更新字段
script.append(";ctx._source.param1 = '" + value3 + "'");
updateRequest.script(new Script(script.toString()));
esClient.update(updateRequest, RequestOptions.DEFAULT);

 

相关标签: elasticsearch