{"record":{"id":"8cbacd20d13c3f10","repo":"elastic/elasticsearch","slug":"cannot-apply-operation-to-type-boolean-8cbacd","errorCode":null,"errorMessage":"Cannot apply [*] operation to type [boolean]","messagePattern":"Cannot apply \\[\\*\\] operation to type \\[boolean\\]","errorType":"exception","errorClass":"ClassCastException","httpStatus":null,"severity":"error","filePath":"modules/lang-painless/src/main/java/org/elasticsearch/painless/DefMath.java","lineNumber":172,"sourceCode":"\n    private static int mul(int a, int b) {\n        return a * b;\n    }\n\n    private static long mul(long a, long b) {\n        return a * b;\n    }\n\n    private static float mul(float a, float b) {\n        return a * b;\n    }\n\n    private static double mul(double a, double b) {\n        return a * b;\n    }\n\n    private static boolean mul(boolean a, boolean b) {\n        throw new ClassCastException(\"Cannot apply [*] operation to type [boolean]\");\n    }\n\n    private static Object mul(Object left, Object right) {\n        if (left instanceof Number) {\n            if (right instanceof Number) {\n                if (left instanceof Double || right instanceof Double) {\n                    return ((Number) left).doubleValue() * ((Number) right).doubleValue();\n                } else if (left instanceof Float || right instanceof Float) {\n                    return ((Number) left).floatValue() * ((Number) right).floatValue();\n                } else if (left instanceof Long || right instanceof Long) {\n                    return ((Number) left).longValue() * ((Number) right).longValue();\n                } else {\n                    return ((Number) left).intValue() * ((Number) right).intValue();\n                }\n            } else if (right instanceof Character) {\n                if (left instanceof Double) {\n                    return ((Number) left).doubleValue() * (char) right;\n                } else if (left instanceof Long) {","sourceCodeStart":154,"sourceCodeEnd":190,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/modules/lang-painless/src/main/java/org/elasticsearch/painless/DefMath.java#L154-L190","documentation":"Multiplication (*) is defined for all numeric types but not for boolean. DefMath's boolean overload for mul() throws a ClassCastException when one or both def operands resolve to Boolean at runtime.","triggerScenarios":"A Painless script multiplies two def variables where at least one resolves to a Boolean. Example: `def x = true; def y = 5; def z = x * y;`","commonSituations":"A script receives a boolean field value and mistakenly uses it in an arithmetic expression. Mixing logical flags with numeric calculations in def-typed code.","solutions":["Ensure both operands are numeric before multiplying.","Use an explicit numeric type declaration instead of def.","Guard with instanceof and branch to handle boolean vs numeric cases."],"exampleFix":"// before\ndef x = doc['active'].value;\ndef rate = doc['rate'].value;\ndef result = x * rate;\n\n// after\ndef x = doc['active'].value;\ndef rate = doc['rate'].value;\nif (x instanceof Number) {\n    def result = (double) x * (double) rate;\n}","handlingStrategy":"type-guard","validationCode":"// Painless: verify both operands are numeric 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}","typeGuard":"// Painless type guard for numeric operands\ndef bothNumeric(def a, def b) {\n    return a instanceof Number && b instanceof Number;\n}","tryCatchPattern":null,"preventionTips":["Declare arithmetic variables as int, long, double, or float instead of def.","Guard binary operators on def values with instanceof checks on both operands.","Verify field mappings are numeric for values used in arithmetic expressions."],"tags":["painless","def-type","multiplication","boolean","type-mismatch","arithmetic"],"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:17:08.281Z"}