{"record":{"id":"552b8f5a68b17d27","repo":"elastic/elasticsearch","slug":"cannot-apply-operation-to-types-and-552b8f","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":788,"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 boolean gte(int a, int b) {\n        return a >= b;\n    }\n\n    private static boolean gte(long a, long b) {\n        return a >= b;\n    }\n\n    private static boolean gte(float a, float b) {","sourceCodeStart":770,"sourceCodeEnd":806,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/modules/lang-painless/src/main/java/org/elasticsearch/painless/DefMath.java#L770-L806","documentation":"Thrown by DefMath.gt(Object, Object) when the runtime types of both operands do not match any supported greater-than combination. The Object overload checks instanceof Number and instanceof Character; type pairs outside those categories fall through to this ClassCastException. Only triggered with def-typed operands.","triggerScenarios":"A Painless script applies > to two def-typed values where at least one resolves at runtime to a non-numeric, non-character type. The Object overload is invoked via def dispatch and no instanceof branch matches.","commonSituations":"Keyword/text field compared with > against a numeric threshold in a script; def variable holding a parsed JSON value of unexpected type; field that is null or missing, returning a non-numeric default; cross-index search where the field has different mappings.","solutions":["Use explicit numeric types for both operands","Guard with instanceof: if (a instanceof Number && b instanceof Number) { return ((Number)a).doubleValue() > ((Number)b).doubleValue(); }","Fix the mapping or use a multi-field with a numeric sub-field","Check for null/missing fields before comparison: if (doc['field'].size() == 0) return false;"],"exampleFix":"// before\ndef score = doc['rating'].value;\ndef cutoff = params['threshold'];\nboolean pass = score > cutoff;\n\n// after\ndouble score = doc['rating'].value;\ndouble cutoff = (double) params['threshold'];\nboolean pass = score > cutoff;","handlingStrategy":"type-guard","validationCode":"// Type-check def values before > comparison\ndef val = doc['rating'].value;\ndef threshold = params['min'];\nif (val instanceof Number && threshold instanceof Number) {\n  return ((Number) val).doubleValue() > ((Number) threshold).doubleValue();\n}\nreturn false;","typeGuard":"// Guard for def > comparison\nboolean safeGreaterThan(def a, def b) {\n  if (a instanceof Number && b instanceof Number) {\n    return ((Number) a).doubleValue() > ((Number) b).doubleValue();\n  }\n  return false;\n}","tryCatchPattern":null,"preventionTips":["Use explicit numeric types for both comparison operands","Verify field mappings are numeric before writing comparison scripts","Handle missing fields: if (doc['field'].size() == 0) return false;","Avoid def for values used in relational expressions"],"tags":["painless","elasticsearch","def","type-error","comparison","runtime","greater-than"],"backgroundTag":null,"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:31:55.035Z"}