SearchAsync

This method searches the vector store for records that are similar to input data.

Declaration

// C#
public async IAsyncEnumerable<VectorSearchResult<TRecord>> SearchAsync<TInput>(TInput searchValue, int top, VectorSearchOptions<TRecord>? options = null, CancellationToken cancellationToken = default)  where TInput : notnull;

Parameters

  • TInput

    The type of the input value to perform the similarity search.

  • searchValue

    Input value for similarity search. For more information, see the Remarks section.

  • top

    The maximum number of results to return.

  • options

    The options that control search behavior.

  • cancellationToken

    The cancellation token.

Return Value

IAsyncEnumerable<VectorSearchResult<TRecord>> records found by the vector search, including their result scores.

Implements

Microsoft.Extensions.VectorData.IVectorSearchable

Exceptions

  • VectorStoreException: The command fails to execute for any reason other than the absence of a record.
  • NotSupportedException: The database is not a 23ai or later database that does not have vector data type support.

Remarks

The types supported for the searchValue parameter vary based on the provider being used and the embedding generation configured.

A string or DataContent(for images, sound...) if an appropriate IEmbeddingGenerator has been configured that accepts that type as input. For example, register an IEmbeddingGenerator that accepts string as input in your dependency injection container, and then pass in a string argument to this method; the argument will be automatically passed to the IEmbeddingGenerator to generate the embedding and perform the search.

Some databases support generating embeddings at the database side. In this case, you can pass in a string or DataContent without configuring an IEmbeddingGenerator with Microsoft.Extensions.VectorData.. The provider will simply send your argument to the database as-is for embedding generation.

Arbitrary .NET types can also be passed in as long as an appropriate IEmbeddingGenerator has been configured; for example, you can create your own IEmbeddingGenerator that accepts your own custom types as input, and uses another IEmbeddingGenerator to generate embedding from multiple properties.

For .NET types beyond string and DataContent , you must use the generic VectorStoreVectorProperty in your record definition.

To work with embeddings directly, pass in the .NET data types that the connector accepts for Vector data type.

If you are using IEmbeddingGenerator directly in your code, that type returns an Embedding (for example, Embedding), which can also be passed in directly, as long as the provider supports the specific embedding type. However, consider registering your IEmbeddingGenerator with the provider instead and pass in the input type (for example, string).