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:
-
A Serverless (vector) database in the AWS
us-east-2region. -
A collection with vector, lexical, and rerank enabled. For more information, see Create a collection that supports hybrid search.
Collections without rerank can use the rerank override option. For an example, see Override the collection’s rerank provider.
-
Documents with the
$lexicaland$vectorfields populated. Documents without both of these fields are excluded from hybrid search.
|
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 |
|---|---|---|
|
|
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 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. |
|
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 |
| Name | Type | Summary |
|---|---|---|
|
|
Specifies queries for the underlying vector and lexical searches. The sort builder provides three methods to configure hybrid search:
For examples, see Find documents with a hybrid search and Use shorthand to specify a single search string. |
|
|
Optional. Controls which fields are included or excluded in the returned document. You must use For more information, see Projections for collections (C#). Default: The default projection for the collection.
All fields prefixed with For an example, see Include only specific fields in the response. |
|
|
Optional.
Limit the total number of documents returned.
Once For an example, see Limit the number of documents returned. Default: The limit set by the Data API. |
|
|
Optional. The maximum number of documents to retrieve for each of the underlying vector and lexical searches. If you set Default: The value of |
|
|
Optional. The maximum number of documents to retrieve for the underlying lexical search. If you set Default: The value of |
|
|
Optional. The maximum number of documents to retrieve for the underlying vector search. If you set Default: The value of |
|
|
Optional. Whether to include the scores from the reranking process in the response. These scores can be inspected in the If false, the For an example, see Include the scores in the response. Default: False |
|
|
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 The sort vector can be read by calling the For an example, see Include the sort vector in the response. Default: False |
|
|
Required if you use The document field to use for the reranking step.
Once the underlying vector and lexical searches complete, the reranker compares the The reserved Documents without this field or with a null or non-string value are excluded. Default unless you use |
|
|
Required if you use Query text for the reranker step. Once the underlying vector and lexical searches complete, the reranker compares the For an example, see Use a different query in the reranking step. Default unless you use |
|
|
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 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.
For general information, see Find data with vector search and Find data with hybrid search.
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.