Data store
Redis data store with capability composition.
RedisDataStore(index_name, url=None, client=None)
Bases: BaseDataStore
Redis data store with fulltext capability support.
Attributes:
| Name | Type | Description |
|---|---|---|
index_name |
str
|
Name for the Redis index. |
client |
Redis
|
Redis client instance. |
Initialize the Redis data store.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index_name
|
str
|
Name of the Redis index to use. |
required |
url
|
str | None
|
URL for Redis connection. Defaults to None. Format: redis://[[username]:[password]]@host:port/database |
None
|
client
|
Redis | None
|
Redis client instance to use. Defaults to None. in which case the url parameter will be used to create a new Redis client. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If neither |
TypeError
|
If |
ConnectionError
|
If Redis connection fails. |
fulltext
property
Access fulltext capability if registered.
This method uses the logic of its parent class to return the fulltext capability handler. This method overrides the parent class to return the RedisFulltextCapability handler for better type hinting.
Returns:
| Name | Type | Description |
|---|---|---|
RedisFulltextCapability |
RedisFulltextCapability
|
Fulltext capability handler. |
Raises:
| Type | Description |
|---|---|
NotRegisteredException
|
If fulltext capability is not registered. |
supported_capabilities
property
Return list of currently supported capabilities.
Returns:
| Type | Description |
|---|---|
list[CapabilityType]
|
list[CapabilityType]: List of capability names that are supported. |
vector
property
Access vector capability if registered.
This method uses the logic of its parent class to return the vector capability handler. This method overrides the parent class to return the RedisVectorCapability handler for better type hinting.
Returns:
| Name | Type | Description |
|---|---|---|
RedisVectorCapability |
RedisVectorCapability
|
Vector capability handler. |
Raises:
| Type | Description |
|---|---|
NotRegisteredException
|
If vector capability is not registered. |
get_size(filters=None)
async
Get the total number of records in the datastore.
Examples:
# Async usage
count = await datastore.get_size()
# With filters (using Query Filters)
from gllm_datastore.core.filters import filter as F
count = await datastore.get_size(filters=F.eq("status", "active"))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filters
|
FilterClause | QueryFilter | None
|
Query filters to apply. |
None
|
options
|
QueryOptions | None
|
Query options. Defaults to None. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The total number of records matching the filters. |
translate_query_filter(query_filter)
Translate QueryFilter or FilterClause to Redis native filter syntax.
This method delegates to the existing RedisQueryTranslator in the redis.query_translator module and returns the result as a string. It uses the instance's index_name and client to detect field types for accurate Redis Search query syntax.
Examples:
from gllm_datastore.core.filters import filter as F
# Create datastore instance
datastore = RedisDataStore(index_name="my_index", url="redis://localhost:6379")
# Single FilterClause (field types detected from index schema)
clause = F.eq("metadata.status", "active")
result = datastore.translate_query_filter(clause)
# Returns: "@metadata_status:{active}" if status is a TAG field
# Returns: "@metadata_status:active" if status is a TEXT field
# QueryFilter with multiple clauses (AND condition)
filter_obj = F.and_(
F.eq("metadata.status", "active"),
F.gt("metadata.age", 25),
)
result = datastore.translate_query_filter(filter_obj)
# Returns: "@metadata_status:{active} @metadata_age:[(25 +inf]"
# QueryFilter with OR condition
filter_obj = F.or_(
F.eq("metadata.status", "active"),
F.eq("metadata.status", "pending"),
)
result = datastore.translate_query_filter(filter_obj)
# Returns: "@metadata_status:{active} | @metadata_status:{pending}"
# IN operator (produces parentheses syntax)
filter_obj = F.in_("metadata.category", ["tech", "science"])
result = datastore.translate_query_filter(filter_obj)
# Returns: "@metadata_category:(tech|science)"
# Empty filter returns None
result = datastore.translate_query_filter(None)
# Returns: None
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query_filter
|
FilterClause | QueryFilter
|
The filter to translate. Can be a single FilterClause or a QueryFilter with multiple clauses. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The translated filter as a Redis Search query string. |
with_encryption(encryptor, fields)
Enable encryption for specified fields.
Note
Encrypted fields (content and metadata fields specified in encryption configuration) must be serializable to strings. Non-string values will be converted to strings before encryption.
Warning
When encryption is enabled for fields, some search and filter operations may be limited or broken. Encrypted fields cannot be used in filters for update or delete operations, as the filter values are not encrypted and will not match the encrypted data stored in the index. Use non-encrypted fields (like 'id') for filtering when working with encrypted data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encryptor
|
BaseEncryptor
|
The encryptor instance to use. Must not be None. |
required |
fields
|
set[str] | list[str]
|
Set or list of field names to encrypt. Must not be empty. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
RedisDataStore |
RedisDataStore
|
Self for method chaining. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If encryptor is None or fields is empty. |
with_fulltext(index_name=None)
Configure fulltext capability and return datastore instance.
Schema will be automatically inferred from chunks when creating a new index, or auto-detected from an existing index when performing operations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index_name
|
str | None
|
The name of the Redis index. Defaults to None, in which case the default class attribute will be utilized. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
RedisDataStore |
RedisDataStore
|
RedisDataStore instance for method chaining. |
with_vector(em_invoker, index_name=None)
Configure vector capability and return datastore instance.
Schema will be automatically inferred from chunks when creating a new index, or auto-detected from an existing index when performing operations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
em_invoker
|
BaseEMInvoker
|
The embedding model to perform vectorization. |
required |
index_name
|
str | None
|
The name of the Redis index. Defaults to None, in which case the default class attribute will be utilized. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
RedisDataStore |
RedisDataStore
|
RedisDataStore instance for method chaining. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If em_invoker is not provided. |