elastic/elasticsearch · error · GeneralScriptException

Error evaluating {}

Error message

Error evaluating {}

What it means

Thrown inside ExpressionNumberSortScript.execute() when values.doubleValue() raises any Exception. The expression engine catches it and wraps it in a GeneralScriptException that includes the compiled expression source text. doubleValue() is called after advanceExact, so this represents a failure computing or retrieving the numeric result for the current document.

Source

Thrown at modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionNumberSortScript.java:72

            // Fake the scorer until setScorer is called.
            DoubleValues values = source.getValues(ctx, new DoubleValues() {
                @Override
                public double doubleValue() {
                    return 0.0D;
                }

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

            @Override
            public double 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 boolean needs_score() {
        return needsScores;
    }

View on GitHub (pinned to db6a809a66)

Solutions

  1. Verify every field referenced in the expression has doc_values enabled (default true for numeric/date — check mapping for "doc_values": false).
  2. Confirm the field exists and has data for the documents being sorted; use the missing sort option to supply a default for documents without the field.
  3. Test the expression against a single document with GET <index>/_search?q=_id:<docId> to isolate which document triggers the failure.
  4. If the field is multi-valued, use a specific accessor method (e.g., doc['field'].value returns the first value) or pre-process the data.

Example fix

// before: field may have doc_values disabled
PUT my-index/_mapping
{ "properties": { "price": { "type": "double", "doc_values": false } } }
// sort expression fails at runtime
// after: enable doc_values (requires reindex)
PUT my-index-new/_mapping
{ "properties": { "price": { "type": "double" } } }
POST _reindex { "source": {"index":"my-index"}, "dest": {"index":"my-index-new"} }
Defensive patterns

Strategy: validation

Validate before calling

// Before using an expression sort, verify all referenced fields have doc_values enabled
GET my-index/_mapping
// In application code, validate the mapping before building the sort:
// Check that each doc['field'] in the expression is a numeric/date/geo field with doc_values: true

Try / catch

// When calling a sort script programmatically:
try {
    double sortValue = sortScript.execute();
} catch (GeneralScriptException e) {
    logger.warn("Expression sort evaluation failed for expression [{}]", exprSource, e);
    // Return a neutral sort value or skip the document
    return 0.0;
}

Prevention

When it happens

Trigger: Sorting by an expression script ("sort": {"_script": {"type": "number", "script": {"lang": "expression", "source": "doc['price'].value * 1.2"}}}) and doubleValue() fails — e.g., the DoubleValues source returned false from advanceExact internally, or a backing doc-values lookup threw.

Common situations: Referencing a field that has doc_values disabled; sorting on a field that is multi-valued when the expression expects a single value; expression produces a value that overflows or is otherwise invalid for the sort path.

Related errors


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