|
4 | 4 | import pytest |
5 | 5 | from testcontainers.compose import DockerCompose |
6 | 6 |
|
| 7 | +from redisvl.index.index import AsyncSearchIndex, SearchIndex |
7 | 8 | from redisvl.redis.connection import RedisConnectionFactory |
| 9 | +from redisvl.redis.utils import array_to_buffer |
8 | 10 | from redisvl.utils.vectorize import HFTextVectorizer |
9 | 11 |
|
10 | 12 |
|
@@ -191,3 +193,111 @@ def pytest_collection_modifyitems( |
191 | 193 | for item in items: |
192 | 194 | if item.get_closest_marker("requires_api_keys"): |
193 | 195 | item.add_marker(skip_api) |
| 196 | + |
| 197 | + |
| 198 | +@pytest.fixture |
| 199 | +def flat_index(sample_data, redis_url): |
| 200 | + """ |
| 201 | + A fixture that uses the "flag" algorithm for its vector field. |
| 202 | + """ |
| 203 | + # construct a search index from the schema |
| 204 | + index = SearchIndex.from_dict( |
| 205 | + { |
| 206 | + "index": { |
| 207 | + "name": "user_index", |
| 208 | + "prefix": "v1", |
| 209 | + "storage_type": "hash", |
| 210 | + }, |
| 211 | + "fields": [ |
| 212 | + {"name": "description", "type": "text"}, |
| 213 | + {"name": "credit_score", "type": "tag"}, |
| 214 | + {"name": "job", "type": "text"}, |
| 215 | + {"name": "age", "type": "numeric"}, |
| 216 | + {"name": "last_updated", "type": "numeric"}, |
| 217 | + {"name": "location", "type": "geo"}, |
| 218 | + { |
| 219 | + "name": "user_embedding", |
| 220 | + "type": "vector", |
| 221 | + "attrs": { |
| 222 | + "dims": 3, |
| 223 | + "distance_metric": "cosine", |
| 224 | + "algorithm": "flat", |
| 225 | + "datatype": "float32", |
| 226 | + }, |
| 227 | + }, |
| 228 | + ], |
| 229 | + }, |
| 230 | + redis_url=redis_url, |
| 231 | + ) |
| 232 | + |
| 233 | + # create the index (no data yet) |
| 234 | + index.create(overwrite=True) |
| 235 | + |
| 236 | + # Prepare and load the data |
| 237 | + def hash_preprocess(item: dict) -> dict: |
| 238 | + return { |
| 239 | + **item, |
| 240 | + "user_embedding": array_to_buffer(item["user_embedding"], "float32"), |
| 241 | + } |
| 242 | + |
| 243 | + index.load(sample_data, preprocess=hash_preprocess) |
| 244 | + |
| 245 | + # run the test |
| 246 | + yield index |
| 247 | + |
| 248 | + # clean up |
| 249 | + index.delete(drop=True) |
| 250 | + |
| 251 | + |
| 252 | +@pytest.fixture |
| 253 | +async def async_flat_index(sample_data, redis_url): |
| 254 | + """ |
| 255 | + A fixture that uses the "flag" algorithm for its vector field. |
| 256 | + """ |
| 257 | + # construct a search index from the schema |
| 258 | + index = AsyncSearchIndex.from_dict( |
| 259 | + { |
| 260 | + "index": { |
| 261 | + "name": "user_index", |
| 262 | + "prefix": "v1", |
| 263 | + "storage_type": "hash", |
| 264 | + }, |
| 265 | + "fields": [ |
| 266 | + {"name": "description", "type": "text"}, |
| 267 | + {"name": "credit_score", "type": "tag"}, |
| 268 | + {"name": "job", "type": "text"}, |
| 269 | + {"name": "age", "type": "numeric"}, |
| 270 | + {"name": "last_updated", "type": "numeric"}, |
| 271 | + {"name": "location", "type": "geo"}, |
| 272 | + { |
| 273 | + "name": "user_embedding", |
| 274 | + "type": "vector", |
| 275 | + "attrs": { |
| 276 | + "dims": 3, |
| 277 | + "distance_metric": "cosine", |
| 278 | + "algorithm": "flat", |
| 279 | + "datatype": "float32", |
| 280 | + }, |
| 281 | + }, |
| 282 | + ], |
| 283 | + }, |
| 284 | + redis_url=redis_url, |
| 285 | + ) |
| 286 | + |
| 287 | + # create the index (no data yet) |
| 288 | + await index.create(overwrite=True) |
| 289 | + |
| 290 | + # Prepare and load the data |
| 291 | + def hash_preprocess(item: dict) -> dict: |
| 292 | + return { |
| 293 | + **item, |
| 294 | + "user_embedding": array_to_buffer(item["user_embedding"], "float32"), |
| 295 | + } |
| 296 | + |
| 297 | + await index.load(sample_data, preprocess=hash_preprocess) |
| 298 | + |
| 299 | + # run the test |
| 300 | + yield index |
| 301 | + |
| 302 | + # clean up |
| 303 | + await index.delete(drop=True) |
0 commit comments