Find and rerank documents (Java)

Hybrid search and reranking are currently in public preview. Development is ongoing, and the features and functionality are subject to change. Astra DB Serverless, and the use of such, is subject to the DataStax Preview Terms.

Finds documents in a collection through a retrieval process that uses a reranker model to combine results from a vector search and a lexical search. This process is called hybrid search. For more information about hybrid search mechanics and best practices, see Find data with hybrid search.

To find documents with vector search, lexicographical matching, and filters, see Find documents (Java).

This method requires the following:

Ready to write code? See the examples for this method to get started. If you are new to the Data API, check out the quickstart.

Result

Returns a cursor (CollectionFindAndRerankCursor) for iterating over the documents returned by the reranker.

Iterating over the cursor yields RerankedResult objects, which represent the returned documents.

The fields included in the returned documents depend on the subset of fields that were requested in the projection.

Each RerankedResult object also includes a map of the scores from the retrieval process. If scores were not requested, the map is empty.

If requested, the result also includes the sort vector used for the underlying vector search. Calling .getSortVector() on the cursor reads the sort vector.

Cursors are lazy iterators, meant to be consumed with for loops or equivalent constructs. You must iterate over the cursor to fetch matching documents and their scores. If you need a list of all results, you can call the .toList() method on the cursor.

For more information about the methods available on cursors, see AbstractCursor.

Parameters

Use the findAndRerank method, which belongs to the com.datastax.astra.client.Collection class.

Method signature
CollectionFindAndRerankCursor<T, R> findAndRerank(
    Filter filter,
    CollectionFindAndRerankOptions options,
    Class<R> newRowType
);
CollectionFindAndRerankCursor<T, T> findAndRerank(
    Filter filter,
    CollectionFindAndRerankOptions options
);
CollectionFindAndRerankCursor<T,T> findAndRerank(
    CollectionFindAndRerankOptions options
);
Name Type Summary

filter

Filter

An object that defines filter criteria using the Data API filter syntax. The method only finds documents that match the filter criteria. Filters can improve performance by reducing the number of documents that the Data API processes.

You must use & to escape any . or & in field names in the filter clause. You cannot use & to escape any other characters. For more information, see Work with . and & in field names (Java).

For a list of available filter operators and more examples, see Filter operators for collections (Java).

Filters can use only indexed fields. If you apply selective indexing when you create a collection, you cannot reference non-indexed fields in a filter.

For an example, see Use filters to restrict the search.

Default: No filter, meaning any document is a possible match.

options

CollectionFindAndRerankOptions

Optional. The options for this operation. See Properties of options for more details.

Most of the options may be provided as either values in the options object, or as builder methods on the cursor itself (cursor.sort(…​)).

Properties of options
Name Type Summary

sort

Sort

Specifies queries for the underlying vector and lexical searches.

  • The $lexical query is a string of space-separated keywords or terms.

  • The $vector query is an array of floats or a DataAPIVector object that serves as a search vector. If you use this query, you must specify the rerankQuery and rerankOn parameters.

  • The $vectorize query is a string that the configured embedding provider will convert into a search vector. Only collections that have vectorize enabled can use $vectorize.

$vector and $vectorize can’t be used together.

You can also use shorthand to specify a single search string for both the $vectorize and $lexical queries.

projection()

Projection

Optional. Controls which fields are included or excluded in the returned document.

You must use & to escape any . or & in field names in the projection clause. You cannot use & to escape any other characters. For more information, see Work with . and & in field names (Java).

For more information, see Projections for collections (Java).

Default: The default projection for the collection. All fields prefixed with $ are excluded by default and will only be returned if you include them in the projection. _id is included by default and will always be returned unless you exclude it from the projection.

limit

number

Optional. Limit the total number of documents returned. Once limit is reached, or the cursor is exhausted due to lack of matching documents, nothing more is returned.

For an example, see Limit the number of documents returned.

Default: The limit set by the Data API.

hybridLimits

Integer | Map<String, Integer>

Optional. Limit the number of documents returned by the underlying vector and lexical searches.

If a single number is specified, it applies to both the vector and lexical searches.

To set different limits for the vector and lexical searches, specify an object in the form {$vector: INTEGER, $lexical: INTEGER}.

Default: The value of limit.

includeScores

boolean

Optional. Whether to include the scores from the reranking process in the response.

