Skip to content

Commit c06a787

Browse files
Fix index.fetch() bug (#120)
`index.fetch()` was accidentally hardcoded to always pull data from hash, even if the schema declared JSON for data storage. Updated the method to use the `Storage` class for handling the get operation and updated the tests.
1 parent bcb27dd commit c06a787

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

redisvl/index/index.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ def add_field(d):
524524
logger.exception("Error while loading data to Redis")
525525
raise
526526

527-
def fetch(self, id: str) -> Dict[str, Any]:
527+
def fetch(self, id: str) -> Optional[Dict[str, Any]]:
528528
"""Fetch an object from Redis by id.
529529
530530
The id is typically either a unique identifier,
@@ -538,7 +538,10 @@ def fetch(self, id: str) -> Dict[str, Any]:
538538
Returns:
539539
Dict[str, Any]: The fetched object.
540540
"""
541-
return convert_bytes(self._redis_client.hgetall(self.key(id))) # type: ignore
541+
obj = self._storage.get(self._redis_client, [self.key(id)]) # type: ignore
542+
if obj:
543+
return convert_bytes(obj[0])
544+
return None
542545

543546
@check_index_exists()
544547
def search(self, *args, **kwargs) -> "Result":
@@ -901,7 +904,7 @@ async def add_field(d):
901904
logger.exception("Error while loading data to Redis")
902905
raise
903906

904-
async def fetch(self, id: str) -> Dict[str, Any]:
907+
async def fetch(self, id: str) -> Optional[Dict[str, Any]]:
905908
"""Asynchronously etch an object from Redis by id. The id is typically
906909
either a unique identifier, or derived from some domain-specific
907910
metadata combination (like a document id or chunk id).
@@ -913,7 +916,10 @@ async def fetch(self, id: str) -> Dict[str, Any]:
913916
Returns:
914917
Dict[str, Any]: The fetched object.
915918
"""
916-
return convert_bytes(await self._redis_client.hgetall(self.key(id))) # type: ignore
919+
obj = await self._storage.aget(self._redis_client, [self.key(id)]) # type: ignore
920+
if obj:
921+
return convert_bytes(obj[0])
922+
return None
917923

918924
@check_async_index_exists()
919925
async def search(self, *args, **kwargs) -> "Result":

tests/integration/test_flow.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,11 @@ def hash_preprocess(item: dict) -> dict:
5454
return {**item, "user_embedding": array_to_buffer(item["user_embedding"])}
5555

5656
if index.storage_type == StorageType.HASH:
57-
index.load(sample_data, preprocess=hash_preprocess)
57+
index.load(sample_data, preprocess=hash_preprocess, id_field="user")
5858
else:
59-
index.load(sample_data)
59+
index.load(sample_data, id_field="user")
60+
61+
assert index.fetch("john")
6062

6163
return_fields = ["user", "age", "job", "credit_score"]
6264
query = VectorQuery(

tests/integration/test_flow_async.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,11 @@ async def hash_preprocess(item: dict) -> dict:
5757
return {**item, "user_embedding": array_to_buffer(item["user_embedding"])}
5858

5959
if index.storage_type == StorageType.HASH:
60-
await index.load(sample_data, preprocess=hash_preprocess)
60+
await index.load(sample_data, preprocess=hash_preprocess, id_field="user")
6161
else:
62-
await index.load(sample_data)
62+
await index.load(sample_data, id_field="user")
63+
64+
assert await index.fetch("john")
6365

6466
# wait for async index to create
6567
time.sleep(1)

0 commit comments

Comments
 (0)