From 2b2d4e49397cb65fcb1bbc41bf3510201e6ba4ac Mon Sep 17 00:00:00 2001 From: Christopher Krah <48338417+chrikrah@users.noreply.github.com> Date: Sat, 7 Nov 2026 21:38:07 +0100 Subject: [PATCH 1/3] fix(query): match stopwords and text weights against raw tokens _stopwords and _text_weights were consulted after TokenEscaper.escape, so the word the user typed never matched: it's was tested as it\'s against the English stopword set, and a text_weights key like don't never applied. Compare the raw normalized word, escape only after the filter and weight decisions, and keep the AGENTS.md normalization expression byte-identical. Signed-off-by: Christopher Krah <48338417+chrikrah@users.noreply.github.com> --- redisvl/utils/full_text_query_helper.py | 22 +++++++---------- tests/unit/test_full_text_query_helper.py | 29 +++++++++++++++++++++++ 2 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 tests/unit/test_full_text_query_helper.py diff --git a/redisvl/utils/full_text_query_helper.py b/redisvl/utils/full_text_query_helper.py index c845065f5..fa5d095bf 100644 --- a/redisvl/utils/full_text_query_helper.py +++ b/redisvl/utils/full_text_query_helper.py @@ -111,19 +111,15 @@ def _tokenize_and_escape_query(self, user_query: str) -> str: """ escaper = TokenEscaper() - tokens = [ - escaper.escape( - token.strip().strip(",").replace("“", "").replace("”", "").lower() - ) - for token in user_query.split() - ] - - token_list = [ - token for token in tokens if token and token not in self._stopwords - ] - for i, token in enumerate(token_list): - if token in self._text_weights: - token_list[i] = f"{token}=>{{$weight:{self._text_weights[token]}}}" + token_list = [] + for token in user_query.split(): + word = token.strip().strip(",").replace("“", "").replace("”", "").lower() + if not word or word in self._stopwords: + continue + escaped = escaper.escape(word) + if word in self._text_weights: + escaped = f"{escaped}=>{{$weight:{self._text_weights[word]}}}" + token_list.append(escaped) if not token_list: raise ValueError("text string cannot be empty after removing stopwords") diff --git a/tests/unit/test_full_text_query_helper.py b/tests/unit/test_full_text_query_helper.py new file mode 100644 index 000000000..b9deffbd6 --- /dev/null +++ b/tests/unit/test_full_text_query_helper.py @@ -0,0 +1,29 @@ +"""Unit tests for FullTextQueryHelper tokenization.""" + +from redisvl.query.aggregate import AggregateHybridQuery +from redisvl.utils.full_text_query_helper import FullTextQueryHelper + + +def test_stopword_with_apostrophe_is_filtered(): + """Stopword membership is checked against the word the user typed.""" + helper = FullTextQueryHelper() + assert helper._tokenize_and_escape_query("it's the fox") == "fox" + + +def test_text_weight_key_with_apostrophe_applies(): + """text_weights keys match the raw word, not its escaped form.""" + helper = FullTextQueryHelper(stopwords=None, text_weights={"don't": 2.0}) + assert helper._tokenize_and_escape_query("don't jump") == ( + "don\\'t=>{$weight:2.0} | jump" + ) + + +def test_aggregate_hybrid_text_clause_drops_apostrophe_stopword(): + """The clause a hybrid query sends contains no escaped stopword.""" + query = AggregateHybridQuery( + text="it's the fox", + text_field_name="body", + vector=[1.0, 0.0], + vector_field_name="v", + ) + assert "(~@body:(fox))" in str(query) From 52127ce2dca458756604dd995ffb17fc265cfb62 Mon Sep 17 00:00:00 2001 From: Christopher Krah <48338417+chrikrah@users.noreply.github.com> Date: Mon, 16 Nov 2026 10:09:53 +0100 Subject: [PATCH 2/3] test(query): test raw-token matching through build_query_string Move the helper tests from the private _tokenize_and_escape_query to build_query_string, the entry point both query classes call, and run the clause check over HybridQuery and AggregateHybridQuery. Add a test that pins the new ValueError for text made only of contractions and stopwords, and rename the loop variable that held the weighted term. Signed-off-by: Christopher Krah <48338417+chrikrah@users.noreply.github.com> --- redisvl/utils/full_text_query_helper.py | 6 +-- tests/unit/test_full_text_query_helper.py | 47 +++++++++++++++++++---- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/redisvl/utils/full_text_query_helper.py b/redisvl/utils/full_text_query_helper.py index fa5d095bf..e3f097e3c 100644 --- a/redisvl/utils/full_text_query_helper.py +++ b/redisvl/utils/full_text_query_helper.py @@ -116,10 +116,10 @@ def _tokenize_and_escape_query(self, user_query: str) -> str: word = token.strip().strip(",").replace("“", "").replace("”", "").lower() if not word or word in self._stopwords: continue - escaped = escaper.escape(word) + term = escaper.escape(word) if word in self._text_weights: - escaped = f"{escaped}=>{{$weight:{self._text_weights[word]}}}" - token_list.append(escaped) + term = f"{term}=>{{$weight:{self._text_weights[word]}}}" + token_list.append(term) if not token_list: raise ValueError("text string cannot be empty after removing stopwords") diff --git a/tests/unit/test_full_text_query_helper.py b/tests/unit/test_full_text_query_helper.py index b9deffbd6..71fa5e2d3 100644 --- a/tests/unit/test_full_text_query_helper.py +++ b/tests/unit/test_full_text_query_helper.py @@ -1,29 +1,60 @@ """Unit tests for FullTextQueryHelper tokenization.""" +import pytest + from redisvl.query.aggregate import AggregateHybridQuery from redisvl.utils.full_text_query_helper import FullTextQueryHelper +try: + from redisvl.query.hybrid import HybridQuery +except (ImportError, ModuleNotFoundError): + HybridQuery = None # type: ignore + + +def _hybrid_classes(): + classes = [pytest.param(AggregateHybridQuery, id="AggregateHybridQuery")] + classes.append( + pytest.param( + HybridQuery, + id="HybridQuery", + marks=pytest.mark.skipif( + HybridQuery is None, reason="Requires redis-py>=7.1.0" + ), + ) + ) + return classes + def test_stopword_with_apostrophe_is_filtered(): """Stopword membership is checked against the word the user typed.""" helper = FullTextQueryHelper() - assert helper._tokenize_and_escape_query("it's the fox") == "fox" + assert helper.build_query_string("it's the fox", "body") == "(~@body:(fox))" -def test_text_weight_key_with_apostrophe_applies(): +def test_text_weight_key_with_apostrophe_is_attached_to_its_term(): """text_weights keys match the raw word, not its escaped form.""" helper = FullTextQueryHelper(stopwords=None, text_weights={"don't": 2.0}) - assert helper._tokenize_and_escape_query("don't jump") == ( - "don\\'t=>{$weight:2.0} | jump" + assert helper.build_query_string("don't jump", "body") == ( + "(~@body:(don\\'t=>{$weight:2.0} | jump))" ) -def test_aggregate_hybrid_text_clause_drops_apostrophe_stopword(): - """The clause a hybrid query sends contains no escaped stopword.""" - query = AggregateHybridQuery( +@pytest.mark.parametrize("query_class", _hybrid_classes()) +def test_hybrid_text_clause_drops_apostrophe_stopword(query_class): + """The clause both hybrid queries send contains no escaped stopword.""" + query = query_class( text="it's the fox", text_field_name="body", vector=[1.0, 0.0], vector_field_name="v", ) - assert "(~@body:(fox))" in str(query) + sent = str(query) if query_class is AggregateHybridQuery else query.query.get_args() + assert "(~@body:(fox))" in str(sent) + + +@pytest.mark.parametrize("text", ["it's", "won't", "you're not"]) +def test_contraction_only_text_is_rejected(text): + """Text made only of contractions and stopwords now fails at construction.""" + helper = FullTextQueryHelper() + with pytest.raises(ValueError, match="empty after removing stopwords"): + helper.build_query_string(text, "body") From bd2c7bf6e4526cd8448e9ae2e605eb67e5724b18 Mon Sep 17 00:00:00 2001 From: Christopher Krah <48338417+chrikrah@users.noreply.github.com> Date: Tue, 24 Nov 2026 13:19:00 +0100 Subject: [PATCH 3/3] test(query): skip the HybridQuery case without redis-py's hybrid module redisvl.query.hybrid imports on any redis-py and only needs redis-py's hybrid API once a HybridQuery is built, so the import guard never set HybridQuery to None and the redis-py 6.x jobs failed with ImportError. Import redis.commands.search.hybrid_query first, as tests/unit/test_hybrid_types.py does, so the skip applies. Signed-off-by: Christopher Krah <48338417+chrikrah@users.noreply.github.com> --- tests/unit/test_full_text_query_helper.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/unit/test_full_text_query_helper.py b/tests/unit/test_full_text_query_helper.py index 71fa5e2d3..d76c2bedd 100644 --- a/tests/unit/test_full_text_query_helper.py +++ b/tests/unit/test_full_text_query_helper.py @@ -6,8 +6,12 @@ from redisvl.utils.full_text_query_helper import FullTextQueryHelper try: + # redisvl.query.hybrid imports on any redis-py; building a HybridQuery + # needs redis-py>=7.1.0, which provides this module. + import redis.commands.search.hybrid_query # noqa: F401 + from redisvl.query.hybrid import HybridQuery -except (ImportError, ModuleNotFoundError): +except ImportError: HybridQuery = None # type: ignore