{"record":{"id":"4e682556bc531aaa","repo":"elastic/elasticsearch","slug":"cannot-apply-operation-to-types-and","errorCode":null,"errorMessage":"Cannot apply [*] operation to types [{}] and [{}].","messagePattern":"Cannot apply \\[\\*\\] operation to types \\[(.+?)\\] and \\[(.+?)\\]\\.","errorType":"exception","errorClass":"ClassCastException","httpStatus":null,"severity":"error","filePath":"modules/lang-painless/src/main/java/org/elasticsearch/painless/DefMath.java","lineNumber":214,"sourceCode":"                }\n            }\n        } else if (left instanceof Character) {\n            if (right instanceof Number) {\n                if (right instanceof Double) {\n                    return (char) left * ((Number) right).doubleValue();\n                } else if (right instanceof Long) {\n                    return (char) left * ((Number) right).longValue();\n                } else if (right instanceof Float) {\n                    return (char) left * ((Number) right).floatValue();\n                } else {\n                    return (char) left * ((Number) right).intValue();\n                }\n            } else if (right instanceof Character) {\n                return (char) left * (char) right;\n            }\n        }\n\n        throw new ClassCastException(\n            \"Cannot apply [*] operation to types \"\n                + \"[\"\n                + left.getClass().getCanonicalName()\n                + \"] and [\"\n                + right.getClass().getCanonicalName()\n                + \"].\"\n        );\n    }\n\n    private static int div(int a, int b) {\n        return a / b;\n    }\n\n    private static long div(long a, long b) {\n        return a / b;\n    }\n\n    private static float div(float a, float b) {","sourceCodeStart":196,"sourceCodeEnd":232,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/modules/lang-painless/src/main/java/org/elasticsearch/painless/DefMath.java#L196-L232","documentation":"DefMath.mul(Object, Object) handles multiplication of def values for Number and Character combinations. If neither operand matches any of the Number or Character branches, the method falls through to a terminal ClassCastException naming both operands' actual types.","triggerScenarios":"A Painless script multiplies two def variables whose runtime types are incompatible with multiplication. Example: `def x = 'hello'; def y = 5; def z = x * y;` where x is a String.","commonSituations":"Multiplying a def value that resolved to a String (from a text or keyword field) with another value. Processing fields whose type varies across documents, causing runtime types to diverge from expectations.","solutions":["Cast both operands to compatible numeric types: `def z = (double) x * (double) y;`","Guard with instanceof to ensure both operands are Number or Character before multiplying.","Use explicit types instead of def for arithmetic variables."],"exampleFix":"// before\ndef x = doc['price'].value;\ndef y = doc['qty'].value;\ndef total = x * y;\n\n// after\ndef x = doc['price'].value;\ndef y = doc['qty'].value;\nif (x instanceof Number && y instanceof Number) {\n    def total = (double) x * (double) y;\n}","handlingStrategy":"type-guard","validationCode":"// Painless: verify both operands are Number or Character before multiplication\ndef a = doc['val1'].value;\ndef b = doc['val2'].value;\nif (a instanceof Number && b instanceof Number) {\n    def result = (double) a * (double) b;\n} else {\n    // handle incompatible types\n}","typeGuard":"// Painless type guard for multipliable types\ndef canMultiply(def a, def b) {\n    return (a instanceof Number && b instanceof Number) || (a instanceof Number && b instanceof Character) || (a instanceof Character && b instanceof Number) || (a instanceof Character && b instanceof Character);\n}","tryCatchPattern":null,"preventionTips":["Cast def values to double or long before arithmetic to ensure type compatibility.","Use explicit types instead of def for arithmetic variables.","Check instanceof on both operands before applying binary operators."],"tags":["painless","def-type","multiplication","type-mismatch","arithmetic"],"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T08:17:17.861Z"}