{"record":{"id":"6f3ed3be177a22e9","repo":"elastic/elasticsearch","slug":"cannot-apply-operation-to-type-boolean-6f3ed3","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":746,"sourceCode":"\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) {\n        return a > b;\n    }\n\n    private static boolean gt(double a, double b) {\n        return a > b;\n    }\n\n    private static boolean gt(boolean a, boolean b) {\n        throw new ClassCastException(\"Cannot apply [>] operation to type [boolean]\");\n    }\n\n    private static boolean gt(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":728,"sourceCodeEnd":764,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/modules/lang-painless/src/main/java/org/elasticsearch/painless/DefMath.java#L728-L764","documentation":"Thrown by DefMath.gt(boolean, boolean) which unconditionally throws. When both operands are statically typed boolean, binary promotion yields boolean.class and the compiler dispatches to this overload. Greater-than has no defined semantics for booleans, so the method always raises this exception.","triggerScenarios":"A Painless script applies the > operator to two operands both statically typed as boolean. The promote() method returns boolean.class for (boolean, boolean), selecting the gt(boolean, boolean) overload.","commonSituations":"Ranking or sorting documents by a boolean field using >; comparing two boolean flags for precedence; copy-pasted numeric comparison logic applied to boolean fields; script_score or runtime field using > on boolean doc values.","solutions":["Replace > with a boolean-appropriate operator: == , != , or conditional logic","Map booleans to integers (true=1, false=0) if numeric ordering is genuinely needed","Re-evaluate whether the field should be boolean or a numeric ranking/scoring field"],"exampleFix":"// before\nboolean premium = doc['is_premium'].value;\nboolean trial = doc['is_trial'].value;\nboolean higher = premium > trial;\n\n// after\nint premiumRank = doc['is_premium'].value ? 1 : 0;\nint trialRank = doc['is_trial'].value ? 1 : 0;\nboolean higher = premiumRank > trialRank;","handlingStrategy":"type-guard","validationCode":"// Convert booleans to integers before using >\nboolean a = doc['flag1'].value;\nboolean b = doc['flag2'].value;\nint ia = a ? 1 : 0;\nint ib = b ? 1 : 0;\nreturn ia > ib;","typeGuard":"// Check that value is not boolean before relational comparison\nboolean isRelationalType(Object v) {\n  return v instanceof Number || v instanceof Character;\n}","tryCatchPattern":null,"preventionTips":["Never apply > to boolean operands — use == or != instead","If boolean ordering is required, convert to integers first","Audit script_score and runtime field scripts for boolean/numeric operator mismatches","Use explicit type declarations to leverage compile-time checking"],"tags":["painless","elasticsearch","boolean","type-error","comparison","runtime","greater-than"],"backgroundTag":null,"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}