{"record":{"id":"87de186d05a5d750","repo":"elastic/elasticsearch","slug":"cannot-apply-operation-to-type-boolean-87de18","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":241,"sourceCode":"\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) {\n        return a / b;\n    }\n\n    private static double div(double a, double b) {\n        return a / b;\n    }\n\n    private static boolean div(boolean a, boolean b) {\n        throw new ClassCastException(\"Cannot apply [/] operation to type [boolean]\");\n    }\n\n    private static Object div(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":223,"sourceCodeEnd":259,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/modules/lang-painless/src/main/java/org/elasticsearch/painless/DefMath.java#L223-L259","documentation":"Division (/) is defined for all numeric types but not for boolean. DefMath's boolean overload for div() throws a ClassCastException when one or both def operands resolve to Boolean.","triggerScenarios":"A Painless script divides def variables where at least one resolves to Boolean. Example: `def x = true; def y = 2; def z = x / y;`","commonSituations":"A boolean field value is mistakenly included in an arithmetic division expression. Using def-typed variables from heterogeneous document fields where some resolve to Boolean.","solutions":["Ensure both operands are numeric before dividing.","Use an explicit numeric type declaration instead of def.","Guard with instanceof and only divide when both operands are Number."],"exampleFix":"// before\ndef x = doc['flag'].value;\ndef y = doc['divisor'].value;\ndef z = x / y;\n\n// after\ndef x = doc['flag'].value;\ndef y = doc['divisor'].value;\nif (x instanceof Number && y instanceof Number) {\n    def z = (double) x / (double) y;\n}","handlingStrategy":"type-guard","validationCode":"// Painless: verify both operands are numeric 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 numeric operands\ndef bothNumeric(def a, def b) {\n    return a instanceof Number && b instanceof Number;\n}","tryCatchPattern":null,"preventionTips":["Declare arithmetic variables with explicit numeric types instead of def.","Guard division with instanceof checks on both operands.","Verify field mappings are numeric for values used in division."],"tags":["painless","def-type","division","boolean","type-mismatch","arithmetic"],"backgroundTag":null,"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}