Skip to content

Commit

Permalink
unify redis_url usage
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerhutcherson committed Nov 17, 2023
1 parent 194bd50 commit 5cd5f87
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 32 deletions.
6 changes: 5 additions & 1 deletion docs/user_guide/getting_started_01.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@
"index:\n",
" name: user_index\n",
" prefix: user\n",
" storage_type: hash\n",
" key_separator: ':'\n",
"\n",
"fields:\n",
" # define tag fields\n",
Expand Down Expand Up @@ -162,6 +164,8 @@
" \"index\": {\n",
" \"name\": \"user_index\",\n",
" \"prefix\": \"user\",\n",
" \"storage_type\": \"hash\",\n",
" \"key_separator\": \":\"\n",
" },\n",
" \"fields\": {\n",
" \"tag\": [{\"name\": \"credit_score\"}],\n",
Expand Down Expand Up @@ -465,7 +469,7 @@
],
"source": [
"# create a new SearchIndex instance from an existing index\n",
"existing_index = SearchIndex.from_existing(\"user_index\", \"redis://localhost:6379\")\n",
"existing_index = SearchIndex.from_existing(name=\"user_index\", redis_url=\"redis://localhost:6379\")\n",
"\n",
"# run the same query\n",
"results = existing_index.query(query)\n",
Expand Down
3 changes: 2 additions & 1 deletion docs/user_guide/hybrid_queries_02.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
" \"index\": {\n",
" \"name\": \"user_index\",\n",
" \"prefix\": \"v1\",\n",
" \"storage_type\": \"hash\",\n",
" \"key_separator\": \":\"\n",
" },\n",
" \"fields\": {\n",
" \"tag\": [{\"name\": \"credit_score\"}],\n",
Expand Down Expand Up @@ -731,7 +733,6 @@
"metadata": {},
"outputs": [],
"source": [
"#\n",
"def make_filter(age=None, credit=None, job=None):\n",
" flexible_filter = (\n",
" (Num(\"age\") > age) &\n",
Expand Down
1 change: 1 addition & 0 deletions docs/user_guide/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ index:
name: providers
prefix: rvl
storage_type: hash
key_separator: ':'

fields:
text:
Expand Down
2 changes: 2 additions & 0 deletions docs/user_guide/vectorizers_04.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@
"index:\n",
" name: providers\n",
" prefix: rvl\n",
" storage_type: hash\n",
" key_separator: ':'\n",
"\n",
"fields:\n",
" text:\n",
Expand Down
14 changes: 7 additions & 7 deletions redisvl/cli/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ def create(self, args: Namespace):
if not args.schema:
logger.error("Schema must be provided to create an index")
index = SearchIndex.from_yaml(args.schema)
url = create_redis_url(args)
index.connect(url)
redis_url = create_redis_url(args)
index.connect(redis_url)
index.create()
logger.info("Index created successfully")

Expand All @@ -79,8 +79,8 @@ def listall(self, args: Namespace):
Usage:
rvl index listall
"""
url = create_redis_url(args)
conn = get_redis_connection(url)
redis_url = create_redis_url(args)
conn = get_redis_connection(redis_url)
indices = convert_bytes(conn.execute_command("FT._LIST"))
logger.info("Indices:")
for i, index in enumerate(indices):
Expand All @@ -107,16 +107,16 @@ def destroy(self, args: Namespace):
def _connect_to_index(self, args: Namespace) -> SearchIndex:
# connect to redis
try:
url = create_redis_url(args)
conn = get_redis_connection(url=url)
redis_url = create_redis_url(args)
conn = get_redis_connection(url=redis_url)
except ValueError:
logger.error(
"Must set REDIS_URL environment variable or provide host and port"
)
exit(0)

if args.index:
index = SearchIndex.from_existing(name=args.index, url=url)
index = SearchIndex.from_existing(name=args.index, redis_url=redis_url)
elif args.schema:
index = SearchIndex.from_yaml(args.schema)
index.set_client(conn)
Expand Down
6 changes: 3 additions & 3 deletions redisvl/cli/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,16 @@ def stats(self, args: Namespace):
def _connect_to_index(self, args: Namespace) -> SearchIndex:
# connect to redis
try:
url = create_redis_url(args)
conn = get_redis_connection(url=url)
redis_url = create_redis_url(args)
conn = get_redis_connection(url=redis_url)
except ValueError:
logger.error(
"Must set REDIS_ADDRESS environment variable or provide host and port"
)
exit(0)

if args.index:
index = SearchIndex.from_existing(name=args.index, url=url)
index = SearchIndex.from_existing(name=args.index, redis_url=redis_url)
elif args.schema:
index = SearchIndex.from_yaml(args.schema)
index.set_client(conn)
Expand Down
34 changes: 17 additions & 17 deletions redisvl/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def from_dict(cls, schema_dict: Dict[str, Any]):
def from_existing(
cls,
name: str,
url: Optional[str] = None,
redis_url: Optional[str] = None,
key_separator: str = ":",
fields: Optional[List["Field"]] = None,
**kwargs,
Expand All @@ -201,7 +201,7 @@ def search(self, *args, **kwargs) -> Union["Result", Any]:
def query(self, query: "BaseQuery") -> List[Dict[str, Any]]:
raise NotImplementedError

def connect(self, url: str, **kwargs):
def connect(self, redis_url: str, **kwargs):
"""Connect to a Redis instance."""
raise NotImplementedError

Expand Down Expand Up @@ -273,7 +273,7 @@ def __init__(
def from_existing(
cls,
name: str,
url: Optional[str] = None,
redis_url: Optional[str] = None,
key_separator: str = ":",
fields: Optional[List["Field"]] = None,
**kwargs,
Expand All @@ -282,7 +282,7 @@ def from_existing(
Args:
name (str): Index name.
url (Optional[str], optional): Redis URL. REDIS_URL env var
redis_url (Optional[str], optional): Redis URL. REDIS_URL env var
is used if not provided. Defaults to None.
key_separator (str, optional): Separator character to combine
prefix and key value for constructing redis keys. Defaults to ":".
Expand All @@ -294,9 +294,9 @@ def from_existing(
Raises:
redis.exceptions.ResponseError: If the index does not exist.
ValueError: If the REDIS_URL env var is not set and url is not provided.
ValueError: If the REDIS_URL env var is not set and redis_url is not provided.
"""
client = get_redis_connection(url, **kwargs)
client = get_redis_connection(redis_url, **kwargs)
info = convert_bytes(client.ft(name).info())
index_definition = make_dict(info["index_definition"])
storage_type = index_definition["key_type"].lower()
Expand All @@ -311,17 +311,17 @@ def from_existing(
instance.set_client(client)
return instance

def connect(self, url: Optional[str] = None, **kwargs):
def connect(self, redis_url: Optional[str] = None, **kwargs):
"""Connect to a Redis instance.
Args:
url (str): Redis URL. REDIS_URL env var is used if not provided.
redis_url (str): Redis URL. REDIS_URL env var is used if not provided.
Raises:
redis.exceptions.ConnectionError: If the connection to Redis fails.
ValueError: If the REDIS_URL env var is not set and url is not provided.
ValueError: If the REDIS_URL env var is not set and redis_url is not provided.
"""
self._redis_conn = get_redis_connection(url, **kwargs)
self._redis_conn = get_redis_connection(redis_url, **kwargs)
return self

@check_connected("_redis_conn")
Expand Down Expand Up @@ -497,7 +497,7 @@ def __init__(
async def from_existing(
cls,
name: str,
url: Optional[str] = None,
redis_url: Optional[str] = None,
key_separator: str = ":",
fields: Optional[List["Field"]] = None,
**kwargs,
Expand All @@ -506,7 +506,7 @@ async def from_existing(
Args:
name (str): Index name.
url (Optional[str], optional): Redis URL. REDIS_URL env var
redis_url (Optional[str], optional): Redis URL. REDIS_URL env var
is used if not provided. Defaults to None.
key_separator (str, optional): Separator character to combine
prefix and key value for constructing redis keys. Defaults to ":".
Expand All @@ -518,9 +518,9 @@ async def from_existing(
Raises:
redis.exceptions.ResponseError: If the index does not exist.
ValueError: If the REDIS_URL env var is not set and url is not provided.
ValueError: If the REDIS_URL env var is not set and redis_url is not provided.
"""
client = get_async_redis_connection(url, **kwargs)
client = get_async_redis_connection(redis_url, **kwargs)
info = convert_bytes(await client.ft(name).info())
index_definition = make_dict(info["index_definition"])
storage_type = index_definition["key_type"].lower()
Expand All @@ -535,17 +535,17 @@ async def from_existing(
instance.set_client(client)
return instance

def connect(self, url: Optional[str] = None, **kwargs):
def connect(self, redis_url: Optional[str] = None, **kwargs):
"""Connect to a Redis instance.
Args:
url (str): Redis URL. REDIS_URL env var is used if not provided.
redis_url (str): Redis URL. REDIS_URL env var is used if not provided.
Raises:
redis.exceptions.ConnectionError: If the connection to Redis fails.
ValueError: If no Redis URL is provided and REDIS_URL env var is not set.
"""
self._redis_conn = get_async_redis_connection(url, **kwargs)
self._redis_conn = get_async_redis_connection(redis_url, **kwargs)
return self

@check_connected("_redis_conn")
Expand Down
2 changes: 1 addition & 1 deletion redisvl/llmcache/semantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def __init__(
index = SearchIndex(
name=index_name, prefix=prefix, fields=self._default_fields
)
index.connect(url=redis_url, **connection_args)
index.connect(redis_url=redis_url, **connection_args)
else:
raise ValueError(
"Index name and prefix must be provided if not constructing from an existing index."
Expand Down
4 changes: 3 additions & 1 deletion redisvl/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ def generate(
if isinstance(field_type, str):
field_class = field_classes.get(field_type)
if field_class:
result[field_type].append(field_class(name=key).dict(exclude_none=True))
result[field_type].append(
field_class(name=key).dict(exclude_none=True)
)

return result
2 changes: 1 addition & 1 deletion tests/unit/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_search_index_create(client, redis_url):
assert si.exists()
assert "my_index" in convert_bytes(si.client.execute_command("FT._LIST"))

s1_2 = SearchIndex.from_existing("my_index", url=redis_url)
s1_2 = SearchIndex.from_existing("my_index", redis_url=redis_url)
assert s1_2.info()["index_name"] == si.info()["index_name"]

si.create(overwrite=False)
Expand Down

0 comments on commit 5cd5f87

Please sign in to comment.