elastic/elasticsearch · error · GeneralScriptException

Error evaluating {}

Error message

Error evaluating {}

What it means

Thrown as GeneralScriptException by ExpressionAggregationScript.execute when calling doubleValue() on the bound DoubleValues raises any Exception during aggregation script execution. The original exception is wrapped as the cause; the message echoes the compiled Expression source text for context. This is a runtime evaluation failure, not a parse/compile error.

Source

Thrown at modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionAggregationScript.java:69

            // Fake the scorer until setScorer is called.
            DoubleValues values = source.getValues(leaf, new DoubleValues() {
                @Override
                public double doubleValue() throws IOException {
                    return get_score().doubleValue();
                }

                @Override
                public boolean advanceExact(int doc) throws IOException {
                    return true;
                }
            });

            @Override
            public Object execute() {
                try {
                    return values.doubleValue();
                } catch (Exception exception) {
                    throw new GeneralScriptException("Error evaluating " + exprScript, exception);
                }
            }

            @Override
            public void setDocument(int d) {
                try {
                    values.advanceExact(d);
                } catch (IOException e) {
                    throw new IllegalStateException("Can't advance to doc using " + exprScript, e);
                }
            }

            @Override
            public void setNextAggregationValue(Object value) {
                // _value isn't used in script if specialValue == null
                if (specialValue != null) {
                    if (value instanceof Number) {
                        specialValue.setValue(leaf, ((Number) value).doubleValue());

View on GitHub (pinned to db6a809a66)

Solutions

  1. Inspect the wrapped cause in the stack trace to find the real failure (missing doc_values, unbound variable, etc.).
  2. Ensure every field referenced in the expression is numeric with doc_values enabled.
  3. Test the expression against a single doc first; simplify to isolate the failing term.

Example fix

// before: 'tags' is a keyword field with no doc_values for numeric use
{"script": {"source": "doc['price'].value + doc['tags'].value", "lang": "expression"}}

// after: reference only numeric doc-values fields
{"script": {"source": "doc['price'].value", "lang": "expression"}}
Defensive patterns

Strategy: try-catch

Validate before calling

// Before using a field in an aggregation expression, confirm it is numeric with doc_values
GetMappingsResponse m = client.indices().getMappings(s -> s.index(idx)).result();
// inspect the field type; ensure 'double','long', etc. and doc_values=true

Try / catch

try {
    // run aggregation with expression script
} catch (GeneralScriptException e) {
    // e.getCause() holds the real reason (missing doc_values, unbound var, ...)
    log.warn("expression aggregation failed", e.getCause());
    throw e;
}

Prevention

When it happens

Trigger: Running an aggregation with a lang: expression script (scripted_metric, bucket_script, etc.) where evaluating the expression at query time throws - e.g. referencing a field that has no doc_values, a binding that returns no values for a doc, or arithmetic producing an invalid result.

Common situations: Referencing a text/keyword field (no numeric doc_values) in a numeric expression; field disabled for doc_values; expression divides by zero or references an unbound variable that yields no value; corrupted fielddata.

Related errors


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