elastic/elasticsearch · error · ClassCastException

Cannot apply [*] operation to type [boolean]

Error message

Cannot apply [*] operation to type [boolean]

What it means

Multiplication (*) is defined for all numeric types but not for boolean. DefMath's boolean overload for mul() throws a ClassCastException when one or both def operands resolve to Boolean at runtime.

Source

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

    private static int mul(int a, int b) {
        return a * b;
    }

    private static long mul(long a, long b) {
        return a * b;
    }

    private static float mul(float a, float b) {
        return a * b;
    }

    private static double mul(double a, double b) {
        return a * b;
    }

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

    private static Object mul(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 multiplying.
  2. Use an explicit numeric type declaration instead of def.
  3. Guard with instanceof and branch to handle boolean vs numeric cases.

Example fix

// before
def x = doc['active'].value;
def rate = doc['rate'].value;
def result = x * rate;

// after
def x = doc['active'].value;
def rate = doc['rate'].value;
if (x instanceof Number) {
    def result = (double) x * (double) rate;
}
Defensive patterns

Strategy: type-guard

Validate before calling

// Painless: verify both operands are numeric before multiplication
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 multiplies two def variables where at least one resolves to a Boolean. Example: `def x = true; def y = 5; def z = x * y;`

Common situations: A script receives a boolean field value and mistakenly uses it in an arithmetic expression. Mixing logical flags with numeric calculations in def-typed code.

Related errors


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