Skip to content

Commit 45c5e9f

Browse files
adds drop_keys and async drop_keys methods to index
1 parent 042f7d8 commit 45c5e9f

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

redisvl/index/index.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,19 @@ def clear(self) -> int:
497497

498498
return total_records_deleted
499499

500+
def drop_keys(self, keys: Union[str, List[str]]) -> None:
501+
"""Remove a specific entry or entries from the index by it's key ID.
502+
Args:
503+
keys (Union[str, List[str]]): The document ID or IDs to remove from the index.
504+
"""
505+
if isinstance(keys, List):
506+
with self._redis_client.pipeline(transaction=False) as pipe: # type: ignore
507+
for key in keys: # type: ignore
508+
pipe.delete(key)
509+
pipe.execute()
510+
else:
511+
self._redis_client.delete(keys) # type: ignore
512+
500513
def load(
501514
self,
502515
data: Iterable[Any],
@@ -935,6 +948,16 @@ async def clear(self) -> int:
935948

936949
return total_records_deleted
937950

951+
async def drop_keys(self, keys: Union[str, List[str]]) -> None:
952+
"""Remove a specific entry or entries from the index by it's key ID.
953+
Args:
954+
keys (Union[str, List[str]]): The document ID or IDs to remove from the index.
955+
"""
956+
if isinstance(keys, List):
957+
await self._redis_client.delete(*keys) # type: ignore
958+
else:
959+
await self._redis_client.delete(keys) # type: ignore
960+
938961
async def load(
939962
self,
940963
data: Iterable[Any],

tests/integration/test_async_search_index.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,37 @@ async def test_search_index_clear(async_client, async_index):
184184
assert await async_index.exists()
185185

186186

187+
@pytest.mark.asyncio
188+
async def test_search_index_drop_key(async_client, async_index):
189+
async_index.set_client(async_client)
190+
await async_index.create(overwrite=True, drop=True)
191+
data = [{"id": "1", "test": "foo"}, {"id": "2", "test": "bar"}]
192+
keys = await async_index.load(data, id_field="id")
193+
194+
await async_index.drop_keys(keys[0])
195+
assert not await async_index.fetch(keys[0])
196+
assert await async_index.fetch(keys[1]) is not None
197+
198+
199+
@pytest.mark.asyncio
200+
async def test_search_index_drop_keys(async_client, async_index):
201+
async_index.set_client(async_client)
202+
await async_index.create(overwrite=True, drop=True)
203+
data = [
204+
{"id": "1", "test": "foo"},
205+
{"id": "2", "test": "bar"},
206+
{"id": "3", "test": "baz"},
207+
]
208+
keys = await async_index.load(data, id_field="id")
209+
210+
await async_index.drop_keys(keys[0:2])
211+
assert not await async_index.fetch(keys[0])
212+
assert not await async_index.fetch(keys[1])
213+
assert await async_index.fetch(keys[2]) is not None
214+
215+
assert await async_index.exists()
216+
217+
187218
@pytest.mark.asyncio
188219
async def test_search_index_load_and_fetch(async_client, async_index):
189220
async_index.set_client(async_client)

tests/integration/test_search_index.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,37 @@ def test_search_index_clear(client, index):
170170
assert index.exists()
171171

172172

173+
def test_search_index_drop_key(client, index):
174+
index.set_client(client)
175+
index.create(overwrite=True, drop=True)
176+
data = [{"id": "1", "test": "foo"}, {"id": "2", "test": "bar"}]
177+
keys = index.load(data, id_field="id")
178+
179+
# test passing a single string key removes only that key
180+
index.drop_keys(keys[0])
181+
assert not index.fetch(keys[0])
182+
assert index.fetch(keys[1]) is not None # still have all other entries
183+
184+
185+
def test_search_index_drop_keys(client, index):
186+
index.set_client(client)
187+
index.create(overwrite=True, drop=True)
188+
data = [
189+
{"id": "1", "test": "foo"},
190+
{"id": "2", "test": "bar"},
191+
{"id": "3", "test": "baz"},
192+
]
193+
keys = index.load(data, id_field="id")
194+
195+
# test passing a list of keys selectively removes only those keys
196+
index.drop_keys(keys[0:2])
197+
assert not index.fetch(keys[0])
198+
assert not index.fetch(keys[1])
199+
assert index.fetch(keys[2]) is not None
200+
201+
assert index.exists()
202+
203+
173204
def test_search_index_load_and_fetch(client, index):
174205
index.set_client(client)
175206
index.create(overwrite=True, drop=True)

0 commit comments

Comments
 (0)