elastic/elasticsearch · error · ClassCastException

Cannot apply [/] operation to types [{}] and [{}].

Error message

Cannot apply [/] operation to types [{}] and [{}].

What it means

DefMath.div(Object, Object) handles division of def values for Number and Character combinations. If neither operand matches any supported branch, the method throws a ClassCastException naming both operands' actual types.

Source

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

                }
            }
        } else if (left instanceof Character) {
            if (right instanceof Number) {
                if (right instanceof Double) {
                    return (char) left / ((Number) right).doubleValue();
                } else if (right instanceof Long) {
                    return (char) left / ((Number) right).longValue();
                } else if (right instanceof Float) {
                    return (char) left / ((Number) right).floatValue();
                } else {
                    return (char) left / ((Number) right).intValue();
                }
            } else if (right instanceof Character) {
                return (char) left / (char) right;
            }
        }

        throw new ClassCastException(
            "Cannot apply [/] operation to types "
                + "["
                + left.getClass().getCanonicalName()
                + "] and ["
                + right.getClass().getCanonicalName()
                + "]."
        );
    }

    private static int rem(int a, int b) {
        return a % b;
    }

    private static long rem(long a, long b) {
        return a % b;
    }

    private static float rem(float a, float b) {

View on GitHub (pinned to db6a809a66)

Solutions

  1. Cast both operands to compatible numeric types: `def z = (double) x / (double) y;`
  2. Guard with instanceof to verify both are Number or Character.
  3. Use explicit types instead of def for arithmetic.

Example fix

// before
def total = doc['total'].value;
def count = doc['count'].value;
def avg = total / count;

// after
def total = doc['total'].value;
def count = doc['count'].value;
if (total instanceof Number && count instanceof Number) {
    def avg = (double) total / (double) count;
}
Defensive patterns

Strategy: type-guard

Validate before calling

// Painless: verify both operands are Number or Character 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 divisible types
def canDivide(def a, def b) {
    return (a instanceof Number && b instanceof Number) || (a instanceof Number && b instanceof Character) || (a instanceof Character && b instanceof Number) || (a instanceof Character && b instanceof Character);
}

Prevention

When it happens

Trigger: A Painless script divides two def variables whose runtime types are not compatible with division. Example: `def x = 'abc'; def y = 2; def z = x / y;` where x is a String.

Common situations: Dividing a def value that resolved to a String from a keyword/text field. Processing dynamically-typed values where the runtime type differs from the expected numeric type.

Related errors


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