Skip to content

ES|QL TOP_SNIPPETS: Validate query argument is foldable at verification time #142462

@mridula-s109

Description

@mridula-s109

Business Goal

TOP_SNIPPETS takes a text field and a search query and extracts the most relevant passages. For example, if a user searches "machine learning," it highlights the best matching snippets from each result.

Currently, if a user passes a field instead of a search term - e.g., TOP_SNIPPETS(description, title) — we don't validate this. This means every row would search for something different (its own title inside its own description). We want to catch this mistake at compile time and show a clear error: "query must be a constant like a string, not a field."

Why This Is Required for semantic_text TOP_SNIPPETS

To score chunks semantically, we convert the query into a vector. This is expensive. We can only do it once if the query is the same for every row. If the query changes per row, we'd call the AI model thousands of times once per document which is neither practical nor performant.

Expected Behaviour

Expression Result
TOP_SNIPPETS(body, "search terms") ✅ Works as before
TOP_SNIPPETS(body, title) ❌ Compile-time error: query must be a constant
TOP_SNIPPETS(body, CONCAT("search", " terms")) ✅ Works — the result is still a constant

Success Criteria

The query argument must be validated as foldable. A foldable expression is one that is sufficient to evaluate only once for all rows:

  • "search terms" → foldable (literal constant)
  • CONCAT("abc", "def") → foldable (all inputs are constants, result is the same for every row)
  • CONCAT(title, description)not foldable (inputs are fields from an Elasticsearch index, result varies per row)

If the query argument is not foldable, the query should fail at compile time with a descriptive error message.


Metadata

Metadata

Assignees

Labels

No fields configured for Enhancement.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions