Skip to content

Commit

Permalink
update index creation routine to respect overwrite=False
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerhutcherson committed Oct 31, 2023
1 parent b69b2b3 commit a3641c6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
35 changes: 27 additions & 8 deletions redisvl/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,14 +332,21 @@ def create(self, overwrite: Optional[bool] = False):

if not self._fields:
raise ValueError("No fields defined for index")

if self.exists() and overwrite:
if not isinstance(overwrite, bool):
raise TypeError("overwrite must be of type bool")

if self.exists():
if not overwrite:
print("Index already exists, not overwriting.")
return None
print("Index already exists, overwriting.")
self.delete()

# set storage_type, default to hash
storage_type = IndexType.HASH
if self._storage.lower() == "json":
storage_type = IndexType.JSON
# TODO - enable JSON support
# if self._storage.lower() == "json":
# storage_type = IndexType.JSON

# Create Index
# will raise correct response error if index already exists
Expand Down Expand Up @@ -510,14 +517,26 @@ async def create(self, overwrite: Optional[bool] = False):
Raises:
redis.exceptions.ResponseError: If the index already exists.
"""
exists = await self.exists()
if exists and overwrite:
# TODO - enable async version of this
# check_redis_modules_exist(self._redis_conn)

if not self._fields:
raise ValueError("No fields defined for index")
if not isinstance(overwrite, bool):
raise TypeError("overwrite must be of type bool")

if await self.exists():
if not overwrite:
print("Index already exists, not overwriting.")
return None
print("Index already exists, overwriting.")
await self.delete()

# set storage_type, default to hash
storage_type = IndexType.HASH
if self._storage.lower() == "json":
storage_type = IndexType.JSON
# TODO - enable JSON support
# if self._storage.lower() == "json":
# storage_type = IndexType.JSON

# Create Index
await self._redis_conn.ft(self._name).create_index( # type: ignore
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ def test_search_index_create(client, redis_url):
s1_2 = SearchIndex.from_existing("my_index", url=redis_url)
assert s1_2.info()["index_name"] == si.info()["index_name"]

si.create(overwrite=False)
assert si.exists()
assert "my_index" in convert_bytes(si.client.execute_command("FT._LIST"))


def test_search_index_delete(client):
si = SearchIndex("my_index", fields=fields)
Expand Down

0 comments on commit a3641c6

Please sign in to comment.