content/develop/ai/redisvl/0.9.0/api/searchindex.md
| Class | Description |
|---|---|
| SearchIndex | Primary class to write, read, and search across data structures in Redis. |
| AsyncSearchIndex | Async version of the SearchIndex to write, read, and search across data structures in Redis. |
<a id="searchindex-api"></a>
class SearchIndex(schema, redis_client=None, redis_url=None, connection_kwargs=None, validate_on_load=False, **kwargs)A search index class for interacting with Redis as a vector database.
The SearchIndex is instantiated with a reference to a Redis database and an IndexSchema (YAML path or dictionary object) that describes the various settings and field configurations.
from redisvl.index import SearchIndex
# initialize the index object with schema from file
index = SearchIndex.from_yaml(
"schemas/schema.yaml",
redis_url="redis://localhost:6379",
validate_on_load=True
)
# create the index
index.create(overwrite=True, drop=False)
# data is an iterable of dictionaries
index.load(data)
# delete index and data
index.delete(drop=True)
Initialize the RedisVL search index with a schema, Redis client (or URL string with other connection args), connection_args, and other kwargs.
aggregate(*args, **kwargs)Perform an aggregation operation against the index.
Wrapper around the aggregation API that adds the index name to the query and passes along the rest of the arguments to the redis-py ft().aggregate() method.
batch_query(queries, batch_size=10)Execute a batch of queries and process results.
batch_search(queries, batch_size=10)Perform a search against the index for multiple queries.
This method takes a list of queries and optionally query params and returns a list of Result objects for each query. Results are returned in the same order as the queries.
NOTE: Cluster users may need to incorporate hash tags into their query to avoid cross-slot operations.
clear()Clear all keys in Redis associated with the index, leaving the index available and in-place for future insertions or updates.
NOTE: This method requires custom behavior for Redis Cluster because here, we can’t easily give control of the keys we’re clearing to the user so they can separate them based on hash tag.
connect(redis_url=None, **kwargs)Connect to a Redis instance using the provided redis_url, falling back to the REDIS_URL environment variable (if available).
Note: Additional keyword arguments (**kwargs) can be used to provide extra options specific to the Redis connection.
create(overwrite=False, drop=False)Create an index in Redis with the current schema and properties.
# create an index in Redis; only if one does not exist with given name
index.create()
# overwrite an index in Redis without dropping associated data
index.create(overwrite=True)
# overwrite an index in Redis; drop associated data (clean slate)
index.create(overwrite=True, drop=True)
delete(drop=True)Delete the search index while optionally dropping all keys associated with the index.
disconnect()Disconnect from the Redis database.
drop_documents(ids)Remove documents from the index by their document IDs.
This method converts document IDs to Redis keys automatically by applying the index’s key prefix and separator configuration.
NOTE: Cluster users will need to incorporate hash tags into their document IDs and only call this method with documents from a single hash tag at a time.
drop_keys(keys)Remove a specific entry or entries from the index by it’s key ID.
exists()Check if the index exists in Redis.
expire_keys(keys, ttl)Set the expiration time for a specific entry or entries in Redis.
fetch(id)Fetch an object from Redis by id.
The id is typically either a unique identifier, or derived from some domain-specific metadata combination (like a document id or chunk id).
classmethod from_dict(schema_dict, **kwargs)Create a SearchIndex from a dictionary.
from redisvl.index import SearchIndex
index = SearchIndex.from_dict({
"index": {
"name": "my-index",
"prefix": "rvl",
"storage_type": "hash",
},
"fields": [
{"name": "doc-id", "type": "tag"}
]
}, redis_url="redis://localhost:6379")
classmethod from_existing(name, redis_client=None, redis_url=None, **kwargs)Initialize from an existing search index in Redis by index name.
classmethod from_yaml(schema_path, **kwargs)Create a SearchIndex from a YAML schema file.
from redisvl.index import SearchIndex
index = SearchIndex.from_yaml("schemas/schema.yaml", redis_url="redis://localhost:6379")
info(name=None)Get information about the index.
key(id)Construct a redis key as a combination of an index key prefix (optional) and specified id.
The id is typically either a unique identifier, or derived from some domain-specific metadata combination (like a document id or chunk id).
listall()List all search indices in Redis database.
load(data, id_field=None, keys=None, ttl=None, preprocess=None, batch_size=None)Load objects to the Redis database. Returns the list of keys loaded to Redis.
RedisVL automatically handles constructing the object keys, batching, optional preprocessing steps, and setting optional expiration (TTL policies) on keys.
paginate(query, page_size=30)Execute a given query against the index and return results in paginated batches.
This method accepts a RedisVL query instance, enabling pagination of results which allows for subsequent processing over each batch with a generator.
# Iterate over paginated search results in batches of 10
for result_batch in index.paginate(query, page_size=10):
# Process each batch of results
pass
NOTEThe page_size parameter controls the number of items each result batch contains. Adjust this value based on performance considerations and the expected volume of search results.
query(query)Execute a query on the index.
This method takes a BaseQuery or AggregationQuery object directly, and handles post-processing of the search.
from redisvl.query import VectorQuery
query = VectorQuery(
vector=[0.16, -0.34, 0.98, 0.23],
vector_field_name="embedding",
num_results=3
)
results = index.query(query)
search(*args, **kwargs)Perform a search against the index.
Wrapper around the search API that adds the index name to the query and passes along the rest of the arguments to the redis-py ft().search() method.
set_client(redis_client, **kwargs)Manually set the Redis client to use with the search index.
This method configures the search index to use a specific Redis or Async Redis client. It is useful for cases where an external, custom-configured client is preferred instead of creating a new one.
property client: Redis | RedisCluster | NoneThe underlying redis-py client object.
property key_separator: strThe optional separator between a defined prefix and key value in forming a Redis key.
property name: strThe name of the Redis search index.
property prefix: strThe optional key prefix that comes before a unique key value in forming a Redis key. If multiple prefixes are configured, returns the first one.
property storage_type: StorageTypeThe underlying storage type for the search index; either hash or json.
<a id="asyncsearchindex-api"></a>
class AsyncSearchIndex(schema, *, redis_url=None, redis_client=None, connection_kwargs=None, validate_on_load=False, **kwargs)A search index class for interacting with Redis as a vector database in async-mode.
The AsyncSearchIndex is instantiated with a reference to a Redis database and an IndexSchema (YAML path or dictionary object) that describes the various settings and field configurations.
from redisvl.index import AsyncSearchIndex
# initialize the index object with schema from file
index = AsyncSearchIndex.from_yaml(
"schemas/schema.yaml",
redis_url="redis://localhost:6379",
validate_on_load=True
)
# create the index
await index.create(overwrite=True, drop=False)
# data is an iterable of dictionaries
await index.load(data)
# delete index and data
await index.delete(drop=True)
Initialize the RedisVL async search index with a schema.
async aggregate(*args, **kwargs)Perform an aggregation operation against the index.
Wrapper around the aggregation API that adds the index name to the query and passes along the rest of the arguments to the redis-py ft().aggregate() method.
async batch_query(queries, batch_size=10)Asynchronously execute a batch of queries and process results.
async batch_search(queries, batch_size=10)Asynchronously execute a batch of search queries.
This method takes a list of search queries and executes them in batches to improve performance when dealing with multiple queries.
NOTE: Cluster users may need to incorporate hash tags into their query to avoid cross-slot operations.
queries = [
"hello world",
("goodbye world", {"num_results": 5}),
]
results = await index.batch_search(queries)
async clear()Clear all keys in Redis associated with the index, leaving the index available and in-place for future insertions or updates.
NOTE: This method requires custom behavior for Redis Cluster because here, we can’t easily give control of the keys we’re clearing to the user so they can separate them based on hash tag.
connect(redis_url=None, **kwargs)[DEPRECATED] Connect to a Redis instance. Use connection parameters in __init__.
async create(overwrite=False, drop=False)Asynchronously create an index in Redis with the current schema : and properties.
# create an index in Redis; only if one does not exist with given name
await index.create()
# overwrite an index in Redis without dropping associated data
await index.create(overwrite=True)
# overwrite an index in Redis; drop associated data (clean slate)
await index.create(overwrite=True, drop=True)
async delete(drop=True)Delete the search index.
async disconnect()Disconnect from the Redis database.
async drop_documents(ids)Remove documents from the index by their document IDs.
This method converts document IDs to Redis keys automatically by applying the index’s key prefix and separator configuration.
NOTE: Cluster users will need to incorporate hash tags into their document IDs and only call this method with documents from a single hash tag at a time.
async drop_keys(keys)Remove a specific entry or entries from the index by it’s key ID.
async exists()Check if the index exists in Redis.
async expire_keys(keys, ttl)Set the expiration time for a specific entry or entries in Redis.
async fetch(id)Asynchronously etch an object from Redis by id. The id is typically either a unique identifier, or derived from some domain-specific metadata combination (like a document id or chunk id).
classmethod from_dict(schema_dict, **kwargs)Create a SearchIndex from a dictionary.
from redisvl.index import SearchIndex
index = SearchIndex.from_dict({
"index": {
"name": "my-index",
"prefix": "rvl",
"storage_type": "hash",
},
"fields": [
{"name": "doc-id", "type": "tag"}
]
}, redis_url="redis://localhost:6379")
async classmethod* from_existing(name, redis_client=None, redis_url=None, **kwargs)Initialize from an existing search index in Redis by index name.
classmethod from_yaml(schema_path, **kwargs)Create a SearchIndex from a YAML schema file.
from redisvl.index import SearchIndex
index = SearchIndex.from_yaml("schemas/schema.yaml", redis_url="redis://localhost:6379")
async info(name=None)Get information about the index.
key(id)Construct a redis key as a combination of an index key prefix (optional) and specified id.
The id is typically either a unique identifier, or derived from some domain-specific metadata combination (like a document id or chunk id).
async listall()List all search indices in Redis database.
load(data, id_field=None, keys=None, ttl=None, preprocess=None, concurrency=None, batch_size=None)Asynchronously load objects to Redis. Returns the list of keys loaded to Redis.
RedisVL automatically handles constructing the object keys, batching, optional preprocessing steps, and setting optional expiration (TTL policies) on keys.
data = [{"test": "foo"}, {"test": "bar"}]
# simple case
keys = await index.load(data)
# set 360 second ttl policy on data
keys = await index.load(data, ttl=360)
# load data with predefined keys
keys = await index.load(data, keys=["rvl:foo", "rvl:bar"])
# load data with preprocessing step
def add_field(d):
d["new_field"] = 123
return d
keys = await index.load(data, preprocess=add_field)
async paginate(query, page_size=30)Execute a given query against the index and return results in paginated batches.
This method accepts a RedisVL query instance, enabling async pagination of results which allows for subsequent processing over each batch with a generator.
# Iterate over paginated search results in batches of 10
async for result_batch in index.paginate(query, page_size=10):
# Process each batch of results
pass
NOTEThe page_size parameter controls the number of items each result batch contains. Adjust this value based on performance considerations and the expected volume of search results.
async query(query)Asynchronously execute a query on the index.
This method takes a BaseQuery or AggregationQuery object directly, runs the search, and handles post-processing of the search.
from redisvl.query import VectorQuery
query = VectorQuery(
vector=[0.16, -0.34, 0.98, 0.23],
vector_field_name="embedding",
num_results=3
)
results = await index.query(query)
async search(*args, **kwargs)Perform an async search against the index.
Wrapper around the search API that adds the index name to the query and passes along the rest of the arguments to the redis-py ft().search() method.
set_client(redis_client)[DEPRECATED] Manually set the Redis client to use with the search index. This method is deprecated; please provide connection parameters in __init__.
property client: Redis | RedisCluster | NoneThe underlying redis-py client object.
property key_separator: strThe optional separator between a defined prefix and key value in forming a Redis key.
property name: strThe name of the Redis search index.
property prefix: strThe optional key prefix that comes before a unique key value in forming a Redis key. If multiple prefixes are configured, returns the first one.
property storage_type: StorageTypeThe underlying storage type for the search index; either hash or json.