|
3 | 3 | import pytest |
4 | 4 |
|
5 | 5 | from redisvl.index import SearchIndex |
6 | | -from redisvl.query import FilterQuery |
| 6 | +from redisvl.query import AggregateHybridQuery, FilterQuery, TextQuery |
| 7 | +from redisvl.query.filter import Tag |
| 8 | +from redisvl.redis.utils import array_to_buffer |
7 | 9 | from redisvl.schema import IndexSchema |
| 10 | +from tests.conftest import skip_if_redis_version_below |
8 | 11 |
|
9 | 12 |
|
10 | 13 | @pytest.fixture |
@@ -91,6 +94,56 @@ def default_stopwords_index(client, default_stopwords_schema): |
91 | 94 | index.delete(drop=True) |
92 | 95 |
|
93 | 96 |
|
| 97 | +@pytest.fixture |
| 98 | +def filtered_queries_stopwords_disabled_index(redis_url, redis_test_name): |
| 99 | + """Index with STOPWORDS 0 for filtered text and hybrid query regressions.""" |
| 100 | + index_name = redis_test_name("filtered_queries_stopwords_disabled") |
| 101 | + index = SearchIndex.from_dict( |
| 102 | + { |
| 103 | + "index": { |
| 104 | + "name": index_name, |
| 105 | + "prefix": f"{index_name}:", |
| 106 | + "storage_type": "hash", |
| 107 | + "stopwords": [], |
| 108 | + }, |
| 109 | + "fields": [ |
| 110 | + {"name": "text", "type": "text"}, |
| 111 | + {"name": "team", "type": "tag"}, |
| 112 | + { |
| 113 | + "name": "embedding", |
| 114 | + "type": "vector", |
| 115 | + "attrs": { |
| 116 | + "dims": 2, |
| 117 | + "distance_metric": "cosine", |
| 118 | + "algorithm": "flat", |
| 119 | + "datatype": "float32", |
| 120 | + }, |
| 121 | + }, |
| 122 | + ], |
| 123 | + }, |
| 124 | + redis_url=redis_url, |
| 125 | + ) |
| 126 | + index.create(overwrite=True, drop=True) |
| 127 | + index.load( |
| 128 | + [ |
| 129 | + { |
| 130 | + "text": "reference handbook", |
| 131 | + "team": "docs", |
| 132 | + "embedding": array_to_buffer([1.0, 0.0], "float32"), |
| 133 | + }, |
| 134 | + { |
| 135 | + "text": "reference handbook", |
| 136 | + "team": "support", |
| 137 | + "embedding": array_to_buffer([1.0, 0.0], "float32"), |
| 138 | + }, |
| 139 | + ] |
| 140 | + ) |
| 141 | + |
| 142 | + yield index |
| 143 | + |
| 144 | + index.delete(drop=True) |
| 145 | + |
| 146 | + |
94 | 147 | def test_create_index_with_stopwords_disabled(client, stopwords_disabled_index): |
95 | 148 | """Test creating an index with STOPWORDS 0.""" |
96 | 149 | # Verify index was created |
@@ -190,3 +243,46 @@ def test_stopwords_disabled_allows_searching_common_words( |
190 | 243 | # With STOPWORDS 0, "of" should be indexed and searchable |
191 | 244 | assert len(results.docs) > 0 |
192 | 245 | assert any("of" in doc.title.lower() for doc in results.docs) |
| 246 | + |
| 247 | + |
| 248 | +def test_filtered_text_query_with_stopwords_disabled( |
| 249 | + filtered_queries_stopwords_disabled_index, |
| 250 | +): |
| 251 | + """Filtered text queries should not add AND as a full-text search term.""" |
| 252 | + query = TextQuery( |
| 253 | + text="handbook", |
| 254 | + text_field_name="text", |
| 255 | + filter_expression=Tag("team") == "docs", |
| 256 | + return_fields=["text", "team"], |
| 257 | + stopwords=None, |
| 258 | + ) |
| 259 | + |
| 260 | + results = filtered_queries_stopwords_disabled_index.query(query) |
| 261 | + |
| 262 | + assert len(results) == 1 |
| 263 | + assert results[0]["text"] == "reference handbook" |
| 264 | + assert results[0]["team"] == "docs" |
| 265 | + |
| 266 | + |
| 267 | +def test_filtered_aggregate_hybrid_query_with_stopwords_disabled( |
| 268 | + filtered_queries_stopwords_disabled_index, |
| 269 | +): |
| 270 | + """Filtered aggregate hybrid queries should work with STOPWORDS 0.""" |
| 271 | + skip_if_redis_version_below( |
| 272 | + filtered_queries_stopwords_disabled_index.client, "7.2.0" |
| 273 | + ) |
| 274 | + query = AggregateHybridQuery( |
| 275 | + text="handbook", |
| 276 | + text_field_name="text", |
| 277 | + vector=[1.0, 0.0], |
| 278 | + vector_field_name="embedding", |
| 279 | + filter_expression=Tag("team") == "docs", |
| 280 | + return_fields=["text", "team"], |
| 281 | + stopwords=None, |
| 282 | + ) |
| 283 | + |
| 284 | + results = filtered_queries_stopwords_disabled_index.query(query) |
| 285 | + |
| 286 | + assert len(results) == 1 |
| 287 | + assert results[0]["text"] == "reference handbook" |
| 288 | + assert results[0]["team"] == "docs" |
0 commit comments