Skip to content

StackOverflowError in automatic kNN pre-filtering with script_score-wrapped semantic_text queries #162295

Closed

Description

@holiholiholiholi

Elasticsearch Version

9.5.5/9.3.0

Installed Plugins

No response

Java Version

OpenJDK 27 (27+35-2325, Oracle Corporation)

OS Version

Chainguard / Linux 5.15.0-1080-gcp (amd64)

Problem Description

We are seeing a reproducible java.lang.StackOverflowError when multiple script_score clauses wrap match queries on semantic_text fields inside the same compound bool query.

The issue appears to be caused by recursive query processing during automatic kNN pre-filtering. A semantic_text match is internally rewritten into a kNN query. While building that query, Elasticsearch inspects the surrounding bool query for conditions that can be applied as automatic pre-filters. When other script_score-wrapped semantic queries are present in the same bool structure, each rewritten kNN query may inspect the surrounding clauses for automatic pre-filtering, including clauses that themselves contain semantic queries.

This creates a recursive cycle roughly like:

kNN → auto pre-filtering → bool/script_score → semantic query → kNN → auto pre-filtering → ...

until the Java call stack is exhausted.

In our reproducer, two semantic script_score clauses inside should work, while adding another semantic script_score in either must or must_not triggers the failure.

The issue is reproducible in both Elasticsearch 9.3.0 and 9.5.5.

This is not limited to a failed search request: the StackOverflowError is treated as a fatal error and terminates the affected Elasticsearch node. In our two-data-node cluster, the shard request was retried on the replica and triggered the same failure there, making the cluster unavailable.

Steps to Reproduce

  1. Use an index containing at least two semantic_text fields, for example:

    • semantic_field_A
    • semantic_field_B
  2. Run the following query with two script_score clauses inside should:

{
  "query": {
    "bool": {
      "should": [
        {
          "script_score": {
            "query": {
              "match": {
                "semantic_field_A": {
                  "query": "semantic query A"
                }
              }
            },
            "script": {
              "source": "_score >= params.min_score ? 5 * _score : 0",
              "params": {
                "min_score": 0.7
              }
            }
          }
        },
        {
          "script_score": {
            "query": {
              "match": {
                "semantic_field_B": {
                  "query": "semantic query A"
                }
              }
            },
            "script": {
              "source": "_score >= params.min_score ? 5 * _score : 0",
              "params": {
                "min_score": 0.7
              }
            }
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}

This query works as expected.

  1. Add another script_score-wrapped semantic query in a must clause in the same compound bool query:
{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "script_score": {
                  "query": {
                    "match": {
                      "semantic_field_A": {
                        "query": "semantic query A"
                      }
                    }
                  },
                  "script": {
                    "source": "_score >= params.min_score ? 5 * _score : 0",
                    "params": {
                      "min_score": 0.7
                    }
                  }
                }
              },
              {
                "script_score": {
                  "query": {
                    "match": {
                      "semantic_field_B": {
                        "query": "semantic query A"
                      }
                    }
                  },
                  "script": {
                    "source": "_score >= params.min_score ? 5 * _score : 0",
                    "params": {
                      "min_score": 0.7
                    }
                  }
                }
              }
            ],
            "minimum_should_match": 1
          }
        },
        {
          "script_score": {
            "query": {
              "match": {
                "semantic_field_B": {
                  "query": "semantic query B"
                }
              }
            },
            "script": {
              "source": "_score >= params.min_score ? 5 * _score : 0",
              "params": {
                "min_score": 0.7
              }
            }
          }
        }
      ]
    }
  }
}
  1. The query triggers a java.lang.StackOverflowError while Elasticsearch is building the query.

  2. The same behavior can also be reproduced by placing the additional semantic script_score clause in must_not instead of must.

  3. Reproduced on Elasticsearch 9.3.0 and 9.5.5.

Logs (if relevant)

java.lang.StackOverflowError
    at org.elasticsearch.index.query.support.AutoPrefilteringUtils.pruneQuery(...)
    at org.elasticsearch.index.query.support.AutoPrefilteringUtils.pruneBoolQuery(...)
    at org.elasticsearch.search.vectors.KnnVectorQueryBuilder.applyAutoPrefilteringIfEnabled(...)
    at org.elasticsearch.search.vectors.KnnVectorQueryBuilder.doToQuery(...)
    at org.elasticsearch.index.query.NestedQueryBuilder.doToQuery(...)
    at org.elasticsearch.index.query.functionscore.ScriptScoreQueryBuilder.doToQuery(...)
    at org.elasticsearch.index.query.BoolQueryBuilder.doToQuery(...)
    at org.elasticsearch.search.vectors.KnnVectorQueryBuilder.applyAutoPrefilteringIfEnabled(...)
    ...

The same sequence repeats recursively until the Java call stack is exhausted.

Elasticsearch then terminates the affected node with:

