elastic/elasticsearch · error · GeneralScriptException

Error evaluating {}

Error message

Error evaluating {}

What it means

Thrown inside ExpressionTermSetQueryScript.execute() when values.doubleValue() raises any Exception, wrapped in GeneralScriptException with the expression source text. This fires during terms_set query evaluation when the computed numeric result for a document cannot be retrieved.

Source

Thrown at modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionTermSetQueryScript.java:50

    ExpressionTermSetQueryScript(Expression e, SimpleBindings b) {
        exprScript = e;
        bindings = b;
        source = exprScript.getDoubleValuesSource(bindings);
    }

    @Override
    public TermsSetQueryScript newInstance(final LeafReaderContext leaf) throws IOException {
        return new TermsSetQueryScript() {
            // Fake the scorer until setScorer is called.
            DoubleValues values = source.getValues(leaf, null);

            @Override
            public Number 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);
                }
            }
        };
    }

}

View on GitHub (pinned to db6a809a66)

Solutions

  1. Verify the field used in the terms_set expression has doc_values enabled and exists in the mapping.
  2. Ensure the field has values for the documents being queried — check with a simple term or exists query first.
  3. Test the expression independently with a function_score or sort to isolate whether it's specific to the terms_set path.
  4. If only some documents are affected, provide a default value via params or use a conditional in Painless.

Example fix

// before: field may be missing or non-numeric
GET my-index/_search
{
  "query": {
    "terms_set": {
      "tags": {
        "terms": ["elasticsearch", "search"],
        "minimum_should_match_script": {
          "lang": "expression",
          "source": "doc['required_matches'].value"
        }
      }
    }
  }
}
// after: verify field exists with GET my-index/_mapping, ensure doc_values enabled
// If field is optional, use Painless with a null check instead.
Defensive patterns

Strategy: try-catch

Validate before calling

// Before using a terms_set expression, verify the referenced field has data
GET my-index/_search
{ "size": 0, "aggs": { "check": { "stats": { "field": "required_matches" } } } }
// If no stats are returned, the field is empty or missing and the expression will fail

Try / catch

try {
    Number result = termsSetScript.execute();
} catch (GeneralScriptException e) {
    logger.warn("Terms set expression evaluation failed: {}", e.getMessage(), e);
    // Return a default that means 'no match' or rethrow depending on requirements
    return 0;
}

Prevention

When it happens

Trigger: Using a terms_set query with "minimum_should_match_field" or "minimum_should_match_script" using "lang":"expression", and doubleValue() fails during evaluation — e.g., the referenced field has no value for the document or the doc-values source encounters an error.

Common situations: Field referenced in the terms_set expression has doc_values disabled or is missing for some documents; expression produces an invalid value; corrupted field data for the specific segment.

Related errors


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