Skip to content

Qdrant

Qdrant is an open source vector database designed for scalable and efficient similarity search and nearest neighbor retrieval. It provides both RESTful and gRPC APIs, making it easy to integrate with various applications, including search, recommendation, AI, and machine learning systems.

Add the following dependency to your project file:

NuGet
1
dotnet add package Testcontainers.Qdrant

You can start an Qdrant container instance from any .NET application. This example uses xUnit.net's IAsyncLifetime interface to manage the lifecycle of the container. The container is started in the InitializeAsync method before the test method runs, ensuring that the environment is ready for testing. After the test completes, the container is removed in the DisposeAsync method.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
private readonly QdrantContainer _qdrantContainer = new QdrantBuilder().Build();

public Task InitializeAsync()
{
    return _qdrantContainer.StartAsync();
}

public Task DisposeAsync()
{
    return _qdrantContainer.DisposeAsync().AsTask();
}

[Fact]
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))]
public async Task HealthReturnsValidResponse()
{
    // Given
    using var client = new QdrantClient(new Uri(_qdrantContainer.GetGrpcConnectionString()));

    // When
    var response = await client.HealthAsync()
        .ConfigureAwait(true);

    // Then
    Assert.NotEmpty(response.Title);
}

The test example uses the following NuGet dependencies:

1
2
3
4
5
<PackageReference Include="Microsoft.NET.Test.Sdk"/>
<PackageReference Include="coverlet.collector"/>
<PackageReference Include="xunit.runner.visualstudio"/>
<PackageReference Include="xunit"/>
<PackageReference Include="Qdrant.Client"/>

To execute the tests, use the command dotnet test from a terminal.

Tip

For the complete source code of this example and additional information, please refer to our test projects.

Configure API key

To set and configure an API key, use the following container builder method:

1
.WithApiKey(ApiKey)

Make sure the underlying Qdrant HTTP or gRPC client adds the API key to the HTTP header or gRPC metadata:

1
httpClient.DefaultRequestHeaders.Add("api-key", ApiKey);

Configure TLS

The following example generates a self-signed certificate and configures the module to use TLS with the certificate and private key:

Note

Please ensure that both the certificate and private key are provided in PEM format.

1
.WithCertificate(Certificate, CertificateKey)

The Qdrant client is configured to validate the TLS certificate using its thumbprint:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
using var httpMessageHandler = new HttpClientHandler();
httpMessageHandler.ServerCertificateCustomValidationCallback = CertificateValidation.Thumbprint(Thumbprint);

using var httpClient = new HttpClient(httpMessageHandler);
httpClient.DefaultRequestHeaders.Host = CommonName;

var grpcChannelOptions = new GrpcChannelOptions();
grpcChannelOptions.HttpClient = httpClient;

using var grpcChannel = GrpcChannel.ForAddress(_qdrantContainer.GetGrpcConnectionString(), grpcChannelOptions);

using var grpcClient = new QdrantGrpcClient(grpcChannel);

using var client = new QdrantClient(grpcClient);

A Note To Developers

The module creates a container that listens to requests over HTTP. The official Qdrant client uses the gRPC APIs to communicate with Qdrant. .NET Core and .NET support the above example with no additional configuration. However, .NET Framework has limited supported for gRPC over HTTP/2, but it can be enabled by:

  1. Configuring the module to use TLS.
  2. Configuring server certificate validation.
  3. Reference System.Net.Http.WinHttpHandler version 6.0.1 or later, and configure WinHttpHandler as the handler for GrpcChannelOptions in the Qdrant client.

Refer to the official Qdrant .NET SDK for more information.