Skip to content

fix(query): reject TextQuery text emptied by stopwords - #831

Merged
vishal-bala merged 3 commits into
redis:mainfrom
pcbeingused333:fix/text-query-stopwords-c1517
Nov 12, 2026
Merged

vishal-bala merged 3 commits into
redis:mainfrom
pcbeingused333:fix/text-query-stopwords-c1517

Conversation

@pcbeingused333

@pcbeingused333 pcbeingused333 commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #821 (the stopword-only TextQuery construction error; escaped-token matching remains a separate followup).

Motivation

TextQuery built an empty full-text clause when stopword filtering removed every token. Rejecting that input during construction gives the same early ValueError as hybrid queries, as approved in the issue.

Changes

TextQuery owns a single FullTextQueryHelper, forwarding stopwords and text-weight access and updates instead of duplicating parsing or rebuilding the helper. The constructor validates the filter before text content, rejects blank text with a plain empty-text message, and retains the rendered query so the first execution does not repeat tokenization. Weight and field setters continue to invalidate the cache.

Regression tests cover filtering, validation precedence, empty text, helper reuse during rebuilding, single tokenization, escaping and updated weights. Stopword tests sit with the existing query-type assertions.

Notes

Escaped-token matching remains separate because it changes results for existing users. Use set_text_weights to update weights and invalidate the cached query. Stopwords access follows the shared helper and returns a copy.

Release Notes

TextQuery raises ValueError during construction when text is blank or tokenization and stopword filtering leave no terms, instead of failing with a Redis syntax error during search.

Reviewer Brief

Start here: TextQuery.init and _tokenize_and_escape_query in redisvl/query/query.py. One retained helper parses configuration; construction builds and caches the validated query after checking the filter.

Not verified: live Redis, cluster behavior and the full suite. Selected tests use --noconftest to avoid the shared container setup. All-files spelling found an existing issue outside this change; checks on changed files pass.

Beyond CI: the added followup regressions failed before the fix (5 failed, 82 passed, 1 skipped) and pass after it (87 passed, 1 skipped).

Reviewed 8237317 · Codex (GPT-6) · lenses: correctness, tests, security; separate challenger pass

Prepared with assistance from an AI coding agent (Codex).


Note

Low Risk
Behavior change is limited to failing fast on invalid text input at construction time; shared helper centralizes existing tokenization logic without altering successful query semantics.

Overview
TextQuery now delegates stopword handling, tokenization, and per-word weights to FullTextQueryHelper, removing duplicated logic from query.py. Construction rejects blank input early (text string cannot be empty), applies the filter before text validation, and builds the query string once at init so stopword-only queries fail with ValueError instead of producing an invalid Redis clause at search time.

Unit tests cover empty or stopword-only text, disabled stopwords, filter validation ordering, and cache invalidation when set_text_weights updates the rendered query.

Reviewed by Cursor Bugbot for commit b990e76. Bugbot is set up for automated code reviews on this repo. Configure here.

@chrikrah chrikrah left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my perspective this can be merged. TextQuery("the a an") now raises the same ValueError as AggregateHybridQuery, and the shared helper is byte for byte the old tokenizer plus that check, so no other query changes.

non-blocking: the MCP fulltext tool builds a TextQuery too, and map_exception turns the new ValueError into a clean invalid-request error, which beats the Redis syntax error it got before.

$ python rv_repro.py        # construction half of the #821 repro, base 930c14e vs head c18f796
base: 'the a an' -> @body:()
head: 'the a an' -> ValueError: text string cannot be empty after removing stopwords

$ pytest -q --noconftest tests/unit/test_text_query_weights.py tests/unit/test_query_types.py tests/unit/test_aggregation_types.py   # head, Python 3.12
83 passed, 1 skipped in 1.21s

$ (same, base query.py, head tests)
FAILED tests/unit/test_text_query_weights.py::test_text_query_rejects_empty_tokenized_text[the a an-english]
4 failed, 79 passed, 1 skipped in 0.62s
# not run: live Redis, the integration suite

I'll send the escaped-token fix as the follow-up once this is in.

@vishal-bala vishal-bala added the auto:patch Increment the patch version when merged label Nov 10, 2026
Comment thread redisvl/query/query.py Resolved
Comment thread redisvl/query/query.py Resolved
Comment thread redisvl/query/query.py Resolved
Comment thread tests/unit/test_text_query_weights.py Resolved
@vishal-bala
vishal-bala self-requested a review November 10, 2026 14:04
Comment thread tests/unit/test_query_types.py Resolved
Comment thread tests/unit/test_query_types.py Resolved
@vishal-bala

Copy link
Copy Markdown
Collaborator

I can get this PR merged as soon as those last comments are addressed 🙌

@pcbeingused333

Copy link
Copy Markdown
Contributor Author

Fixed in b990e76:

  • tests/unit/test_query_types.py (weights mutation): removed test_text_query_does_not_revalidate_mutated_weights_on_rebuild, so direct mutation of the returned dictionary is no longer pinned as supported behaviour.
  • tests/unit/test_query_types.py (tokenization): removed test_text_query_tokenizes_once_until_weights_change; the existing test_set_text_weights_resets_cached_query_string still covers cache invalidation.

Selected query tests pass: 85 passed, 1 skipped.

This comment was drafted with Codex.

@vishal-bala
vishal-bala merged commit afd7cc6 into redis:main Nov 12, 2026
55 checks passed
@applied-ai-release-bot

Copy link
Copy Markdown

🚀 PR was released in v0.32.1 🚀

@applied-ai-release-bot applied-ai-release-bot Bot added the released This issue/pull request has been released. label Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auto:patch Increment the patch version when merged released This issue/pull request has been released.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TextQuery builds "@field:()" when every token is a stopword, instead of raising ValueError

3 participants