适用版本: 6.8-8.x
1. 错误说明 #
报错 failed to parse indices privileges for role [<role>]. expected field [<field>] value to be an array; but found [<token>] instead 表示角色定义中的 indices 权限块,在当前位置必须是数组,但请求里给了其他 JSON 类型。
从附录逻辑可以看到,parseIndices 入口首先检查当前 token 是否为 START_ARRAY。只要不是数组开头,就会立即报错。
2. 常见触发场景 #
- 把
indices直接写成对象,而不是对象数组。 - 用 YAML 转 JSON 时层级缩进错误,导致数组被转成单个对象。
- 程序序列化角色定义时,针对单元素场景自动“去数组化”。
错误示例:
{
"indices": {
"names": ["logs-*"],
"privileges": ["read"]
}
}
3. 排查方法 #
- 查看角色定义中
indices字段的最终 JSON 结构。 - 确认该字段是否是数组
[],而不是对象{}或字符串。 - 如果配置来自 SDK 或配置中心,检查序列化后的请求体,而不是只看源码对象。
- 若是从旧版本模板迁移过来,确认字段格式是否仍符合当前版本要求。
4. 修复方法 #
- 把
indices改为数组,数组中的每一项才是单个索引权限对象。 - 不要依赖框架的自动类型转换;在发送前显式校验 JSON 结构。
修正示例:
{
"indices": [
{
"names": ["logs-*"],
"privileges": ["read"]
}
]
}
5. 预防建议 #
- 为角色配置增加 JSON Schema 校验,明确
indices必须是数组。 - 在 CI 中保留一个最小角色定义回放测试,避免模板修改后破坏结构。
- 对单元素数组不要做“简化输出”,否则容易和 API 规范冲突。
相关错误 #
附:日志上下文 #
} private static RoleDescriptor.IndicesPrivileges[] parseIndices(String roleName; XContentParser parser;
boolean allow2xFormat) throws IOException {
if (parser.currentToken() != XContentParser.Token.START_ARRAY) {
throw new ElasticsearchParseException("failed to parse indices privileges for role [{}]. expected field [{}] value " +
"to be an array; but found [{}] instead"; roleName; parser.currentName(); parser.currentToken());
}
Listprivileges = new ArrayList<>();
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
privileges.add(parseIndex(roleName; parser; allow2xFormat));