These scores can be inspected in the scores attribute of each RerankedResult<R> object yielded by the CollectionFindAndRerankCursor. This attribute is a free-form object such as { $vector: 0.81, $rerank: 0.12 }.

If false, the scores attribute of each RerankedResult object is an empty object.

For an example, see Include the scores in the response.

Default: False

includeSortVector

boolean

Optional. Whether to include the sort vector that was used for the underlying vector search in the response.

This can be useful if you query through the $vectorize field instead of the $vector field, since you don’t know the sort vector in advance.

The sort vector can be read by calling the .getSortVector() method on the returned cursor.

Default: False

rerankOn

string

Required if you use $vector in sort; otherwise optional.

The document field to use for the reranking step. Once the underlying vector and lexical searches complete, the reranker compares the rerankQuery text with each document’s rerankOn field.

The reserved $lexical field is often used for this parameter, but you can specify any field that stores a string.

Documents without this field or with a null or non-string value are excluded.

Default unless you use $vector in sort: "$lexical".

rerankQuery

string

Required if you use $vector in sort; otherwise optional.

Query text for the reranker step.

Once the underlying vector and lexical searches complete, the reranker compares the rerankQuery text with each document’s rerankOn field.

Default unless you use $vector in sort: the query used for the underlying vector search, which is specified by the sort parameter.

rerankService

RerankServiceOptions

Optional. Overrides the reranking service configured for the collection, even if the collection does not have a reranking service configured.

Only the NVIDIA llama-3.2-nv-rerankqa-1b-v2 reranking model reranker model is supported.

Only collections in databases in the AWS us-east-2 region support this parameter.

For an example, see Override the collection’s rerank provider.

Examples

The following examples demonstrate how to find documents with hybrid search.

Find documents with a hybrid search

  • With $vectorize

  • Without $vectorize

Use the sort parameter to specify the queries for the underlying vector search and lexical search.

The $lexical query is a string of space-separated keywords or terms.

The $vectorize query is a string that the configured embedding provider will convert into a search vector. Alternatively, you use a $vector query, as the "Without $vectorize" example demonstrates.

import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.collections.commands.cursor.CollectionFindAndRerankCursor;
import com.datastax.astra.client.collections.commands.options.CollectionFindAndRerankOptions;
import com.datastax.astra.client.collections.definition.documents.Document;
import com.datastax.astra.client.core.hybrid.Hybrid;
import com.datastax.astra.client.core.query.Sort;
import com.datastax.astra.client.core.rerank.RerankedResult;

public class Example {

  public static void main(String[] args) {
    // Get an existing collection
    Collection<Document> collection =
        new DataAPIClient("APPLICATION_TOKEN")
            .getDatabase("API_ENDPOINT")
            .getCollection("COLLECTION_NAME");

    // Find documents
    Hybrid hybrid = new Hybrid().vectorize("A tree in the woods").lexical("house hill grassy");
    CollectionFindAndRerankCursor<Document, Document> cursor =
        collection.findAndRerank(new CollectionFindAndRerankOptions().sort(Sort.hybrid(hybrid)));

    // Iterate over the results
    for (RerankedResult<Document> result : cursor) {
      System.out.println(result.getDocument());
    }
  }
}

Use the sort parameter to specify the queries for the underlying vector search and lexical search.

The $lexical query is a string of space-separated keywords or terms.

The $vector query is a DataAPIVector object or an array of floats.

You must also specify the rerankQuery and rerankOn parameters.

import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.collections.commands.cursor.CollectionFindAndRerankCursor;
import com.datastax.astra.client.collections.commands.options.CollectionFindAndRerankOptions;
import com.datastax.astra.client.collections.definition.documents.Document;
import com.datastax.astra.client.core.hybrid.Hybrid;
import com.datastax.astra.client.core.query.Sort;
import com.datastax.astra.client.core.rerank.RerankedResult;

public class Example {

  public static void main(String[] args) {
    // Get an existing collection
    Collection<Document> collection =
        new DataAPIClient("APPLICATION_TOKEN")
            .getDatabase("API_ENDPOINT")
            .getCollection("COLLECTION_NAME");

    // Find documents
    Hybrid hybrid =
        new Hybrid().vector(new float[] {0.08f, -0.62f, 0.39f}).lexical("house hill grassy");
    CollectionFindAndRerankCursor<Document, Document> cursor =
        collection.findAndRerank(
            new CollectionFindAndRerankOptions()
                .sort(Sort.hybrid(hybrid))
                .rerankOn("$lexical")
                .rerankQuery("A tree in the woods"));

    // Iterate over the results
    for (RerankedResult<Document> result : cursor) {
      System.out.println(result.getDocument());
    }
  }
}

