{"record":{"id":"8e1394b000bf5dd7","repo":"elastic/elasticsearch","slug":"cannot-apply-operation-to-types-and-8e1394","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":283,"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 rem(int a, int b) {\n        return a % b;\n    }\n\n    private static long rem(long a, long b) {\n        return a % b;\n    }\n\n    private static float rem(float a, float b) {","sourceCodeStart":265,"sourceCodeEnd":301,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/modules/lang-painless/src/main/java/org/elasticsearch/painless/DefMath.java#L265-L301","documentation":"DefMath.div(Object, Object) handles division of def values for Number and Character combinations. If neither operand matches any supported branch, the method throws a ClassCastException naming both operands' actual types.","triggerScenarios":"A Painless script divides two def variables whose runtime types are not compatible with division. Example: `def x = 'abc'; def y = 2; def z = x / y;` where x is a String.","commonSituations":"Dividing a def value that resolved to a String from a keyword/text field. Processing dynamically-typed values where the runtime type differs from the expected numeric type.","solutions":["Cast both operands to compatible numeric types: `def z = (double) x / (double) y;`","Guard with instanceof to verify both are Number or Character.","Use explicit types instead of def for arithmetic."],"exampleFix":"// before\ndef total = doc['total'].value;\ndef count = doc['count'].value;\ndef avg = total / count;\n\n// after\ndef total = doc['total'].value;\ndef count = doc['count'].value;\nif (total instanceof Number && count instanceof Number) {\n    def avg = (double) total / (double) count;\n}","handlingStrategy":"type-guard","validationCode":"// Painless: verify both operands are Number or Character before division\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 divisible types\ndef canDivide(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 division.","Use explicit types instead of def for arithmetic variables.","Check instanceof on both operands before applying the / operator."],"tags":["painless","def-type","division","type-mismatch","arithmetic"],"backgroundTag":null,"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}