{"record":{"id":"53df9b0eb4fd1cf2","repo":"elastic/elasticsearch","slug":"cannot-apply-operation-to-type-boolean-53df9b","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":130,"sourceCode":"\n    private static int plus(int v) {\n        return +v;\n    }\n\n    private static long plus(long v) {\n        return +v;\n    }\n\n    private static float plus(float v) {\n        return +v;\n    }\n\n    private static double plus(double v) {\n        return +v;\n    }\n\n    private static boolean plus(boolean v) {\n        throw new ClassCastException(\"Cannot apply [+] operation to type [boolean]\");\n    }\n\n    private static Object plus(final Object unary) {\n        if (unary instanceof Double) {\n            return +(double) unary;\n        } else if (unary instanceof Long) {\n            return +(long) unary;\n        } else if (unary instanceof Integer) {\n            return +(int) unary;\n        } else if (unary instanceof Float) {\n            return +(float) unary;\n        } else if (unary instanceof Short) {\n            return +(short) unary;\n        } else if (unary instanceof Character) {\n            return +(char) unary;\n        } else if (unary instanceof Byte) {\n            return +(byte) unary;\n        }","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/modules/lang-painless/src/main/java/org/elasticsearch/painless/DefMath.java#L112-L148","documentation":"Unary plus (+) is defined for all numeric types but not for boolean. DefMath's boolean overload for plus() throws a ClassCastException when a def variable resolves to Boolean and the unary + operator is applied.","triggerScenarios":"A Painless script applies unary + to a def variable holding a Boolean. Example: `def x = true; def y = +x;`","commonSituations":"Rarely intentional; usually results from a def variable whose type was inferred from runtime data that happened to be a boolean flag, combined with an expression that uses unary + as a no-op coercion.","solutions":["Verify the operand is numeric before applying unary +.","Use an explicit numeric type declaration instead of def.","Remove the unary + if it was not intended, or guard with instanceof."],"exampleFix":"// before\ndef x = doc['flag'].value;\ndef y = +x;\n\n// after\ndef x = doc['flag'].value;\nif (x instanceof Number) {\n    def y = +(int) x;\n}","handlingStrategy":"type-guard","validationCode":"// Painless: ensure the value is numeric before unary plus\ndef x = doc['flag'].value;\nif (x instanceof Number) {\n    def y = +(int) x;\n}","typeGuard":"// Painless type guard for numeric types\ndef isNumeric(def value) {\n    return value instanceof Number;\n}","tryCatchPattern":null,"preventionTips":["Declare numeric variables with explicit types instead of def.","Remove unnecessary unary + operators, or guard them with instanceof checks.","Verify document field mappings are numeric, not boolean, for values used in arithmetic."],"tags":["painless","def-type","unary","boolean","type-mismatch","arithmetic"],"backgroundTag":null,"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:31:55.035Z"}