Use shorthand to specify a single search string

If your collection has vectorize enabled, you can use shorthand to specify the same string for both the $vectorize and $lexical queries.

import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.collections.commands.cursor.CollectionFindAndRerankCursor;
import com.datastax.astra.client.collections.commands.options.CollectionFindAndRerankOptions;
import com.datastax.astra.client.collections.definition.documents.Document;
import com.datastax.astra.client.core.query.Sort;
import com.datastax.astra.client.core.rerank.RerankedResult;

public class Example {

  public static void main(String[] args) {
    // Get an existing collection
    Collection<Document> collection =
        new DataAPIClient("APPLICATION_TOKEN")
            .getDatabase("API_ENDPOINT")
            .getCollection("COLLECTION_NAME");

    // Find documents
    CollectionFindAndRerankCursor<Document, Document> cursor =
        collection.findAndRerank(
            new CollectionFindAndRerankOptions().sort(Sort.hybrid("A tree in the woods")));

    // Iterate over the results
    for (RerankedResult<Document> result : cursor) {
      System.out.println(result.getDocument());
    }
  }
}

Use a different query in the reranking step

The results of the underlying vector search and lexical search are run through a reranker model. The reranker uses a search string to rerank the documents that were returned by the underlying searches.

If you query through the $vector field, you must specify the search string for the reranker to use and the field to rerank the documents on.

If you query through the $vectorize field, the reranker will use the string that was used to perform the underlying vector search unless you specify a different string. It will also rerank documents on their $lexical field, unless you specify a different field.

Use filters to restrict the search

You can use a filter to find documents that match specific criteria. For example, you can find documents with an is_checked_out value of false and a number_of_pages value less than 300.

Only documents that match the filter will be included in the hybrid search.

For a list of available filter operators and more examples, see Filter operators for collections (Java).

Filters can use only indexed fields. If you apply selective indexing when you create a collection, you cannot reference non-indexed fields in a filter.

  • With $vectorize

  • Without $vectorize

import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.collections.commands.cursor.CollectionFindAndRerankCursor;
import com.datastax.astra.client.collections.commands.options.CollectionFindAndRerankOptions;
import com.datastax.astra.client.collections.definition.documents.Document;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.core.query.Sort;
import com.datastax.astra.client.core.rerank.RerankedResult;

public class Example {

  public static void main(String[] args) {
    // Get an existing collection
    Collection<Document> collection =
        new DataAPIClient("APPLICATION_TOKEN")
            .getDatabase("API_ENDPOINT")
            .getCollection("COLLECTION_NAME");

    // Find documents
    Filter filter =
        Filters.and(Filters.eq("is_checked_out", false), Filters.lt("number_of_pages", 300));
    CollectionFindAndRerankCursor<Document, Document> cursor =
        collection.findAndRerank(
            filter, new CollectionFindAndRerankOptions().sort(Sort.hybrid("A tree in the woods")));

    // Iterate over the results
    for (RerankedResult<Document> result : cursor) {
      System.out.println(result.getDocument());
    }
  }
}
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.collections.commands.cursor.CollectionFindAndRerankCursor;
import com.datastax.astra.client.collections.commands.options.CollectionFindAndRerankOptions;
import com.datastax.astra.client.collections.definition.documents.Document;
import com.datastax.astra.client.core.hybrid.Hybrid;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.core.query.Sort;
import com.datastax.astra.client.core.rerank.RerankedResult;

public class Example {

