Find and rerank documents (C#)

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 (C#).

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 CollectionFindAndRerankCursor for iterating over the documents returned by the reranker. The object supports fluent chaining of options to modify the FindAndRerank operation.

This object implements IEnumerable and IAsyncEnumerable for iteration. 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. The sort vector can be read by calling the GetSortVector() method on the returned cursor.

Parameters

Use the FindAndRerank method, which belongs to the Collection class.

Method signature
public CollectionFindAndRerankCursor<T> FindAndRerank(
  CollectionFindAndRerankOptions<T> options = null
);
public CollectionFindAndRerankCursor<T> FindAndRerank(
  CollectionFilter<T> filter,
  CollectionFindAndRerankOptions<T> options = null
);
public CollectionFindAndRerankCursor<T, RerankedResult<TResult>> FindAndRerank<TResult>(
  CollectionFindAndRerankOptions<T> options = null
) where TResult : class;
public CollectionFindAndRerankCursor<T, RerankedResult<TResult>> FindAndRerank<TResult>(
  CollectionFilter<T> filter,
  CollectionFindAndRerankOptions<T> options = null
) where TResult : class;
Name Type Summary

filter

CollectionFilter

Optional. 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 (C#).

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

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.

commandOptions

CollectionFindAndRerankOptions

Optional. Options for this operation. For more information and examples for general options such as timeout, see Customize API interaction. For options specific to this method, see Method-specific properties of the CollectionFindAndRerankOptions class.

Method-specific properties of the CollectionFindAndRerankOptions class
Name Type Summary

Sort

FindAndRerankSortBuilder<T>

Specifies queries for the underlying vector and lexical searches.

The sort builder provides three methods to configure hybrid search:

  • Hybrid(float[] vector, string lexical) specifies separate queries:

    • vector: An array of floats that serves as a search vector for the underlying vector search.

    • lexical: A string of space-separated keywords or terms for the underlying lexical search.

      When using this method, you must also specify the RerankQuery and RerankOn options.

  • Hybrid(string vectorize, string lexical) specifies separate query strings:

    • vectorize: A string that the configured embedding provider converts into a search vector for the underlying vector search.

    • lexical: A string of space-separated keywords or terms for the underlying lexical search.

      This method requires vectorize to be enabled on the collection.

  • Hybrid(string hybrid) uses a single query string for both vector (via vectorize) and lexical searches.

    This method requires vectorize to be enabled on the collection.

Projection

IProjectionBuilder

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 (C#).

For more information, see Projections for collections (C#).

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

int

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

int

Optional. The maximum number of documents to retrieve for each of the underlying vector and lexical searches.

If you set HybridLimits, you can’t set LexicalLimit or VectorLimit.

Default: The value of Limit.

LexicalLimit

int

Optional. The maximum number of documents to retrieve for the underlying lexical search.

If you set LexicalLimit, you can’t set HybridLimits and you must set VectorLimit.

Default: The value of Limit.

VectorLimit

int

Optional. The maximum number of documents to retrieve for the underlying vector search.

If you set VectorLimit, you can’t set HybridLimits and you must set LexicalLimit.

Default: The value of Limit.

IncludeScores

bool

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 object yielded by the CollectionFindAndRerankCursor.

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

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

Default: False

IncludeSortVector

bool

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.

Service

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.

using System.Text.Json;
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Core.Query;

namespace Examples;

public class Program
{
  static async Task Main()
  {
    // Get an existing collection
    var client = new DataAPIClient();
    var database = client.GetDatabase(
      "API_ENDPOINT",
      "APPLICATION_TOKEN"
    );
    var collection = database.GetCollection("COLLECTION_NAME");

    // Find documents
    var result = collection.FindAndRerank(
      new CollectionFindAndRerankOptions<Document>
      {
        Sort = Builders<Document>.CollectionFindAndRerankSort.Hybrid(
          "A tree in the woods",
          "house hill grassy"
        ),
      }
    );

    await foreach (var document in result)
    {
      Console.WriteLine(JsonSerializer.Serialize(document.Document));
    }
  }
}

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 an array of floats.

You must also specify the RerankQuery and RerankOn options.

using System.Text.Json;
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Core.Query;

namespace Examples;

public class Program
{
  static async Task Main()
  {
    // Get an existing collection
    var client = new DataAPIClient();
    var database = client.GetDatabase(
      "API_ENDPOINT",
      "APPLICATION_TOKEN"
    );
    var collection = database.GetCollection("COLLECTION_NAME");

    // Find documents
    var result = collection.FindAndRerank(
      new CollectionFindAndRerankOptions<Document>
      {
        Sort = Builders<Document>.CollectionFindAndRerankSort.Hybrid(
          new float[] { 0.08f, -0.62f, 0.39f },
          "house hill grassy"
        ),
        RerankQuery = "A tree in the woods",
        RerankOn = "$lexical",
      }
    );

    await foreach (var document in result)
    {
      Console.WriteLine(JsonSerializer.Serialize(document.Document));
    }
  }
}

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.

using System.Text.Json;
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Core.Query;

namespace Examples;

public class Program
{
  static async Task Main()
  {
    // Get an existing collection
    var client = new DataAPIClient();
    var database = client.GetDatabase(
      "API_ENDPOINT",
      "APPLICATION_TOKEN"
    );
    var collection = database.GetCollection("COLLECTION_NAME");

    // Find documents
    var result = collection.FindAndRerank(
      new CollectionFindAndRerankOptions<Document>
      {
        Sort = Builders<Document>.CollectionFindAndRerankSort.Hybrid(
          "A tree in the woods"
        ),
      }
    );

    await foreach (var document in result)
    {
      Console.WriteLine(JsonSerializer.Serialize(document.Document));
    }
  }
}

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 (C#).

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

using System.Text.Json;
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Core.Query;

namespace Examples;

public class Program
{
  static async Task Main()
  {
    // Get an existing collection
    var client = new DataAPIClient();
    var database = client.GetDatabase(
      "API_ENDPOINT",
      "APPLICATION_TOKEN"
    );
    var collection = database.GetCollection("COLLECTION_NAME");

    // Find documents
    var filterBuilder = Builders<Document>.CollectionFilter;
    var filter = filterBuilder.And(
      filterBuilder.Eq("is_checked_out", false),
      filterBuilder.Lt("number_of_pages", 300)
    );
    var result = collection.FindAndRerank(
      filter,
      new CollectionFindAndRerankOptions<Document>
      {
        Sort = Builders<Document>.CollectionFindAndRerankSort.Hybrid(
          "A tree in the woods"
        ),
      }
    );

    await foreach (var document in result)
    {
      Console.WriteLine(JsonSerializer.Serialize(document.Document));
    }
  }
}
using System.Text.Json;
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Core.Query;

namespace Examples;

public class Program
{
  static async Task Main()
  {
    // Get an existing collection
    var client = new DataAPIClient();
    var database = client.GetDatabase(
      "API_ENDPOINT",
      "APPLICATION_TOKEN"
    );
    var collection = database.GetCollection("COLLECTION_NAME");

    // Find documents
    var filterBuilder = Builders<Document>.CollectionFilter;
    var filter = filterBuilder.And(
      filterBuilder.Eq("is_checked_out", false),
      filterBuilder.Lt("number_of_pages", 300)
    );
    var result = collection.FindAndRerank(
      filter,
      new CollectionFindAndRerankOptions<Document>
      {
        Sort = Builders<Document>.CollectionFindAndRerankSort.Hybrid(
          new float[] { 0.08f, -0.62f, 0.39f },
          "house hill grassy"
        ),
        RerankQuery = "A tree in the woods",
        RerankOn = "$lexical",
      }
    );

    await foreach (var document in result)
    {
      Console.WriteLine(JsonSerializer.Serialize(document.Document));
    }
  }
}

Limit the number of documents returned

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

  • With $vectorize

  • Without $vectorize

using System.Text.Json;
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Core.Query;

namespace Examples;

public class Program
{
  static async Task Main()
  {
    // Get an existing collection
    var client = new DataAPIClient();
    var database = client.GetDatabase(
      "API_ENDPOINT",
      "APPLICATION_TOKEN"
    );
    var collection = database.GetCollection("COLLECTION_NAME");

    // Find documents
    var result = collection.FindAndRerank(
      new CollectionFindAndRerankOptions<Document>
      {
        Sort = Builders<Document>.CollectionFindAndRerankSort.Hybrid(
          "A tree in the woods"
        ),
        Limit = 2,
      }
    );

    await foreach (var document in result)
    {
      Console.WriteLine(JsonSerializer.Serialize(document.Document));
    }
  }
}
using System.Text.Json;
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Core.Query;

namespace Examples;

public class Program
{
  static async Task Main()
  {
    // Get an existing collection
    var client = new DataAPIClient();
    var database = client.GetDatabase(
      "API_ENDPOINT",
      "APPLICATION_TOKEN"
    );
    var collection = database.GetCollection("COLLECTION_NAME");

    // Find documents
    var result = collection.FindAndRerank(
      new CollectionFindAndRerankOptions<Document>
      {
        Sort = Builders<Document>.CollectionFindAndRerankSort.Hybrid(
          new float[] { 0.08f, -0.62f, 0.39f },
          "house hill grassy"
        ),
        RerankQuery = "A tree in the woods",
        RerankOn = "$lexical",
        Limit = 2,
      }
    );

    await foreach (var document in result)
    {
      Console.WriteLine(JsonSerializer.Serialize(document.Document));
    }
  }
}

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

using System.Text.Json;
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Core.Query;

namespace Examples;

public class Program
{
  static async Task Main()
  {
    // Get an existing collection
    var client = new DataAPIClient();
    var database = client.GetDatabase(
      "API_ENDPOINT",
      "APPLICATION_TOKEN"
    );
    var collection = database.GetCollection("COLLECTION_NAME");

    // Find documents
    var result = collection.FindAndRerank(
      new CollectionFindAndRerankOptions<Document>
      {
        Sort = Builders<Document>.CollectionFindAndRerankSort.Hybrid(
          "A tree in the woods"
        ),
        VectorLimit = 8,
        LexicalLimit = 20,
      }
    );

    await foreach (var document in result)
    {
      Console.WriteLine(JsonSerializer.Serialize(document.Document));
    }
  }
}
using System.Text.Json;
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Core.Query;

namespace Examples;

public class Program
{
  static async Task Main()
  {
    // Get an existing collection
    var client = new DataAPIClient();
    var database = client.GetDatabase(
      "API_ENDPOINT",
      "APPLICATION_TOKEN"
    );
    var collection = database.GetCollection("COLLECTION_NAME");

    // Find documents
    var result = collection.FindAndRerank(
      new CollectionFindAndRerankOptions<Document>
      {
        Sort = Builders<Document>.CollectionFindAndRerankSort.Hybrid(
          new float[] { 0.08f, -0.62f, 0.39f },
          "house hill grassy"
        ),
        VectorLimit = 8,
        LexicalLimit = 20,
        RerankQuery = "A tree in the woods",
        RerankOn = "$lexical",
      }
    );

    await foreach (var document in result)
    {
      Console.WriteLine(JsonSerializer.Serialize(document.Document));
    }
  }
}

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

using System.Text.Json;
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Core.Query;

namespace Examples;

public class Program
{
  static async Task Main()
  {
    // Get an existing collection
    var client = new DataAPIClient();
    var database = client.GetDatabase(
      "API_ENDPOINT",
      "APPLICATION_TOKEN"
    );
    var collection = database.GetCollection("COLLECTION_NAME");

    // Find documents
    var result = collection.FindAndRerank(
      new CollectionFindAndRerankOptions<Document>
      {
        Sort = Builders<Document>.CollectionFindAndRerankSort.Hybrid(
          "A tree in the woods"
        ),
        Projection = Builders<Document>
          .Projection.Include("is_checked_out")
          .Include("title"),
      }
    );

    await foreach (var document in result)
    {
      Console.WriteLine(JsonSerializer.Serialize(document.Document));
    }
  }
}
using System.Text.Json;
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Core.Query;

namespace Examples;

public class Program
{
  static async Task Main()
  {
    // Get an existing collection
    var client = new DataAPIClient();
    var database = client.GetDatabase(
      "API_ENDPOINT",
      "APPLICATION_TOKEN"
    );
    var collection = database.GetCollection("COLLECTION_NAME");

    // Find documents
    var result = collection.FindAndRerank(
      new CollectionFindAndRerankOptions<Document>
      {
        Sort = Builders<Document>.CollectionFindAndRerankSort.Hybrid(
          new float[] { 0.08f, -0.62f, 0.39f },
          "house hill grassy"
        ),
        Projection = Builders<Document>
          .Projection.Include("is_checked_out")
          .Include("title"),
        RerankQuery = "A tree in the woods",
        RerankOn = "$lexical",
      }
    );

    await foreach (var document in result)
    {
      Console.WriteLine(JsonSerializer.Serialize(document.Document));
    }
  }
}

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.

using System.Text.Json;
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Core.Query;

namespace Examples;

public class Program
{
  static async Task Main()
  {
    // Get an existing collection
    var client = new DataAPIClient();
    var database = client.GetDatabase(
      "API_ENDPOINT",
      "APPLICATION_TOKEN"
    );
    var collection = database.GetCollection("COLLECTION_NAME");

    // Find documents
    var result = collection.FindAndRerank(
      new CollectionFindAndRerankOptions<Document>
      {
        Sort = Builders<Document>.CollectionFindAndRerankSort.Hybrid(
          "A tree in the woods"
        ),
        RerankQuery = "A house on a hill",
        Service = new()
        {
          ModelName = "nvidia/llama-3.2-nv-rerankqa-1b-v2",
          Provider = "nvidia",
        },
      }
    );

    await foreach (var document in result)
    {
      Console.WriteLine(JsonSerializer.Serialize(document.Document));
    }
  }
}

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