Share via


IVectorSearchable<TRecord>.SearchAsync<TInput> Method

Definition

Searches the vector store for records that are similar to the given value.

public System.Collections.Generic.IAsyncEnumerable<Microsoft.Extensions.VectorData.VectorSearchResult<TRecord>> SearchAsync<TInput>(TInput searchValue, int top, Microsoft.Extensions.VectorData.VectorSearchOptions<TRecord>? options = default, System.Threading.CancellationToken cancellationToken = default);
abstract member SearchAsync : 'Input * int * Microsoft.Extensions.VectorData.VectorSearchOptions<'Record> * System.Threading.CancellationToken -> System.Collections.Generic.IAsyncEnumerable<Microsoft.Extensions.VectorData.VectorSearchResult<'Record>>
Public Function SearchAsync(Of TInput) (searchValue As TInput, top As Integer, Optional options As VectorSearchOptions(Of TRecord) = Nothing, Optional cancellationToken As CancellationToken = Nothing) As IAsyncEnumerable(Of VectorSearchResult(Of TRecord))

Type Parameters

TInput

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

Parameters

searchValue
TInput

The value on which to perform the similarity search. See the remarks section for more details.

top
Int32

The maximum number of results to return.

options
VectorSearchOptions<TRecord>

The options that control the behavior of the search.

cancellationToken
CancellationToken

The CancellationToken to monitor for cancellation requests. The default is None.

Returns

The records found by the vector search, including their result scores.

Remarks

The types supported for the searchValue 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<TInput> in your record definition.
  • To work with embeddings directly, pass in a ReadOnlyMemory<T> or a .NET array of the appropriate type. Most providers support at least ReadOnlyMemory<float> and float[], but some support other types (for example, ReadOnlyMemory<Half>, BitArray). Some providers might also support their own custom types as well, for example, to represent sparse embeddings. Consult your provider's documentation for supported types.
  • If you're using IEmbeddingGenerator directly in your code, that type returns an Embedding (for example, Embedding{float}), 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).

Applies to