  public static void main(String[] args) {
    // Get an existing collection
    Collection<Document> collection =
        new DataAPIClient("APPLICATION_TOKEN")
            .getDatabase("API_ENDPOINT")
            .getCollection("COLLECTION_NAME");

    // Find documents
    Filter filter =
        Filters.and(Filters.eq("is_checked_out", false), Filters.lt("number_of_pages", 300));
    Hybrid hybrid =
        new Hybrid().vector(new float[] {0.08f, -0.62f, 0.39f}).lexical("house hill grassy");
    CollectionFindAndRerankCursor<Document, Document> cursor =
        collection.findAndRerank(
            filter,
            new CollectionFindAndRerankOptions()
                .sort(Sort.hybrid(hybrid))
                .rerankOn("$lexical")
                .rerankQuery("A tree in the woods"));

    // Iterate over the results
    for (RerankedResult<Document> result : cursor) {
      System.out.println(result.getDocument());
    }
  }
}

Limit the number of documents returned

Specify a limit to only fetch up to a certain number of documents.

  • With $vectorize

  • Without $vectorize

import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.collections.commands.cursor.CollectionFindAndRerankCursor;
import com.datastax.astra.client.collections.commands.options.CollectionFindAndRerankOptions;
import com.datastax.astra.client.collections.definition.documents.Document;
import com.datastax.astra.client.core.query.Sort;
import com.datastax.astra.client.core.rerank.RerankedResult;

public class Example {
  public static void main(String[] args) {
    // Get an existing collection
    Collection<Document> collection =
        new DataAPIClient("APPLICATION_TOKEN")
            .getDatabase("API_ENDPOINT")
            .getCollection("COLLECTION_NAME");

    // Find documents
    CollectionFindAndRerankCursor<Document, Document> cursor =
        collection.findAndRerank(
            new CollectionFindAndRerankOptions().sort(Sort.hybrid("A tree in the woods")).limit(2));

    // Iterate over the results
    for (RerankedResult<Document> result : cursor) {
      System.out.println(result.getDocument());
    }
  }
}
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.collections.commands.cursor.CollectionFindAndRerankCursor;
import com.datastax.astra.client.collections.commands.options.CollectionFindAndRerankOptions;
import com.datastax.astra.client.collections.definition.documents.Document;
import com.datastax.astra.client.core.hybrid.Hybrid;
import com.datastax.astra.client.core.query.Sort;
import com.datastax.astra.client.core.rerank.RerankedResult;

public class Example {

  public static void main(String[] args) {
    // Get an existing collection
    Collection<Document> collection =
        new DataAPIClient("APPLICATION_TOKEN")
            .getDatabase("API_ENDPOINT")
            .getCollection("COLLECTION_NAME");

    // Find documents
    Hybrid hybrid =
        new Hybrid().vector(new float[] {0.08f, -0.62f, 0.39f}).lexical("house hill grassy");
    CollectionFindAndRerankCursor<Document, Document> cursor =
        collection.findAndRerank(
            new CollectionFindAndRerankOptions()
                .sort(Sort.hybrid(hybrid))
                .limit(2)
                .rerankOn("$lexical")
                .rerankQuery("A house in the woods"));

    // Iterate over the results
    for (RerankedResult<Document> result : cursor) {
      System.out.println(result.getDocument());
    }
  }
}

Limit the number of documents returned by the underlying searches

You can customize the number of documents returned by the underlying vector and lexical searches.

You can provide a single number, which is then used for both the vector search and the lexical search. Or, you can specify a different limit for each search. Specifying different limits can help boost the importance of one type of search over the other.

By default, each underlying search uses the same limit as the overall method.

  • With $vectorize

  • Without $vectorize

import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.collections.commands.cursor.CollectionFindAndRerankCursor;
import com.datastax.astra.client.collections.commands.options.CollectionFindAndRerankOptions;
import com.datastax.astra.client.collections.definition.documents.Document;
import com.datastax.astra.client.core.query.Sort;
import com.datastax.astra.client.core.rerank.RerankedResult;
import java.util.Map;

public class Example {

  public static void main(String[] args) {
    // Get an existing collection
    Collection<Document> collection =
        new DataAPIClient("APPLICATION_TOKEN")
            .getDatabase("API_ENDPOINT")
            .getCollection("COLLECTION_NAME");

    // Find documents
    CollectionFindAndRerankCursor<Document, Document> cursor =
        collection.findAndRerank(
            new CollectionFindAndRerankOptions()
                .sort(Sort.hybrid("A tree in the woods"))
                .hybridLimits(Map.of("$vector", 8, "$lexical", 20)));

    // Iterate over the results
    for (RerankedResult<Document> result : cursor) {
      System.out.println(result.getDocument());
    }
  }
}
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.collections.commands.cursor.CollectionFindAndRerankCursor;
import com.datastax.astra.client.collections.commands.options.CollectionFindAndRerankOptions;
import com.datastax.astra.client.collections.definition.documents.Document;
import com.datastax.astra.client.core.hybrid.Hybrid;
import com.datastax.astra.client.core.query.Sort;
import com.datastax.astra.client.core.rerank.RerankedResult;
import java.util.Map;

