适用版本: 6.8-8.x
1. 错误说明 #
报错 failed to parse [<type>] action [<watch_id>/<action_id>]. expected [<field>] to be of type string; but found [<token>] instead 表示 Elasticsearch 在解析 Watcher 的某个 action 配置时,发现指定字段必须是字符串,但请求里给了其他 JSON 类型。
附录代码已经很明确:解析器读取到 ACCOUNT 字段后,只接受 VALUE_STRING。如果这里传入的是对象、数组、数字、布尔值或 null,就会直接抛出这个异常。
2. 常见触发场景 #
account字段被误写成对象,而不是账号名称字符串。- 模板渲染后把变量展开成了数组或嵌套 JSON。
- 通过 SDK 或脚本拼接 Watcher 定义时,字段类型和接口要求不一致。
错误示例:
{
"actions": {
"notify": {
"slack": {
"account": {
"name": "team-default"
}
}
}
}
}
这里的 account 应该是字符串,而不是对象。
3. 排查方法 #
- 找到异常中的
action [watch_id/action_id],确认是哪一个 Watcher 动作解析失败。 - 检查该 action 对应字段是否要求字符串,尤其是
account这类标识型字段。 - 查看应用最终发送到 Elasticsearch 的原始 JSON,避免被中间模板或序列化逻辑误导。
- 如果配置来自变量替换,核对变量源类型是否已经从字符串漂移成对象或数组。
4. 修复方法 #
- 把报错字段改回普通字符串值。
- 对 Watcher 配置生成逻辑增加类型校验,禁止把对象直接写进字符串字段。
- 如果确实需要传递复杂结构,应改写到该 action 支持的对象字段,而不是复用字符串字段。
修正示例:
{
"actions": {
"notify": {
"slack": {
"account": "team-default"
}
}
}
}
5. 预防建议 #
- 为 Watcher 配置增加 JSON Schema 或单元测试,提前拦截类型错误。
- 对所有字符串型字段做显式断言,不依赖调用方“默认传对”。
- 在发布前回放典型 Watcher 配置,确认序列化输出与接口约定一致。
相关错误 #
附:日志上下文 #
currentFieldName = parser.currentName();
} else if (Field.ACCOUNT.match(currentFieldName; parser.getDeprecationHandler())) {
if (token == XContentParser.Token.VALUE_STRING) {
account = parser.text();
} else {
throw new ElasticsearchParseException("failed to parse [{}] action [{}/{}]. expected [{}] to be of type string; but " +
"found [{}] instead"; TYPE; watchId; actionId; Field.ACCOUNT.getPreferredName(); token);
}
} else if (Field.PROXY.match(currentFieldName; parser.getDeprecationHandler())) {
proxy = HttpProxy.parse(parser);
} else if (Field.FIELDS.match(currentFieldName; parser.getDeprecationHandler())) {





