{"record":{"id":"3be10f7be82b1f03","repo":"elastic/elasticsearch","slug":"cannot-apply-operation-to-type-3be10f","errorCode":null,"errorMessage":"Cannot apply [+] operation to type [{}].","messagePattern":"Cannot apply \\[\\+\\] operation to type \\[(.+?)\\]\\.","errorType":"exception","errorClass":"ClassCastException","httpStatus":null,"severity":"error","filePath":"modules/lang-painless/src/main/java/org/elasticsearch/painless/DefMath.java","lineNumber":150,"sourceCode":"\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        }\n\n        throw new ClassCastException(\"Cannot apply [+] operation to type \" + \"[\" + unary.getClass().getCanonicalName() + \"].\");\n    }\n\n    // multiplication/division/remainder/subtraction: applicable to all integer types\n\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;","sourceCodeStart":132,"sourceCodeEnd":168,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/modules/lang-painless/src/main/java/org/elasticsearch/painless/DefMath.java#L132-L168","documentation":"DefMath.plus(Object) is the catch-all for unary plus on def values. It accepts boxed numeric types (Double, Long, Integer, Float, Short, Character, Byte). If the runtime type matches none of these, it throws a ClassCastException naming the actual type.","triggerScenarios":"A Painless script applies unary + to a def variable whose runtime type is not a recognized boxed numeric type. Example: `def x = new BigInteger('10'); def y = +x;`","commonSituations":"Applying unary + to a def value that resolved to a String, BigInteger, or BigDecimal from a document field or Java library call.","solutions":["Convert to a numeric type first: `def y = +(double) x;`","Guard with instanceof to verify the value is a recognized Number.","Use explicit types instead of def."],"exampleFix":"// before\ndef x = doc['value'].value;\ndef y = +x;\n\n// after\ndef x = doc['value'].value;\nif (x instanceof Number) {\n    def y = +(double) x;\n}","handlingStrategy":"type-guard","validationCode":"// Painless: check the value is a recognized Number before unary plus\ndef x = doc['value'].value;\nif (x instanceof Number) {\n    def y = +(double) x;\n}","typeGuard":"// Painless type guard for supported numeric box types\ndef isRecognizedNumber(def value) {\n    return value instanceof Double || value instanceof Long || value instanceof Integer || value instanceof Float || value instanceof Short || value instanceof Character || value instanceof Byte;\n}","tryCatchPattern":null,"preventionTips":["Convert non-standard types (String, BigInteger) to double/long before arithmetic.","Use explicit types instead of def for arithmetic variables.","Check instanceof before applying unary + to def-typed values."],"tags":["painless","def-type","unary","type-mismatch","arithmetic"],"backgroundTag":null,"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}