Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix index loading empty array #173

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 21 additions & 20 deletions redisvl/index/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,27 +210,28 @@ def write(
keys_iterator = iter(keys) if keys else None
added_keys: List[str] = []

with redis_client.pipeline(transaction=False) as pipe:
for i, obj in enumerate(objects, start=1):
# Construct key, validate, and write
key = (
next(keys_iterator)
if keys_iterator
else self._create_key(obj, id_field)
)
obj = self._preprocess(obj, preprocess)
self._validate(obj)
self._set(pipe, key, obj)
# Set TTL if provided
if ttl:
pipe.expire(key, ttl)
# Execute mini batch
if i % batch_size == 0:
if objects:
with redis_client.pipeline(transaction=False) as pipe:
for i, obj in enumerate(objects, start=1):
# Construct key, validate, and write
key = (
next(keys_iterator)
if keys_iterator
else self._create_key(obj, id_field)
)
obj = self._preprocess(obj, preprocess)
self._validate(obj)
self._set(pipe, key, obj)
# Set TTL if provided
if ttl:
pipe.expire(key, ttl)
# Execute mini batch
if i % batch_size == 0:
pipe.execute()
added_keys.append(key)
# Clean up batches if needed
if i % batch_size != 0:
pipe.execute()
added_keys.append(key)
# Clean up batches if needed
if i % batch_size != 0:
pipe.execute()

return added_keys

Expand Down
7 changes: 7 additions & 0 deletions tests/unit/test_async_search_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ async def bad_preprocess(record):
await async_index.load(data, id_field="id", preprocess=bad_preprocess)


@pytest.mark.asyncio
async def test_search_index_load_empty(async_client, async_index):
async_index.set_client(async_client)
await async_index.create(overwrite=True, drop=True)
await async_index.load([])


@pytest.mark.asyncio
async def test_no_id_field(async_client, async_index):
async_index.set_client(async_client)
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/test_search_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ def bad_preprocess(record):
index.load(data, id_field="id", preprocess=bad_preprocess)


def test_search_index_load_empty(client, index):
index.set_client(client)
index.create(overwrite=True, drop=True)
index.load([])


def test_no_id_field(client, index):
index.set_client(client)
index.create(overwrite=True, drop=True)
Expand Down
Loading