elastic/elasticsearch · error · ClassCastException

Cannot apply [>] operation to type [boolean]

Error message

Cannot apply [>] operation to type [boolean]

What it means

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.

Source

Thrown at modules/lang-painless/src/main/java/org/elasticsearch/painless/DefMath.java:746

    private static boolean gt(int a, int b) {
        return a > b;
    }

    private static boolean gt(long a, long b) {
        return a > b;
    }

    private static boolean gt(float a, float b) {
        return a > b;
    }

    private static boolean gt(double a, double b) {
        return a > b;
    }

    private static boolean gt(boolean a, boolean b) {
        throw new ClassCastException("Cannot apply [>] operation to type [boolean]");
    }

    private static boolean gt(Object left, Object right) {
        if (left instanceof Number) {
            if (right instanceof Number) {
                if (left instanceof Double || right instanceof Double) {
                    return ((Number) left).doubleValue() > ((Number) right).doubleValue();
                } else if (left instanceof Float || right instanceof Float) {
                    return ((Number) left).floatValue() > ((Number) right).floatValue();
                } else if (left instanceof Long || right instanceof Long) {
                    return ((Number) left).longValue() > ((Number) right).longValue();
                } else {
                    return ((Number) left).intValue() > ((Number) right).intValue();
                }
            } else if (right instanceof Character) {
                if (left instanceof Double) {
                    return ((Number) left).doubleValue() > (char) right;
                } else if (left instanceof Long) {

View on GitHub (pinned to db6a809a66)

Solutions

  1. Replace > with a boolean-appropriate operator: == , != , or conditional logic
  2. Map booleans to integers (true=1, false=0) if numeric ordering is genuinely needed
  3. Re-evaluate whether the field should be boolean or a numeric ranking/scoring field

Example fix

// before
boolean premium = doc['is_premium'].value;
boolean trial = doc['is_trial'].value;
boolean higher = premium > trial;

// after
int premiumRank = doc['is_premium'].value ? 1 : 0;
int trialRank = doc['is_trial'].value ? 1 : 0;
boolean higher = premiumRank > trialRank;
Defensive patterns

Strategy: type-guard

Validate before calling

// Convert booleans to integers before using >
boolean a = doc['flag1'].value;
boolean b = doc['flag2'].value;
int ia = a ? 1 : 0;
int ib = b ? 1 : 0;
return ia > ib;

Type guard

// Check that value is not boolean before relational comparison
boolean isRelationalType(Object v) {
  return v instanceof Number || v instanceof Character;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of elastic/elasticsearch@db6a809a66 (2026-08-12). Data as JSON: /api/errors/6f3ed3be177a22e9. Report an issue: GitHub.