{"record":{"id":"37f57f40f9f72fbb","repo":"elastic/elasticsearch","slug":"cannot-apply-operation-to-types-and-37f57f","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":719,"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 gt(int a, int b) {\n        return a > b;\n    }\n\n    private static boolean gt(long a, long b) {\n        return a > b;\n    }\n\n    private static boolean gt(float a, float b) {","sourceCodeStart":701,"sourceCodeEnd":737,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/modules/lang-painless/src/main/java/org/elasticsearch/painless/DefMath.java#L701-L737","documentation":"Thrown by DefMath.lte(Object, Object) when the runtime types of both operands do not match any supported less-than-or-equal combination. The Object overload dispatches through instanceof Number and instanceof Character checks; unrecognized type pairs 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 (String, List, Map, null). The Object overload is invoked and no instanceof branch matches.","commonSituations":"Field mapped as keyword/text used in a <= threshold check; def variable from an untyped source (params map, _source parsing) compared numerically; heterogeneous documents where field type varies; comparing a date-formatted string against a numeric epoch.","solutions":["Use explicit numeric types for both operands to get compile-time type checking","Add an instanceof Number guard: if (a instanceof Number && b instanceof Number) { return ((Number)a).doubleValue() <= ((Number)b).doubleValue(); }","Fix the mapping to use a consistent numeric type for the field","If comparing date strings, parse to long epoch millis first using Long.parseLong() or a date formatter"],"exampleFix":"// before\ndef val = doc['price'].value;\ndef cap = params['max'];\nboolean ok = val <= cap;\n\n// after\ndouble val = doc['price'].value;\ndouble cap = (double) params['max'];\nboolean ok = val <= cap;","handlingStrategy":"type-guard","validationCode":"// Guard def comparison with type checks\ndef val = doc['score'].value;\ndef limit = params['cap'];\nif (val instanceof Number && limit instanceof Number) {\n  return ((Number) val).doubleValue() <= ((Number) limit).doubleValue();\n}\nreturn false;","typeGuard":"// Check numeric compatibility before <= comparison\nboolean bothNumeric(def a, def b) {\n  return a instanceof Number && b instanceof Number;\n}","tryCatchPattern":null,"preventionTips":["Declare both operands with explicit numeric types instead of def","Verify index mappings are consistent across all indices involved in the search","Check for missing/null fields: if (doc['field'].size() == 0) return false;","Test scripts against representative documents before production deployment"],"tags":["painless","elasticsearch","def","type-error","comparison","runtime","less-than-equal"],"backgroundTag":null,"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:31:55.035Z"}