Update or remove element in an array of objects

My script fails with this error
{"type":"class_cast_exception","reason":"java.util.LinkedHashMap cannot be cast to java.lang.Number"}}}}}

,"errors":true,"items":[{"update":{"_index":"user","_type":"user","_id":"59706ad67313e0513619e333","status":400,"error":{"type":"illegal_argument_exception","reason":"failed to execute script","caused_by":{"type":"script_exception","reason":"runtime error","script_stack":["if (ctx._source._dms[i] && ctx._source._dms[i].user == params.dms.user) {\n "," ^---- HERE"],"script":"\n int found = 0;\n for(int i=0; i<ctx._source._dms.length; i++) {\n if (ctx._source._dms[i] && ctx._source._dms[i].user == params.dms.user) {\n ctx._source._dms[i] = params.dms;\n found = 1;\n }\n }\n \n ctx._source._dms.add(params.dms);\n ","lang":"painless","caused_by":{"type":"class_cast_exception","reason":"java.util.LinkedHashMap cannot be cast to java.lang.Number"}}}}},{"update":{"_index":"user","_type":"user","_id":"59706dab7313e0513619e334","status":400,"error":{"^C

    const inline = `
      int found = 0;
      for(int i=0; i<ctx._source._dms.length; i++) {
        if (ctx._source._dms[i] && ctx._source._dms[i].user == params.dms.user) {
          ctx._source._dms[i] = params.dms;
          found = 1;
        }
      }
      
      ctx._source._dms.add(params.dms);
    `

    const body = [{
      update: {
        _index: 'user',
        _type: 'user',
        _id: firstUser.toString(),
        _retry_on_conflict: 10
      }
    }, {
      body: {
        script: {
          inline,
          params: {
            dms: {
              disabled: (disabledUsers || []).map(String).includes(secondUser.toString()),
              tag,
              user: secondUser.toString()
            }
          }
        }
      }
    }, {
      update: {
        _index: 'user',
        _type: 'user',
        _id: secondUser.toString(),
        _retry_on_conflict: 10
      }
    }, {
      body: {
        script: {
          inline,
          params: {
            dms: {
              disabled: (disabledUsers || []).map(String).includes(firstUser.toString()),
              tag,
              user: firstUser.toString()
            }
          }
        }
      }
    }]

Documents have an array of objects under the _dms field

    "_dms" : [
      {
        "disabled" : false,
        "tag" : "5979bc28a55761cfcb2a3a09",
        "user" : "597052c27313e0513619e332"
      }
 curl localhost:9200/user/user/_mapping?pretty
{
  "user" : {
    "mappings" : {
      "user" : {
        "properties" : {
          "_dms" : {
            "properties" : {
              "disabled" : {
                "type" : "boolean"
              },
              "tag" : {
                "type" : "text",
                "fields" : {
                  "keyword" : {
                    "type" : "keyword",
                    "ignore_above" : 256
                  }
                }
              },
              "user" : {
                "type" : "text",
                "fields" : {
                  "keyword" : {
                    "type" : "keyword",
                    "ignore_above" : 256
                  }
                }
              }
            }
          },
...

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.