Class BaseReranker

java.lang.Object
com.redis.vl.utils.rerank.BaseReranker
Direct Known Subclasses:
CohereReranker, HFCrossEncoderReranker, VoyageAIReranker

public abstract class BaseReranker extends Object
Abstract base class for document rerankers.

Rerankers improve search result quality by reordering documents based on their relevance to a query using specialized models (cross-encoders, API-based rerankers, etc.).

Implementations must provide the rank(String, List) method which takes a query and a list of documents and returns a RerankResult with the documents reordered by relevance.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final int
    Maximum number of results to return.
    protected final String
    The model name or identifier used for reranking.
    protected final List<String>
    Optional list of field names to rank by (may be null).
    protected final boolean
    Whether to include relevance scores in results.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    BaseReranker(String model, List<String> rankBy, int limit, boolean returnScore)
    Create a reranker with the specified configuration.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Get the maximum number of results to return.
    Get the model name.
    Get the fields to rank by.
    boolean
    Check if scores should be returned with results.
    abstract RerankResult
    rank(String query, List<?> docs)
    Rerank documents based on their relevance to the query.
    protected void
    validateDocs(List<?> docs)
    Validate documents parameter.
    protected void
    Validate query parameter.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • model

      protected final String model
      The model name or identifier used for reranking.
    • rankBy

      protected final List<String> rankBy
      Optional list of field names to rank by (may be null).
    • limit

      protected final int limit
      Maximum number of results to return.
    • returnScore

      protected final boolean returnScore
      Whether to include relevance scores in results.
  • Constructor Details

    • BaseReranker

      protected BaseReranker(String model, List<String> rankBy, int limit, boolean returnScore)
      Create a reranker with the specified configuration.
      Parameters:
      model - The model name or identifier
      rankBy - Optional list of field names to rank by (may be null)
      limit - Maximum number of results to return (must be positive)
      returnScore - Whether to include relevance scores in results
      Throws:
      IllegalArgumentException - if limit is not positive
  • Method Details

    • rank

      public abstract RerankResult rank(String query, List<?> docs)
      Rerank documents based on their relevance to the query.
      Parameters:
      query - The search query
      docs - The documents to rerank (either List<String> or List<Map<String, Object>>)
      Returns:
      RerankResult containing reranked documents and optional scores
      Throws:
      IllegalArgumentException - if query or docs are invalid
    • getModel

      public String getModel()
      Get the model name.
      Returns:
      Model identifier
    • getRankBy

      public List<String> getRankBy()
      Get the fields to rank by.
      Returns:
      Unmodifiable list of field names, or null if not applicable
    • getLimit

      public int getLimit()
      Get the maximum number of results to return.
      Returns:
      Result limit
    • isReturnScore

      public boolean isReturnScore()
      Check if scores should be returned with results.
      Returns:
      true if scores are included
    • validateQuery

      protected void validateQuery(String query)
      Validate query parameter.
      Parameters:
      query - The query string to validate
      Throws:
      IllegalArgumentException - if query is null or empty
    • validateDocs

      protected void validateDocs(List<?> docs)
      Validate documents parameter.
      Parameters:
      docs - The documents list to validate
      Throws:
      IllegalArgumentException - if docs is null