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 a right-shift (>>) is applied to a boolean value through the def/dynamic path. DefMath.rsh(boolean, long) is a stub that always throws; booleans are not numeric and cannot participate in bitwise shift. The stub exists so the dynamic dispatcher has a method handle for every promoted type, yielding a clear message rather than a NoSuchMethodException.
Source
Thrown at modules/lang-painless/src/main/java/org/elasticsearch/painless/DefMath.java:1032
private static int rsh(int a, long b) {
return a >> b;
}
private static long rsh(long a, long b) {
return a >> b;
}
private static float rsh(float a, long b) {
throw new ClassCastException("Cannot apply [>>] operation to type [float]");
}
private static double rsh(double a, long b) {
throw new ClassCastException("Cannot apply [>>] operation to type [double]");
}
private static boolean rsh(boolean a, long b) {
throw new ClassCastException("Cannot apply [>>] operation to type [boolean]");
}
public static Object rsh(Object left, long right) {
if (left instanceof Long) {
return (long) left >> right;
} else {
return intIntegralValue(left) >> right;
}
}
private static int ush(int a, long b) {
return a >>> b;
}
private static long ush(long a, long b) {
return a >>> b;
}
View on GitHub (pinned to db6a809a66)
Solutions
- Convert the boolean to an integer explicitly before shifting: '(x ? 1 : 0) >> 1'.
- Use a typed declaration (int/long) and assign a numeric value so the compiler rejects the boolean assignment early.
- Reconsider the logic; shifting a boolean is almost always a sign of a type confusion bug.
Example fix
// before def x = doc['active'].value; // boolean field def y = x >> 1; // after int x = doc['active'].value ? 1 : 0; int y = x >> 1;
Defensive patterns
Strategy: type-guard
Validate before calling
// Convert boolean to int before any bitwise op: // def x = doc['active'].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
- Check field mappings; boolean fields need explicit conversion before bit operations.
- Do not assume def values are numeric.
- Prefer typed declarations for fields with known types.
When it happens
Trigger: A def-typed variable that holds a boolean at runtime is shifted: 'def x = true; def y = x >> 1;'. Also occurs when a doc field is boolean-typed and the script attempts bit operations on it.
Common situations: Scripts that operate generically over def values without checking the runtime type, or that assume a flag field (0/1) is stored as an integer when it is actually boolean. Logic ported from dynamically-typed languages that treat booleans as 1/0.
Related errors
- Cannot apply [>>] operation to type [float]
- Cannot apply [>>] operation to type [double]
- Cannot apply [>>>] operation to type [float]
- Cannot apply [>>>] operation to type [double]
- Cannot apply [>>>] operation to type [boolean]
AI-assisted analysis of elastic/elasticsearch@db6a809a66 (2026-08-12).
Data as JSON: /api/errors/c45850bb6487f247.
Report an issue: GitHub.