Package com.redis.vl.index
Class SearchIndex
java.lang.Object
com.redis.vl.index.SearchIndex
Manages Redis search index operations.
This class is final to prevent finalizer attacks, as it throws exceptions in constructors for input validation (SEI CERT OBJ11-J).
-
Constructor Summary
ConstructorsConstructorDescriptionSearchIndex
(RedisConnectionManager connectionManager, IndexSchema schema) Create a SearchIndex with connection manager and schemaSearchIndex
(IndexSchema schema) Create a SearchIndex with schema only (no connection)SearchIndex
(IndexSchema schema, boolean validateOnLoad) Create a SearchIndex with schema and validateOnLoad optionSearchIndex
(IndexSchema schema, String redisUrl) Create a SearchIndex with schema and Redis URLSearchIndex
(IndexSchema schema, String redisUrl, boolean validateOnLoad) Create a SearchIndex with schema, Redis URL, and validateOnLoad optionSearchIndex
(IndexSchema schema, Jedis client) Create a SearchIndex with schema and Jedis clientSearchIndex
(IndexSchema schema, Jedis client, boolean validateOnLoad) Create a SearchIndex with schema, Jedis client, and validateOnLoad optionSearchIndex
(IndexSchema schema, UnifiedJedis unifiedClient) Create a SearchIndex with schema and UnifiedJedis client (preferred for RediSearch)SearchIndex
(IndexSchema schema, UnifiedJedis unifiedClient, boolean validateOnLoad) Create a SearchIndex with schema, UnifiedJedis client, and validateOnLoad option -
Method Summary
Modifier and TypeMethodDescriptionaddDocument
(String docId, Map<String, Object> document) Add a document to the indexbatchQuery
(List<Filter> queries) Execute multiple filter queries in batchbatchQuery
(List<Filter> queries, int batchSize) Execute multiple filter queries in batch with specified batch sizebatchSearch
(List<String> queries) Execute multiple search queries in batchbatchSearch
(List<String> queries, int batchSize) Execute multiple search queries in batch with specified batch sizeint
clear()
Clear all documents from the index without dropping the index itselflong
count
(CountQuery query) Count documents matching a queryvoid
create()
Create the index in Redis using FT.CREATEvoid
create
(boolean overwrite) Create the index with overwrite optionvoid
create
(boolean overwrite, boolean drop) Create the index with overwrite and drop optionsvoid
delete
(boolean drop) Delete the indexboolean
deleteDocument
(String docId) Delete a document from the indexboolean
drop()
Drop the index using FT.DROPINDEXint
Drop a single keyint
Drop multiple keysboolean
Drop the index and delete all documents using FT.DROPINDEX DDboolean
exists()
Check if index exists using FT.INFOvoid
expireKeys
(String key, int seconds) Set expiration time for a keyexpireKeys
(List<String> keys, int seconds) Set expiration time for multiple keysFetch a document by ID or keystatic SearchIndex
Create a SearchIndex from a dictionary/mapstatic SearchIndex
Create a SearchIndex from a dictionary/map with a Redis URLstatic SearchIndex
Create a SearchIndex from a dictionary/map with a Redis URL and validateOnLoad optionstatic SearchIndex
fromDict
(Map<String, Object> dict, UnifiedJedis client) Create a SearchIndex from a dictionary/map with a UnifiedJedis clientstatic SearchIndex
fromDict
(Map<String, Object> dict, UnifiedJedis client, boolean validateOnLoad) Create a SearchIndex from a dictionary/map with a UnifiedJedis client and validateOnLoad optionstatic SearchIndex
fromExisting
(String indexName, UnifiedJedis client) Create a SearchIndex from an existing index in Redisstatic SearchIndex
Create a SearchIndex from a YAML filelong
Get document count in the indexgetInfo()
Get index information using FT.INFOGet the key separatorgetName()
Get the index nameGet the key prefix for documentsGet the storage typeinfo()
Get index informationGenerate a full key from an ID using the index prefix and separatorList all search indexes in RedisLoad data with automatic ULID key generation.Load data with specified id field.load
(List<Map<String, Object>> data, String idField, Function<Map<String, Object>, Map<String, Object>> preprocess) Load data with preprocessing.Execute a query and return results in paginated batches.Query the index using various query types and return results as a list of mapsQuery the index and return results as a list of mapsvoid
recreate()
Recreate the index (drop if exists and create new)search
(VectorQuery query) Search the index using a VectorQuerySearch the index using a query stringSearch the index using a query string with parametersvoid
updateDocument
(String docId, Map<String, Object> document) Update an existing document
-
Constructor Details
-
SearchIndex
Create a SearchIndex with connection manager and schema- Parameters:
connectionManager
- Redis connection managerschema
- Index schema definition
-
SearchIndex
Create a SearchIndex with schema only (no connection)- Parameters:
schema
- Index schema definition
-
SearchIndex
Create a SearchIndex with schema and validateOnLoad option- Parameters:
schema
- Index schema definitionvalidateOnLoad
- Whether to validate documents on load
-
SearchIndex
Create a SearchIndex with schema and Jedis client- Parameters:
schema
- Index schema definitionclient
- Jedis client for Redis operations
-
SearchIndex
Create a SearchIndex with schema, Jedis client, and validateOnLoad option- Parameters:
schema
- Index schema definitionclient
- Jedis client for Redis operationsvalidateOnLoad
- Whether to validate documents on load
-
SearchIndex
Create a SearchIndex with schema and Redis URL- Parameters:
schema
- Index schema definitionredisUrl
- Redis connection URL
-
SearchIndex
Create a SearchIndex with schema, Redis URL, and validateOnLoad option- Parameters:
schema
- Index schema definitionredisUrl
- Redis connection URLvalidateOnLoad
- Whether to validate documents on load
-
SearchIndex
Create a SearchIndex with schema and UnifiedJedis client (preferred for RediSearch)- Parameters:
schema
- Index schema definitionunifiedClient
- UnifiedJedis client for Redis operations
-
SearchIndex
Create a SearchIndex with schema, UnifiedJedis client, and validateOnLoad option- Parameters:
schema
- Index schema definitionunifiedClient
- UnifiedJedis client for Redis operationsvalidateOnLoad
- Whether to validate documents on load
-
-
Method Details
-
fromYaml
Create a SearchIndex from a YAML file- Parameters:
yamlPath
- Path to the YAML file containing schema definition- Returns:
- SearchIndex instance
-
fromDict
Create a SearchIndex from a dictionary/map- Parameters:
dict
- Schema definition as a map- Returns:
- SearchIndex instance
-
fromDict
Create a SearchIndex from a dictionary/map with a UnifiedJedis client- Parameters:
dict
- Schema definition as a mapclient
- UnifiedJedis client for Redis operations- Returns:
- SearchIndex instance
-
fromDict
Create a SearchIndex from a dictionary/map with a Redis URL- Parameters:
dict
- Schema definition as a mapredisUrl
- Redis connection URL- Returns:
- SearchIndex instance
-
fromDict
public static SearchIndex fromDict(Map<String, Object> dict, UnifiedJedis client, boolean validateOnLoad) Create a SearchIndex from a dictionary/map with a UnifiedJedis client and validateOnLoad option- Parameters:
dict
- Schema definition as a mapclient
- UnifiedJedis client for Redis operationsvalidateOnLoad
- Whether to validate documents on load- Returns:
- SearchIndex instance
-
fromDict
public static SearchIndex fromDict(Map<String, Object> dict, String redisUrl, boolean validateOnLoad) Create a SearchIndex from a dictionary/map with a Redis URL and validateOnLoad option- Parameters:
dict
- Schema definition as a mapredisUrl
- Redis connection URLvalidateOnLoad
- Whether to validate documents on load- Returns:
- SearchIndex instance
-
fromExisting
Create a SearchIndex from an existing index in Redis- Parameters:
indexName
- Name of the existing indexclient
- UnifiedJedis client for Redis operations- Returns:
- SearchIndex instance
-
create
public void create()Create the index in Redis using FT.CREATE- Throws:
RuntimeException
- if index creation fails
-
drop
public boolean drop()Drop the index using FT.DROPINDEX- Returns:
- true if index was dropped, false if it didn't exist
-
dropWithData
public boolean dropWithData()Drop the index and delete all documents using FT.DROPINDEX DD- Returns:
- true if index was dropped, false if it didn't exist
-
exists
public boolean exists()Check if index exists using FT.INFO- Returns:
- true if index exists, false otherwise
-
recreate
public void recreate()Recreate the index (drop if exists and create new)- Throws:
RuntimeException
- if recreation fails
-
addDocument
Add a document to the index- Parameters:
docId
- Document IDdocument
- Document fields- Returns:
- Document ID
-
updateDocument
Update an existing document- Parameters:
docId
- Document IDdocument
- Updated fields
-
deleteDocument
Delete a document from the index- Parameters:
docId
- Document ID- Returns:
- true if document was deleted, false if it didn't exist
-
getDocumentCount
public long getDocumentCount()Get document count in the index- Returns:
- Number of documents
-
getInfo
Get index information using FT.INFO- Returns:
- Map of index information
-
getName
Get the index name- Returns:
- Index name
-
getPrefix
Get the key prefix for documents- Returns:
- Key prefix
-
getKeySeparator
Get the key separator- Returns:
- Key separator (default is ":")
-
getStorageType
Get the storage type- Returns:
- Storage type (HASH or JSON)
-
key
Generate a full key from an ID using the index prefix and separator- Parameters:
id
- Document ID- Returns:
- Full key with prefix
-
create
public void create(boolean overwrite) Create the index with overwrite option- Parameters:
overwrite
- Whether to overwrite an existing index
-
create
public void create(boolean overwrite, boolean drop) Create the index with overwrite and drop options- Parameters:
overwrite
- Whether to overwrite an existing indexdrop
- Whether to drop existing data when overwriting
-
delete
public void delete(boolean drop) Delete the index- Parameters:
drop
- Whether to also drop the data
-
clear
public int clear()Clear all documents from the index without dropping the index itself- Returns:
- Number of keys deleted
-
load
Load data with automatic ULID key generation.- Parameters:
data
- List of documents to load- Returns:
- List of generated keys
-
load
Load data with specified id field.- Parameters:
data
- List of documents to loadidField
- Field to use as document ID (null for auto-generated ULIDs)- Returns:
- List of keys
-
load
public List<String> load(List<Map<String, Object>> data, String idField, Function<Map<String, Object>, Map<String, Object>> preprocess) Load data with preprocessing.- Parameters:
data
- List of documents to loadidField
- Field to use as document ID (null for auto-generated ULIDs)preprocess
- Optional preprocessing function- Returns:
- List of keys
-
fetch
Fetch a document by ID or key- Parameters:
idOrKey
- Document ID or full key- Returns:
- Document fields as a map, or null if not found
-
dropKeys
Drop a single key- Parameters:
key
- Key to delete- Returns:
- Number of keys deleted (0 or 1)
-
dropKeys
Drop multiple keys- Parameters:
keys
- List of keys to delete- Returns:
- Number of keys deleted
-
info
Get index information- Returns:
- Index information as a map
-
search
Search the index using a VectorQuery- Parameters:
query
- Vector query to execute- Returns:
- Search results
-
search
Search the index using a query string- Parameters:
query
- Query string- Returns:
- Search results
-
search
Search the index using a query string with parameters- Parameters:
query
- Query stringparams
- Query parameters- Returns:
- Search results
-
listIndexes
List all search indexes in Redis- Returns:
- Set of index names
-
count
Count documents matching a query- Parameters:
query
- The count query- Returns:
- The number of matching documents
-
query
Query the index and return results as a list of maps- Parameters:
queryString
- Query string- Returns:
- List of document maps
-
query
Query the index using various query types and return results as a list of maps- Parameters:
query
- Query object (VectorQuery, FilterQuery, TextQuery, etc.)- Returns:
- List of document maps
-
batchSearch
Execute multiple search queries in batch- Parameters:
queries
- List of query strings- Returns:
- List of search results
-
batchSearch
Execute multiple search queries in batch with specified batch size- Parameters:
queries
- List of query stringsbatchSize
- Number of queries to process per batch- Returns:
- List of search results
-
batchQuery
Execute multiple filter queries in batch- Parameters:
queries
- List of filter queries- Returns:
- List of query results
-
batchQuery
Execute multiple filter queries in batch with specified batch size- Parameters:
queries
- List of filter queriesbatchSize
- Number of queries to process per batch- Returns:
- List of query results
-
expireKeys
Set expiration time for a key- Parameters:
key
- Key to expireseconds
- Time to live in seconds
-
expireKeys
Set expiration time for multiple keys- Parameters:
keys
- List of keys to expireseconds
- Time to live in seconds- Returns:
- List of results (1 if successful, 0 if key doesn't exist)
-
paginate
Execute a query and return results in paginated batches.- Parameters:
query
- The query to execute (VectorQuery, FilterQuery, or TextQuery)batchSize
- Number of results per batch- Returns:
- Iterable of result batches
-