Class SearchIndex

java.lang.Object
com.redis.vl.index.SearchIndex

public final class SearchIndex extends Object
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 Details

    • SearchIndex

      public SearchIndex(RedisConnectionManager connectionManager, IndexSchema schema)
      Create a SearchIndex with connection manager and schema
      Parameters:
      connectionManager - Redis connection manager
      schema - Index schema definition
    • SearchIndex

      public SearchIndex(IndexSchema schema)
      Create a SearchIndex with schema only (no connection)
      Parameters:
      schema - Index schema definition
    • SearchIndex

      public SearchIndex(IndexSchema schema, boolean validateOnLoad)
      Create a SearchIndex with schema and validateOnLoad option
      Parameters:
      schema - Index schema definition
      validateOnLoad - Whether to validate documents on load
    • SearchIndex

      public SearchIndex(IndexSchema schema, Jedis client)
      Create a SearchIndex with schema and Jedis client
      Parameters:
      schema - Index schema definition
      client - Jedis client for Redis operations
    • SearchIndex

      public SearchIndex(IndexSchema schema, Jedis client, boolean validateOnLoad)
      Create a SearchIndex with schema, Jedis client, and validateOnLoad option
      Parameters:
      schema - Index schema definition
      client - Jedis client for Redis operations
      validateOnLoad - Whether to validate documents on load
    • SearchIndex

      public SearchIndex(IndexSchema schema, String redisUrl)
      Create a SearchIndex with schema and Redis URL
      Parameters:
      schema - Index schema definition
      redisUrl - Redis connection URL
    • SearchIndex

      public SearchIndex(IndexSchema schema, String redisUrl, boolean validateOnLoad)
      Create a SearchIndex with schema, Redis URL, and validateOnLoad option
      Parameters:
      schema - Index schema definition
      redisUrl - Redis connection URL
      validateOnLoad - Whether to validate documents on load
    • SearchIndex

      public SearchIndex(IndexSchema schema, UnifiedJedis unifiedClient)
      Create a SearchIndex with schema and UnifiedJedis client (preferred for RediSearch)
      Parameters:
      schema - Index schema definition
      unifiedClient - UnifiedJedis client for Redis operations
    • SearchIndex

      public SearchIndex(IndexSchema schema, UnifiedJedis unifiedClient, boolean validateOnLoad)
      Create a SearchIndex with schema, UnifiedJedis client, and validateOnLoad option
      Parameters:
      schema - Index schema definition
      unifiedClient - UnifiedJedis client for Redis operations
      validateOnLoad - Whether to validate documents on load
  • Method Details

    • fromYaml

      public static SearchIndex fromYaml(String yamlPath)
      Create a SearchIndex from a YAML file
      Parameters:
      yamlPath - Path to the YAML file containing schema definition
      Returns:
      SearchIndex instance
    • fromDict

      public static SearchIndex fromDict(Map<String,Object> dict)
      Create a SearchIndex from a dictionary/map
      Parameters:
      dict - Schema definition as a map
      Returns:
      SearchIndex instance
    • fromDict

      public static SearchIndex fromDict(Map<String,Object> dict, UnifiedJedis client)
      Create a SearchIndex from a dictionary/map with a UnifiedJedis client
      Parameters:
      dict - Schema definition as a map
      client - UnifiedJedis client for Redis operations
      Returns:
      SearchIndex instance
    • fromDict

      public static SearchIndex fromDict(Map<String,Object> dict, String redisUrl)
      Create a SearchIndex from a dictionary/map with a Redis URL
      Parameters:
      dict - Schema definition as a map
      redisUrl - 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 map
      client - UnifiedJedis client for Redis operations
      validateOnLoad - 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 map
      redisUrl - Redis connection URL
      validateOnLoad - Whether to validate documents on load
      Returns:
      SearchIndex instance
    • fromExisting

      public static SearchIndex fromExisting(String indexName, UnifiedJedis client)
      Create a SearchIndex from an existing index in Redis
      Parameters:
      indexName - Name of the existing index
      client - 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

      public String addDocument(String docId, Map<String,Object> document)
      Add a document to the index
      Parameters:
      docId - Document ID
      document - Document fields
      Returns:
      Document ID
    • updateDocument

      public void updateDocument(String docId, Map<String,Object> document)
      Update an existing document
      Parameters:
      docId - Document ID
      document - Updated fields
    • deleteDocument

      public boolean deleteDocument(String docId)
      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

      public Map<String,Object> getInfo()
      Get index information using FT.INFO
      Returns:
      Map of index information
    • getName

      public String getName()
      Get the index name
      Returns:
      Index name
    • getPrefix

      public String getPrefix()
      Get the key prefix for documents
      Returns:
      Key prefix
    • getKeySeparator

      public String getKeySeparator()
      Get the key separator
      Returns:
      Key separator (default is ":")
    • getStorageType

      public IndexSchema.StorageType getStorageType()
      Get the storage type
      Returns:
      Storage type (HASH or JSON)
    • key

      public String key(String id)
      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 index
      drop - 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

      public List<String> load(List<Map<String,Object>> data)
      Load data with automatic ULID key generation.
      Parameters:
      data - List of documents to load
      Returns:
      List of generated keys
    • load

      public List<String> load(List<Map<String,Object>> data, String idField)
      Load data with specified id field.
      Parameters:
      data - List of documents to load
      idField - 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 load
      idField - Field to use as document ID (null for auto-generated ULIDs)
      preprocess - Optional preprocessing function
      Returns:
      List of keys
    • fetch

      public Map<String,Object> fetch(String idOrKey)
      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

      public int dropKeys(String key)
      Drop a single key
      Parameters:
      key - Key to delete
      Returns:
      Number of keys deleted (0 or 1)
    • dropKeys

      public int dropKeys(List<String> keys)
      Drop multiple keys
      Parameters:
      keys - List of keys to delete
      Returns:
      Number of keys deleted
    • info

      public Map<String,Object> info()
      Get index information
      Returns:
      Index information as a map
    • search

      public SearchResult search(VectorQuery query)
      Search the index using a VectorQuery
      Parameters:
      query - Vector query to execute
      Returns:
      Search results
    • search

      public SearchResult search(String query)
      Search the index using a query string
      Parameters:
      query - Query string
      Returns:
      Search results
    • search

      public SearchResult search(String query, Map<String,Object> params)
      Search the index using a query string with parameters
      Parameters:
      query - Query string
      params - Query parameters
      Returns:
      Search results
    • listIndexes

      public Set<String> listIndexes()
      List all search indexes in Redis
      Returns:
      Set of index names
    • count

      public long count(CountQuery query)
      Count documents matching a query
      Parameters:
      query - The count query
      Returns:
      The number of matching documents
    • query

      public List<Map<String,Object>> query(String queryString)
      Query the index and return results as a list of maps
      Parameters:
      queryString - Query string
      Returns:
      List of document maps
    • query

      public List<Map<String,Object>> query(Object 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

      public List<SearchResult> batchSearch(List<String> queries)
      Execute multiple search queries in batch
      Parameters:
      queries - List of query strings
      Returns:
      List of search results
    • batchSearch

      public List<SearchResult> batchSearch(List<String> queries, int batchSize)
      Execute multiple search queries in batch with specified batch size
      Parameters:
      queries - List of query strings
      batchSize - Number of queries to process per batch
      Returns:
      List of search results
    • batchQuery

      public List<List<Map<String,Object>>> batchQuery(List<Filter> queries)
      Execute multiple filter queries in batch
      Parameters:
      queries - List of filter queries
      Returns:
      List of query results
    • batchQuery

      public List<List<Map<String,Object>>> batchQuery(List<Filter> queries, int batchSize)
      Execute multiple filter queries in batch with specified batch size
      Parameters:
      queries - List of filter queries
      batchSize - Number of queries to process per batch
      Returns:
      List of query results
    • expireKeys

      public void expireKeys(String key, int seconds)
      Set expiration time for a key
      Parameters:
      key - Key to expire
      seconds - Time to live in seconds
    • expireKeys

      public List<Long> expireKeys(List<String> keys, int seconds)
      Set expiration time for multiple keys
      Parameters:
      keys - List of keys to expire
      seconds - Time to live in seconds
      Returns:
      List of results (1 if successful, 0 if key doesn't exist)
    • paginate

      public Iterable<List<Map<String,Object>>> paginate(Object query, int batchSize)
      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