{"record":{"id":"ac3fa0cfb7b084b2","repo":"elastic/elasticsearch","slug":"cannot-use-expression-with-text-variable-using","errorCode":null,"errorMessage":"Cannot use expression with text variable using {}","messagePattern":"Cannot use expression with text variable using (.+?)","errorType":"exception","errorClass":"GeneralScriptException","httpStatus":null,"severity":"error","filePath":"modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionAggregationScript.java","lineNumber":89,"sourceCode":"            }\n\n            @Override\n            public void setDocument(int d) {\n                try {\n                    values.advanceExact(d);\n                } catch (IOException e) {\n                    throw new IllegalStateException(\"Can't advance to doc using \" + exprScript, e);\n                }\n            }\n\n            @Override\n            public void setNextAggregationValue(Object value) {\n                // _value isn't used in script if specialValue == null\n                if (specialValue != null) {\n                    if (value instanceof Number) {\n                        specialValue.setValue(leaf, ((Number) value).doubleValue());\n                    } else {\n                        throw new GeneralScriptException(\"Cannot use expression with text variable using \" + exprScript);\n                    }\n                }\n            }\n        };\n    }\n}\n","sourceCodeStart":71,"sourceCodeEnd":96,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionAggregationScript.java#L71-L96","documentation":"Thrown as GeneralScriptException by ExpressionAggregationScript.setNextAggregationValue when the incoming _value for an aggregation script is not a Number. Expression scripts are strictly numeric; if a metric feeding _value is non-numeric (e.g. a string from a keyword max aggregation), binding it into the expression is impossible and the script aborts.","triggerScenarios":"Using a lang: expression script in an aggregation that receives _value (e.g. bucket_script with a parent aggregation emitting strings, or a scripted metric passing a text value). The specialValue slot is populated, but value instanceof Number is false.","commonSituations":"Chaining a keyword/terms aggregation's value into a numeric expression; mixing a max aggregation on a text field upstream; misconfigured bucket_script paths referencing a non-numeric bucket path.","solutions":["Ensure every parent aggregation feeding _value into the expression emits numeric values.","Switch the upstream field to a numeric type, or use a sub-aggregation that produces numbers.","If you need string handling, switch the script lang from 'expression' to 'painless'."],"exampleFix":"// before: parent 'max' agg is on a keyword field, _value is a string\n{\"bucket_script\": {\"buckets_path\": {\"v\": \"max_tag\"}, \"script\": {\"source\": \"_value + 1\", \"lang\": \"expression\"}}}\n\n// after: parent agg is on a numeric field\n{\"bucket_script\": {\"buckets_path\": {\"v\": \"max_price\"}, \"script\": {\"source\": \"_value + 1\", \"lang\": \"expression\"}}}","handlingStrategy":"validation","validationCode":"// Ensure parent aggregations feeding _value emit numbers\n// e.g. verify the buckets_path points to a numeric metric agg (avg/sum/max/min on a numeric field)\nif (!isNumericAgg(parentAggType)) {\n    throw new IllegalArgumentException(\"_value must come from a numeric aggregation for expression scripts\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Only feed numeric aggregation values into lang: expression scripts.","For string/text handling, switch to lang: painless.","Double-check buckets_path targets numeric sub-aggregations."],"tags":["elasticsearch","lang-expression","expression-script","aggregation","type-mismatch"],"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T06:17:24.410Z"}