Skip to content

Commit 2e5ba37

Browse files
Fix index loading empty array (#173)
This PR fixes the case when a user tries to load an empty array using `index.load([], ...)`. Currently, this borks and throws an exception for something that should have been captured in one of our tests. Test conditions added, and big fixed.
1 parent 114637a commit 2e5ba37

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

redisvl/index/storage.py

+21-20
Original file line numberDiff line numberDiff line change
@@ -210,27 +210,28 @@ def write(
210210
keys_iterator = iter(keys) if keys else None
211211
added_keys: List[str] = []
212212

213-
with redis_client.pipeline(transaction=False) as pipe:
214-
for i, obj in enumerate(objects, start=1):
215-
# Construct key, validate, and write
216-
key = (
217-
next(keys_iterator)
218-
if keys_iterator
219-
else self._create_key(obj, id_field)
220-
)
221-
obj = self._preprocess(obj, preprocess)
222-
self._validate(obj)
223-
self._set(pipe, key, obj)
224-
# Set TTL if provided
225-
if ttl:
226-
pipe.expire(key, ttl)
227-
# Execute mini batch
228-
if i % batch_size == 0:
213+
if objects:
214+
with redis_client.pipeline(transaction=False) as pipe:
215+
for i, obj in enumerate(objects, start=1):
216+
# Construct key, validate, and write
217+
key = (
218+
next(keys_iterator)
219+
if keys_iterator
220+
else self._create_key(obj, id_field)
221+
)
222+
obj = self._preprocess(obj, preprocess)
223+
self._validate(obj)
224+
self._set(pipe, key, obj)
225+
# Set TTL if provided
226+
if ttl:
227+
pipe.expire(key, ttl)
228+
# Execute mini batch
229+
if i % batch_size == 0:
230+
pipe.execute()
231+
added_keys.append(key)
232+
# Clean up batches if needed
233+
if i % batch_size != 0:
229234
pipe.execute()
230-
added_keys.append(key)
231-
# Clean up batches if needed
232-
if i % batch_size != 0:
233-
pipe.execute()
234235

235236
return added_keys
236237

tests/unit/test_async_search_index.py

+7
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ async def bad_preprocess(record):
129129
await async_index.load(data, id_field="id", preprocess=bad_preprocess)
130130

131131

132+
@pytest.mark.asyncio
133+
async def test_search_index_load_empty(async_client, async_index):
134+
async_index.set_client(async_client)
135+
await async_index.create(overwrite=True, drop=True)
136+
await async_index.load([])
137+
138+
132139
@pytest.mark.asyncio
133140
async def test_no_id_field(async_client, async_index):
134141
async_index.set_client(async_client)

tests/unit/test_search_index.py

+6
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ def bad_preprocess(record):
125125
index.load(data, id_field="id", preprocess=bad_preprocess)
126126

127127

128+
def test_search_index_load_empty(client, index):
129+
index.set_client(client)
130+
index.create(overwrite=True, drop=True)
131+
index.load([])
132+
133+
128134
def test_no_id_field(client, index):
129135
index.set_client(client)
130136
index.create(overwrite=True, drop=True)

0 commit comments

Comments
 (0)