public class Example {

  public static void main(String[] args) {

    // Get an existing collection
    Collection<Document> collection =
        new DataAPIClient("APPLICATION_TOKEN")
            .getDatabase("API_ENDPOINT")
            .getCollection("COLLECTION_NAME");

    // Find documents
    Hybrid hybrid =
        new Hybrid().vector(new float[] {0.08f, -0.62f, 0.39f}).lexical("house hill grassy");
    CollectionFindAndRerankCursor<Document, Document> cursor =
        collection.findAndRerank(
            new CollectionFindAndRerankOptions()
                .sort(Sort.hybrid(hybrid))
                .hybridLimits(Map.of("$vector", 8, "$lexical", 20))
                .rerankOn("$lexical")
                .rerankQuery("A tree in the woods"));

    // Iterate over the results
    for (RerankedResult<Document> result : cursor) {
      System.out.println(result.getDocument());
    }
  }
}

Include the scores in the response

You can request the scores to be returned alongside the documents.

The reranking retrieval process assigns scores to each document, such as vector similarity and reranker scores, and then compares those scores across all retrieved documents to determine the best overall results.

Include the sort vector in the response

You can include the sort vector in the result. This can be useful if you use $vectorize and a search string in the sort parameter, since you don’t know the sort vector in advance.

Include only specific fields in the response

To specify which fields to include or exclude in the returned documents, use a projection.

All fields prefixed with $ are excluded by default and will only be returned if you include them in the projection. _id is included by default and will always be returned unless you exclude it from the projection.

  • With $vectorize

  • Without $vectorize

import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.collections.commands.cursor.CollectionFindAndRerankCursor;
import com.datastax.astra.client.collections.commands.options.CollectionFindAndRerankOptions;
import com.datastax.astra.client.collections.definition.documents.Document;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Projection;
import com.datastax.astra.client.core.query.Sort;
import com.datastax.astra.client.core.rerank.RerankedResult;

public class Example {

  public static void main(String[] args) {
    // Get an existing collection
    Collection<Document> collection =
        new DataAPIClient("APPLICATION_TOKEN")
            .getDatabase("API_ENDPOINT")
            .getCollection("COLLECTION_NAME");

    // Find documents
    Filter filter = null;
    CollectionFindAndRerankCursor<Document, Document> cursor =
        collection.findAndRerank(
            filter,
            new CollectionFindAndRerankOptions()
                .projection(Projection.include("is_checked_out", "title"))
                .sort(Sort.hybrid("A tree in the woods")));

    // Iterate over the results
    for (RerankedResult<Document> result : cursor) {
      System.out.println(result.getDocument());
    }
  }
}
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.collections.commands.cursor.CollectionFindAndRerankCursor;
import com.datastax.astra.client.collections.commands.options.CollectionFindAndRerankOptions;
import com.datastax.astra.client.collections.definition.documents.Document;
import com.datastax.astra.client.core.hybrid.Hybrid;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Projection;
import com.datastax.astra.client.core.query.Sort;
import com.datastax.astra.client.core.rerank.RerankedResult;

public class Example {

  public static void main(String[] args) {

    // Get an existing collection
    Collection<Document> collection =
        new DataAPIClient("APPLICATION_TOKEN")
            .getDatabase("API_ENDPOINT")
            .getCollection("COLLECTION_NAME");

    // Find documents
    Hybrid hybrid =
        new Hybrid().vector(new float[] {0.08f, -0.62f, 0.39f}).lexical("house hill grassy");
    Filter filter = null;
    CollectionFindAndRerankCursor<Document, Document> cursor =
        collection.findAndRerank(
            filter,
            new CollectionFindAndRerankOptions()
                .projection(Projection.include("is_checked_out", "title"))
                .sort(Sort.hybrid(hybrid))
                .rerankOn("$lexical")
                .rerankQuery("A tree in the woods"));

    // Iterate over the results
    for (RerankedResult<Document> result : cursor) {
      System.out.println(result.getDocument());
    }
  }
}

