Skip to content

Update fails silently when specifying a stored script (withScriptName) #3324

Closed

Description

@Edu93u

UpdateQuery.Builder exposes withScriptName(...) and UpdateQuery.Builder.build() accepts it as a valid way to define an update:

if (script == null && scriptName == null && document == null && query == null) {
   throw new IllegalArgumentException("either script, scriptName, document or query must be set");
}

However, ElasticsearchOperations.update(UpdateQuery, IndexCoordinates) never sends that script. The request reaches Elasticsearch with neither script nor doc, and the server answers:

400 Validation Failed: 1: script or doc is missing

Cause

RequestConverter.documentUpdateRequest (line 1059 in 6.0.5) guards the whole script block on the inline source rather than on the presence of script data:

1066:  var scriptData = query.getScriptData();
1067:  var script = scriptData != null ? scriptData.script() : null;   // inline source only
1068:
1069:  if (script != null) {                                           // <-- guard
1076:      uqb.script(sb -> {
1077:          sb.lang(scriptData.language()).params(params);
1081:          if (script != null) { sb.source(s -> s.scriptString(script)); }
1084:          sb.id(scriptData.scriptName());                         // never reached for a stored script
               });
         }

With withScriptName(...) and no withScript(...), scriptData.script() is null, the block at 1069 is skipped, and sb.id(scriptData.scriptName()) at 1084 is never executed.

The bulk path does not have this problem. RequestConverter.getScript(ScriptData) (around line 735) sets the id unconditionally and only appends the source when present:

sb.lang(scriptData.language()).params(params).id(scriptData.scriptName());
    if (scriptData.script() != null) { sb.source(s -> s.scriptString(scriptData.script())); }

which is why bulkUpdate(...) and updateByQuery(...) both transmit stored scripts correctly, and only the single-document update does not.

Why the bulk workaround is not always available

Elasticsearch rejects scripted updates sent through _bulk on any index containing a semantic_text field, with 400 Cannot apply update with a script on indices that contain [semantic_text] field(s) (see elastic/elasticsearch#136074) and reference. The single-document _update endpoint is not subject to that restriction, because it resolves the script server-side before the inference filter processes the resulting document.

So for indices with semantic_text, the single-document update is the only path that can carry a script — and it is precisely the path where a stored script is silently dropped. The two restrictions combined leave inline scripts as the sole option, which forces applications to ship the script with the code instead of managing it as a cluster-side artefact.

Suggested fix

Guard on the script data rather than on the inline source, so a stored script is attached as well:

if (scriptData != null && (scriptData.script() != null || scriptData.scriptName() != null)) {
        uqb.script(sb -> {
            sb.params(params);
            if (scriptData.script() != null) {
                sb.lang(scriptData.language());
                sb.source(s -> s.scriptString(scriptData.script()));
            }
            else {
                sb.id(scriptData.scriptName());
            }
            return sb;
        });
    }

Activity

  1. changed the title [-]DocumentUpdateRequest silently ignores a stored script (withScriptName) on the single-document update[/-] [+]Update fails silently when specifying a stored script (withScriptName)[/+] on Aug 7, 2026
  2. adityaanikam commented on Aug 9, 2026

    @adityaanikam

    Confirmed exactly against current main: the guard at documentUpdateRequest is if (script != null), so a stored script only update with scriptData.scriptName() set and scriptData.script() null skips the whole block and sb.id(scriptData.scriptName()) never runs. The bulk path you found (getScript) sets .id() unconditionally and only wraps .source() in a null check, so it doesn't have this problem. Fix looks straightforward, guard on scriptData != null with either field set, like you suggested. I'd like to open the PR.

  3. added
    type: bug
    and removed
    status: waiting-for-triage
    on Aug 15, 2026
  4. sothawo commented on Aug 15, 2026

    @sothawo
    Collaborator

    Thank you for finding and reporting. While having a look at this I found that the getScript(ScriptData) can be reused, so it's basically replacing the existing code

    			var scriptData = query.getScriptData();
    			var script = scriptData != null ? scriptData.script() : null;
    
    			if (script != null) {
    				Map<String, JsonData> params = new HashMap<>();
    
    				if (query.getParams() != null) {
    					query.getParams().forEach((key, value) -> params.put(key, JsonData.of(value, jsonpMapper)));
    				}
    
    				uqb.script(sb -> {
    					sb
    							.lang(scriptData.language())
    							.params(params);
    
    					if (script != null) {
    						sb.source(s -> s.scriptString(script));
    					}
    					sb.id(scriptData.scriptName());
    
    					return sb;
    				});
    			}
    

    with just this:

    			uqb.script(getScript(query.getScriptData()));
    

    Elasticsearch's query builder script() method can handle null values, so it's safe to just pass in the result from getScript(scriptDate)

    No need for a PR, I just have the changes on my machine, but I have to wait until after release on Monday as the branches relevant for the releases are locked down in release preparation

  5. self-assigned this
    on Aug 15, 2026
  6. Edu93u commented on Aug 17, 2026

    @Edu93u
    Author

    Thank you for your reply and for fixing it.

  7. added 2 commits that reference this issue on Aug 22, 2026
    ee67a9c
    e3b66f7
  8. added this to the 6.2.0-M2 (2026.1.0) milestone on Aug 22, 2026
  9. added 2 commits that reference this issue on Aug 22, 2026
    8c05e42
    0d33e65
  10. added a commit that references this issue on Aug 23, 2026
    4d0ec05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

type: bug

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions