Skip to content

Commit fb87d93

Browse files
drop_keys returns number of records removed from Redis
1 parent 1499bfb commit fb87d93

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

redisvl/index/index.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -497,15 +497,19 @@ def clear(self) -> int:
497497

498498
return total_records_deleted
499499

500-
def drop_keys(self, keys: Union[str, List[str]]) -> None:
500+
def drop_keys(self, keys: Union[str, List[str]]) -> int:
501501
"""Remove a specific entry or entries from the index by it's key ID.
502+
502503
Args:
503504
keys (Union[str, List[str]]): The document ID or IDs to remove from the index.
505+
506+
Returns:
507+
int: Count of records deleted from Redis.
504508
"""
505509
if isinstance(keys, List):
506-
self._redis_client.delete(*keys) # type: ignore
510+
return self._redis_client.delete(*keys) # type: ignore
507511
else:
508-
self._redis_client.delete(keys) # type: ignore
512+
return self._redis_client.delete(keys) # type: ignore
509513

510514
def load(
511515
self,
@@ -945,15 +949,19 @@ async def clear(self) -> int:
945949

946950
return total_records_deleted
947951

948-
async def drop_keys(self, keys: Union[str, List[str]]) -> None:
952+
async def drop_keys(self, keys: Union[str, List[str]]) -> int:
949953
"""Remove a specific entry or entries from the index by it's key ID.
954+
950955
Args:
951956
keys (Union[str, List[str]]): The document ID or IDs to remove from the index.
957+
958+
Returns:
959+
int: Count of records deleted from Redis.
952960
"""
953961
if isinstance(keys, List):
954-
await self._redis_client.delete(*keys) # type: ignore
962+
return await self._redis_client.delete(*keys) # type: ignore
955963
else:
956-
await self._redis_client.delete(keys) # type: ignore
964+
return await self._redis_client.delete(keys) # type: ignore
957965

958966
async def load(
959967
self,

tests/integration/test_async_search_index.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ async def test_search_index_drop_key(async_client, async_index):
191191
data = [{"id": "1", "test": "foo"}, {"id": "2", "test": "bar"}]
192192
keys = await async_index.load(data, id_field="id")
193193

194-
await async_index.drop_keys(keys[0])
194+
dropped = await async_index.drop_keys(keys[0])
195+
assert dropped == 1
195196
assert not await async_index.fetch(keys[0])
196197
assert await async_index.fetch(keys[1]) is not None
197198

@@ -207,7 +208,8 @@ async def test_search_index_drop_keys(async_client, async_index):
207208
]
208209
keys = await async_index.load(data, id_field="id")
209210

210-
await async_index.drop_keys(keys[0:2])
211+
dropped = await async_index.drop_keys(keys[0:2])
212+
assert dropped == 2
211213
assert not await async_index.fetch(keys[0])
212214
assert not await async_index.fetch(keys[1])
213215
assert await async_index.fetch(keys[2]) is not None

tests/integration/test_search_index.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ def test_search_index_drop_key(client, index):
177177
keys = index.load(data, id_field="id")
178178

179179
# test passing a single string key removes only that key
180-
index.drop_keys(keys[0])
180+
dropped = index.drop_keys(keys[0])
181+
assert dropped == 1
181182
assert not index.fetch(keys[0])
182183
assert index.fetch(keys[1]) is not None # still have all other entries
183184

@@ -193,7 +194,8 @@ def test_search_index_drop_keys(client, index):
193194
keys = index.load(data, id_field="id")
194195

195196
# test passing a list of keys selectively removes only those keys
196-
index.drop_keys(keys[0:2])
197+
dropped = index.drop_keys(keys[0:2])
198+
assert dropped == 2
197199
assert not index.fetch(keys[0])
198200
assert not index.fetch(keys[1])
199201
assert index.fetch(keys[2]) is not None

0 commit comments

Comments
 (0)