fatal error in thread [elasticsearch[instance-0000000025][search][T#1]], exiting

Activity

  1. elasticsearchmachine commented on Oct 16, 2026

    @elasticsearchmachine
    Collaborator

    Bug analysis

    Verdict: High-confidence hypothesis, based on reading the code on main (e6ef806037d). I wrote a focused unit test but could not run it here because the local JVM was unavailable.

    Root cause: BoolQueryBuilder.addBooleanClauses pushes the sibling must/filter/inverted must_not clauses into AutoPrefilteringScope before converting each clause. KnnVectorQueryBuilder.applyAutoPrefilteringIfEnabled then calls toQuery() on each pruned prefilter while that scope is still active. The recursion check only skips a prefilter when it is the knn query itself (this == scopedPrefilter.query()). AutoPrefilteringUtils.pruneQuery does not look inside script_score, so the nested query that semantic_text produces stays inside it unpruned. So when sibling knn A builds script_score(nested(knn B)), knn B sees that same script_score as its own prefilter and builds it again, which repeats until the stack overflows. should clauses are never collected as prefilters, which is why the should-only query works.

    Reproduced: Not executed (local java gave permission denied). These test cases are in reproduction.patch, in AbstractKnnVectorQueryBuilderTestCase, and run via KnnFloatVectorQueryBuilderTests:

    • testAutoPrefilteringWithScriptScoreWrappedKnnInMustDoesNotRecurse
    • testAutoPrefilteringWithScriptScoreWrappedKnnInMustNotDoesNotRecurse
    • testAutoPrefilteringWithConstantScoreWrappedKnnInMustDoesNotRecurse. Reading the code, constant_score/function_score wrappers should trigger the same loop.

    Recommended fix:

    1. Add a general guard: while knn converts its prefilters to Lucene queries, stop collecting prefilters for anything nested inside them (AutoPrefilteringScope.suspend()). A prefilter is only used for filtering and does not need prefiltering itself. This is sketched in candidate-fix.patch, which has not been tested.
    2. Optionally, also make pruneQuery look inside ScriptScoreQueryBuilder so nested (semantic) prefilters are pruned there as well.

    Risk: Blocker. A valid user query raises an uncaught StackOverflowError that kills the node (fatal error ... exiting). Any user who can search can trigger it, and it affects 9.3.0 and later.

    Additional notes
    • Reporter holiholiholiholi: the elastic org membership lookup returned 404, so I classified them as an external contributor. This is inferred, not verified.
    • Test layer: I chose a unit test (AbstractQueryTestCase, building the bool query with a SearchExecutionContext), because the loop is purely in query-builder-to-Lucene conversion. An internal cluster test with semantic_text (in the inference plugin) would be more faithful but much more expensive.
    • Command to run the tests: ./gradlew :server:test --tests "org.elasticsearch.search.vectors.KnnFloatVectorQueryBuilderTests.testAutoPrefilteringWith*"
    • Workaround: put the extra semantic script_score clauses in should, or use separate queries or a retriever, until this is fixed.

    Generated by bug-analysis · archimedes v1.1.0 · openrouter/anthropic/claude-opus-5.5

  2. elasticsearchmachine commented on Oct 16, 2026

    @elasticsearchmachine
    Collaborator

    This issue has been labeled blocker, which will prevent Elasticsearch Serverless from being promoted until the blocking condition is resolved.

    To resolve this blocker, all of the following must be true:

    • A fix PR is linked to this issue — via the "Development" panel in the GitHub sidebar, or a closing keyword (e.g. Fixes #NNN) in the PR description
    • All linked PRs are merged
    • All linked PRs' merge commits are included in the promoted build
    • This issue is closed

    Note

    If no linked PRs are found, the blocker cannot be resolved even if the issue is closed.

    Optional — narrow the scope with Introduced in:

    If the bug was introduced in a specific commit, add the following line to this issue's description:

    Introduced in: <commit-sha-or-pull-request-url>
    

    This tells the promotion check that builds predating this commit are unaffected and should not be blocked. For the commit SHA, both the short (7-character) and full (40-character) SHA are accepted.

    For more information, see the Promotion Blocker documentation.

  3. elasticsearchmachine commented on Oct 16, 2026

    @elasticsearchmachine
    Collaborator

    Pinging @elastic/es-search-relevance (Team:Search Relevance)

  4. self-assigned this
    on Oct 16, 2026
  5. added
    statefulMarking issues only relevant for stateful releases. Affects release notes.
    on Oct 16, 2026
  6. added a commit that references this issue on Oct 19, 2026
    68790bc
  7. added 2 commits that reference this issue on Oct 19, 2026
    54e59aa
    4774a82
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

:Search Relevance/VectorsVector search>bugTeam:Search RelevanceMeta label for the Search Relevance team in ElasticsearchblockerstatefulMarking issues only relevant for stateful releases. Affects release notes.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions