|
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,211 @@ 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) |
| 304 | + |
| 305 | + |
| 306 | +@pytest.fixture |
| 307 | +async def async_hnsw_index(sample_data, redis_url): |
| 308 | + """ |
| 309 | + A fixture that uses the "hnsw" algorithm for its vector field. |
| 310 | + """ |
| 311 | + index = AsyncSearchIndex.from_dict( |
| 312 | + { |
| 313 | + "index": { |
| 314 | + "name": "user_index", |
| 315 | + "prefix": "v1", |
| 316 | + "storage_type": "hash", |
| 317 | + }, |
| 318 | + "fields": [ |
| 319 | + {"name": "description", "type": "text"}, |
| 320 | + {"name": "credit_score", "type": "tag"}, |
| 321 | + {"name": "job", "type": "text"}, |
| 322 | + {"name": "age", "type": "numeric"}, |
| 323 | + {"name": "last_updated", "type": "numeric"}, |
| 324 | + {"name": "location", "type": "geo"}, |
| 325 | + { |
| 326 | + "name": "user_embedding", |
| 327 | + "type": "vector", |
| 328 | + "attrs": { |
| 329 | + "dims": 3, |
| 330 | + "distance_metric": "cosine", |
| 331 | + "algorithm": "hnsw", |
| 332 | + "datatype": "float32", |
| 333 | + }, |
| 334 | + }, |
| 335 | + ], |
| 336 | + }, |
| 337 | + redis_url=redis_url, |
| 338 | + ) |
| 339 | + |
| 340 | + # create the index (no data yet) |
| 341 | + await index.create(overwrite=True) |
| 342 | + |
| 343 | + # Prepare and load the data |
| 344 | + def hash_preprocess(item: dict) -> dict: |
| 345 | + return { |
| 346 | + **item, |
| 347 | + "user_embedding": array_to_buffer(item["user_embedding"], "float32"), |
| 348 | + } |
| 349 | + |
| 350 | + await index.load(sample_data, preprocess=hash_preprocess) |
| 351 | + |
| 352 | + # run the test |
| 353 | + yield index |
| 354 | + |
| 355 | + |
| 356 | +@pytest.fixture |
| 357 | +def hnsw_index(sample_data, redis_url): |
| 358 | + """ |
| 359 | + A fixture that uses the "hnsw" algorithm for its vector field. |
| 360 | + """ |
| 361 | + index = SearchIndex.from_dict( |
| 362 | + { |
| 363 | + "index": { |
| 364 | + "name": "user_index", |
| 365 | + "prefix": "v1", |
| 366 | + "storage_type": "hash", |
| 367 | + }, |
| 368 | + "fields": [ |
| 369 | + {"name": "description", "type": "text"}, |
| 370 | + {"name": "credit_score", "type": "tag"}, |
| 371 | + {"name": "job", "type": "text"}, |
| 372 | + {"name": "age", "type": "numeric"}, |
| 373 | + {"name": "last_updated", "type": "numeric"}, |
| 374 | + {"name": "location", "type": "geo"}, |
| 375 | + { |
| 376 | + "name": "user_embedding", |
| 377 | + "type": "vector", |
| 378 | + "attrs": { |
| 379 | + "dims": 3, |
| 380 | + "distance_metric": "cosine", |
| 381 | + "algorithm": "hnsw", |
| 382 | + "datatype": "float32", |
| 383 | + }, |
| 384 | + }, |
| 385 | + ], |
| 386 | + }, |
| 387 | + redis_url=redis_url, |
| 388 | + ) |
| 389 | + |
| 390 | + # create the index (no data yet) |
| 391 | + index.create(overwrite=True) |
| 392 | + |
| 393 | + # Prepare and load the data |
| 394 | + def hash_preprocess(item: dict) -> dict: |
| 395 | + return { |
| 396 | + **item, |
| 397 | + "user_embedding": array_to_buffer(item["user_embedding"], "float32"), |
| 398 | + } |
| 399 | + |
| 400 | + index.load(sample_data, preprocess=hash_preprocess) |
| 401 | + |
| 402 | + # run the test |
| 403 | + yield index |
0 commit comments