elastic/elasticsearch · error · ClassCastException

Cannot apply [>>>] operation to type [boolean]

Error message

Cannot apply [>>>] operation to type [boolean]

What it means

Painless throws this ClassCastException at runtime when the unsigned right-shift (>>>) is applied to a boolean value through the def/dynamic path. DefMath.ush(boolean, long) is a throwing stub: booleans are not integers and bitwise shift is meaningless on them. The stub exists so the dispatcher has a registered handle for the boolean promotion class, surfacing a readable error.

Source

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

    private static int ush(int a, long b) {
        return a >>> b;
    }

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

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

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

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

    public static Object ush(Object left, long right) {
        if (left instanceof Long) {
            return (long) (left) >>> right;
        } else {
            return intIntegralValue(left) >>> right;
        }
    }

    /**
     * unboxes a class to its primitive type, or returns the original
     * class if its not a boxed type.
     */
    private static Class<?> unbox(Class<?> clazz) {
        return MethodType.methodType(clazz).unwrap().returnType();
    }

View on GitHub (pinned to db6a809a66)

Solutions

  1. Convert the boolean to an integer explicitly: '(x ? 1 : 0) >>> 1'.
  2. Declare the variable as an explicit integral type and assign a numeric value.
  3. Re-examine the intent; shifting a boolean almost always indicates a type-confusion bug.

Example fix

// before
def x = doc['enabled'].value;  // boolean
def y = x >>> 1;
// after
int x = doc['enabled'].value ? 1 : 0;
int y = x >>> 1;
Defensive patterns

Strategy: type-guard

Validate before calling

// Convert boolean def to int before >>>:
// def x = doc['enabled'].value;
// int n = (x instanceof Boolean) ? (((Boolean)x) ? 1 : 0) : ((Number)x).intValue();
// int y = n >>> 1;

Type guard

// boolean isBool = x instanceof Boolean;

Prevention

When it happens

Trigger: A def variable holding a boolean is shifted with >>>: 'def x = false; def y = x >>> 1;'. The dispatch through ush(Object, long) lands on the boolean stub.

Common situations: Scripts treating boolean doc fields as integers (assuming 0/1), generic def-based logic over heterogeneous field types, or code ported from languages that coerce booleans numerically.

Related errors


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