You can also specify a projection on the cursor:

  • With $vectorize

  • Without $vectorize

import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.collections.commands.cursor.CollectionFindAndRerankCursor;
import com.datastax.astra.client.collections.commands.options.CollectionFindAndRerankOptions;
import com.datastax.astra.client.collections.definition.documents.Document;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Projection;
import com.datastax.astra.client.core.query.Sort;
import com.datastax.astra.client.core.rerank.RerankedResult;

public class Example {

  public static void main(String[] args) {
    // Get an existing collection
    Collection<Document> collection =
        new DataAPIClient("APPLICATION_TOKEN")
            .getDatabase("API_ENDPOINT")
            .getCollection("COLLECTION_NAME");

    // Find documents
    Filter filter = null;
    CollectionFindAndRerankCursor<Document, Document> cursor =
        collection.findAndRerank(
            filter, new CollectionFindAndRerankOptions().sort(Sort.hybrid("A tree in the woods")));

    cursor.project(Projection.include("title", "is_checked_out"));

    // Iterate over the results
    for (RerankedResult<Document> result : cursor) {
      System.out.println(result.getDocument());
    }
  }
}
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.collections.commands.cursor.CollectionFindAndRerankCursor;
import com.datastax.astra.client.collections.commands.options.CollectionFindAndRerankOptions;
import com.datastax.astra.client.collections.definition.documents.Document;
import com.datastax.astra.client.core.hybrid.Hybrid;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Projection;
import com.datastax.astra.client.core.query.Sort;
import com.datastax.astra.client.core.rerank.RerankedResult;

public class Example {

  public static void main(String[] args) {

    // Get an existing collection
    Collection<Document> collection =
        new DataAPIClient("APPLICATION_TOKEN")
            .getDatabase("API_ENDPOINT")
            .getCollection("COLLECTION_NAME");

    // Find documents
    Hybrid hybrid =
        new Hybrid().vector(new float[] {0.08f, -0.62f, 0.39f}).lexical("house hill grassy");
    Filter filter = null;
    CollectionFindAndRerankCursor<Document, Document> cursor =
        collection.findAndRerank(
            filter,
            new CollectionFindAndRerankOptions()
                .sort(Sort.hybrid(hybrid))
                .rerankOn("$lexical")
                .rerankQuery("A tree in the woods"));

    cursor.project(Projection.include("title", "is_checked_out"));

    // Iterate over the results
    for (RerankedResult<Document> result : cursor) {
      System.out.println(result.getDocument());
    }
  }
}

Override the collection’s rerank provider

You can override the reranking service configured for the collection, even if the collection does not have a reranking service configured.

Only the NVIDIA llama-3.2-nv-rerankqa-1b-v2 reranking model reranker model is supported.

Only collections in databases in the AWS us-east-2 region support this parameter.

import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.collections.commands.cursor.CollectionFindAndRerankCursor;
import com.datastax.astra.client.collections.commands.options.CollectionFindAndRerankOptions;
import com.datastax.astra.client.collections.definition.documents.Document;
import com.datastax.astra.client.core.query.Sort;
import com.datastax.astra.client.core.rerank.RerankServiceOptions;
import com.datastax.astra.client.core.rerank.RerankedResult;

public class Example {

  public static void main(String[] args) {
    // Get an existing collection
    Collection<Document> collection =
        new DataAPIClient("APPLICATION_TOKEN")
            .getDatabase("API_ENDPOINT")
            .getCollection("COLLECTION_NAME");

    // Find documents
    CollectionFindAndRerankCursor<Document, Document> cursor =
        collection.findAndRerank(
            new CollectionFindAndRerankOptions()
                .sort(Sort.hybrid("A tree in the woods"))
                .rerankService(
                    new RerankServiceOptions()
                        .modelName("nvidia/llama-3.2-nv-rerankqa-1b-v2")
                        .provider("nvidia")));

    // Iterate over the results
    for (RerankedResult<Document> result : cursor) {
      System.out.println(result.getDocument());
    }
  }
}

Client reference

For more information, see the client reference.

Was this helpful?

Give Feedback

How can we improve the documentation?

© Copyright IBM Corporation 2026 | Privacy policy | Terms of use Manage Privacy Choices

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: Contact IBM