Skip to content

Commit 4aab8b7

Browse files
Update key prefix and access method (#66)
Adds an explicit `key` method help function that combines the index key prefix + in addition to the records identifier to create the key name.
1 parent 266de35 commit 4aab8b7

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

redisvl/index.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,24 @@ def disconnect(self):
151151
"""Disconnect from the Redis instance"""
152152
self._redis_conn = None
153153

154-
def _get_key(self, record: Dict[str, Any], key_field: Optional[str] = None) -> str:
154+
def key(self, key_value: str) -> str:
155+
"""
156+
Create a redis key as a combination of an index key prefix (optional) and specified key value.
157+
The key value is typically a unique identifier, created at random, or derived from
158+
some specified metadata.
159+
160+
Args:
161+
key_value (str): The specified unique identifier for a particular document
162+
indexed in Redis.
163+
164+
Returns:
165+
str: The full Redis key including key prefix and value as a string.
166+
"""
167+
return f"{self._prefix}:{key_value}" if self._prefix else key_value
168+
169+
def _create_key(
170+
self, record: Dict[str, Any], key_field: Optional[str] = None
171+
) -> str:
155172
"""Construct the Redis HASH top level key.
156173
157174
Args:
@@ -166,13 +183,13 @@ def _get_key(self, record: Dict[str, Any], key_field: Optional[str] = None) -> s
166183
ValueError: If the key field is not found in the record.
167184
"""
168185
if key_field is None:
169-
key = uuid4().hex
186+
key_value = uuid4().hex
170187
else:
171188
try:
172-
key = record[key_field] # type: ignore
189+
key_value = record[key_field] # type: ignore
173190
except KeyError:
174191
raise ValueError(f"Key field {key_field} not found in record {record}")
175-
return f"{self._prefix}:{key}" if self._prefix else key
192+
return self.key(key_value)
176193

177194
@check_connected("_redis_conn")
178195
def info(self) -> Dict[str, Any]:
@@ -363,7 +380,7 @@ def load(
363380
ttl = kwargs.get("ttl")
364381
with self._redis_conn.pipeline(transaction=False) as pipe: # type: ignore
365382
for record in data:
366-
key = self._get_key(record, key_field)
383+
key = self._create_key(record, key_field)
367384
pipe.hset(key, mapping=record) # type: ignore
368385
if ttl:
369386
pipe.expire(key, ttl)
@@ -516,7 +533,7 @@ async def load(
516533

517534
async def _load(record: dict):
518535
async with semaphore:
519-
key = self._get_key(record, key_field)
536+
key = self._create_key(record, key_field)
520537
await self._redis_conn.hset(key, mapping=record) # type: ignore
521538
if ttl:
522539
await self._redis_conn.expire(key, ttl) # type: ignore

tests/test_index.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,22 @@
1010

1111
def test_search_index_get_key():
1212
si = SearchIndex("my_index", fields=fields)
13-
key = si._get_key({"id": "foo"}, "id")
13+
key = si.key("foo")
1414
assert key.startswith(si._prefix)
1515
assert "foo" in key
16-
key = si._get_key({"id": "foo"})
16+
key = si._create_key({"id": "foo"})
1717
assert key.startswith(si._prefix)
1818
assert "foo" not in key
1919

2020

21+
def test_search_index_no_prefix():
22+
# specify None as the prefix...
23+
si = SearchIndex("my_index", prefix=None, fields=fields)
24+
key = si.key("foo")
25+
assert not si._prefix
26+
assert key == "foo"
27+
28+
2129
def test_search_index_client(client):
2230
si = SearchIndex("my_index", fields=fields)
2331
si.set_client(client)
@@ -29,6 +37,7 @@ def test_search_index_create(client, redis_url):
2937
si = SearchIndex("my_index", fields=fields)
3038
si.set_client(client)
3139
si.create(overwrite=True)
40+
assert si.exists()
3241
assert "my_index" in convert_bytes(si.client.execute_command("FT._LIST"))
3342

3443
s1_2 = SearchIndex.from_existing("my_index", url=redis_url)
@@ -40,7 +49,6 @@ def test_search_index_delete(client):
4049
si.set_client(client)
4150
si.create(overwrite=True)
4251
si.delete()
43-
4452
assert "my_index" not in convert_bytes(si.client.execute_command("FT._LIST"))
4553

4654

0 commit comments

Comments
 (0)