elastic/elasticsearch · error · ClassCastException

Cannot apply [/] operation to type [boolean]

Error message

Cannot apply [/] operation to type [boolean]

What it means

Division (/) is defined for all numeric types but not for boolean. DefMath's boolean overload for div() throws a ClassCastException when one or both def operands resolve to Boolean.

Source

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

    private static int div(int a, int b) {
        return a / b;
    }

    private static long div(long a, long b) {
        return a / b;
    }

    private static float div(float a, float b) {
        return a / b;
    }

    private static double div(double a, double b) {
        return a / b;
    }

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

    private static Object div(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. Ensure both operands are numeric before dividing.
  2. Use an explicit numeric type declaration instead of def.
  3. Guard with instanceof and only divide when both operands are Number.

Example fix

// before
def x = doc['flag'].value;
def y = doc['divisor'].value;
def z = x / y;

// after
def x = doc['flag'].value;
def y = doc['divisor'].value;
if (x instanceof Number && y instanceof Number) {
    def z = (double) x / (double) y;
}
Defensive patterns

Strategy: type-guard

Validate before calling

// Painless: verify both operands are numeric before division
def a = doc['val1'].value;
def b = doc['val2'].value;
if (a instanceof Number && b instanceof Number) {
    def result = (double) a / (double) b;
}

Type guard

// Painless type guard for numeric operands
def bothNumeric(def a, def b) {
    return a instanceof Number && b instanceof Number;
}

Prevention

When it happens

Trigger: A Painless script divides def variables where at least one resolves to Boolean. Example: `def x = true; def y = 2; def z = x / y;`

Common situations: A boolean field value is mistakenly included in an arithmetic division expression. Using def-typed variables from heterogeneous document fields where some resolve to Boolean.

Related errors


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