You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Elasticsearch 16.7.3中用update_by_query向数组加对象报错求助

问题解决:Elasticsearch updateByQuery 中的 parsing_exception 错误

错误原因分析

你遇到的parsing_exception是因为两个核心问题:

  • params位置错误:在Elasticsearch的updateByQuery请求体中,params必须嵌套在script对象内部,不能和script同级。
  • card参数格式错误:你用模板字符串把card属性拼接成了字符串,Elasticsearch无法将其识别为可添加到数组的对象,应该直接传入完整的card对象。

修正后的TypeScript代码

const client: Client = await this.elastic.client;
let result: CardDto = card;

const response = await client.updateByQuery({
    index: "index",
    type: "type",
    body: {
        "query": { "match": { "id": idUser }},
        "script": {
            "inline": "ctx._source.cards.add(params.card)",
            "params": {
                "card": card // 直接传入card对象,无需字符串拼接
            }
        }
    }
});

if (response) {
    console.log(response);
}
return result;

关键修改说明

  • 将params移动到script对象内部,符合Elasticsearch的请求体结构规范。
  • 脚本中的变量引用从card改为params.card,确保正确读取参数。
  • 直接传入card对象作为参数,避免字符串拼接导致的类型错误,Elasticsearch会自动处理对象的序列化。

内容的提问来源于stack exchange,提问作者Reut

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 00:54:56