Qdrant¶
Since v0.29.0
Introduction¶
The Testcontainers module for Qdrant.
Adding this module to your project dependencies¶
Please run the following command to add the Qdrant module to your Go dependencies:
go get github.com/testcontainers/testcontainers-go/modules/qdrant
Usage example¶
ctx := context.Background()
qdrantContainer, err := qdrant.Run(ctx, "qdrant/qdrant:v1.7.4")
defer func() {
if err := testcontainers.TerminateContainer(qdrantContainer); err != nil {
log.Printf("failed to terminate container: %s", err)
}
}()
if err != nil {
log.Printf("failed to start container: %s", err)
return
}
Module Reference¶
Run function¶
- Since v0.32.0
Info
The RunContainer(ctx, opts...)
function is deprecated and will be removed in the next major release of Testcontainers for Go.
The Qdrant module exposes one entrypoint function to create the Qdrant container, and this function receives three parameters:
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*QdrantContainer, error)
context.Context
, the Go context.string
, the Docker image to use.testcontainers.ContainerCustomizer
, a variadic argument for passing options.
Image¶
Use the second argument in the Run
function to set a valid Docker image.
In example: Run(context.Background(), "qdrant/qdrant:v1.7.4")
.
Container Options¶
When starting the Qdrant container, you can pass options in a variadic way to configure it.
The following options are exposed by the testcontainers
package.
Basic Options¶
WithExposedPorts
Since v0.37.0WithEnv
Since v0.29.0WithWaitStrategy
Since v0.20.0WithAdditionalWaitStrategy
Not available until the next release mainWithWaitStrategyAndDeadline
Since v0.20.0WithAdditionalWaitStrategyAndDeadline
Not available until the next release mainWithEntrypoint
Since v0.37.0WithEntrypointArgs
Since v0.37.0WithCmd
Since v0.37.0WithCmdArgs
Since v0.37.0WithLabels
Since v0.37.0
Lifecycle Options¶
WithLifecycleHooks
Not available until the next release mainWithAdditionalLifecycleHooks
Not available until the next release mainWithStartupCommand
Since v0.25.0WithAfterReadyCommand
Since v0.28.0
Files & Mounts Options¶
WithFiles
Since v0.37.0WithMounts
Since v0.37.0WithTmpfs
Since v0.37.0WithImageMount
Since v0.37.0
Build Options¶
WithDockerfile
Since v0.37.0
Logging Options¶
WithLogConsumers
Since v0.28.0WithLogConsumerConfig
Not available until the next release mainWithLogger
Since v0.29.0
Image Options¶
WithAlwaysPull
Not available until the next release mainWithImageSubstitutors
Since v0.26.0WithImagePlatform
Not available until the next release main
Networking Options¶
WithNetwork
Since v0.27.0WithNetworkByName
Not available until the next release mainWithBridgeNetwork
Not available until the next release mainWithNewNetwork
Since v0.27.0
Advanced Options¶
WithHostPortAccess
Since v0.31.0WithConfigModifier
Since v0.20.0WithHostConfigModifier
Since v0.20.0WithEndpointSettingsModifier
Since v0.20.0CustomizeRequest
Since v0.20.0WithName
Not available until the next release mainWithNoStart
Not available until the next release main
Experimental Options¶
WithReuseByName
Since v0.37.0
Container Methods¶
The Qdrant container exposes the following methods:
REST Endpoint¶
- Since v0.29.0
This method returns the REST endpoint of the Qdrant container, using the default 6333
port.
restEndpoint, err := ctr.RESTEndpoint(ctx)
Web UI Endpoint¶
- Since v0.29.0
This method returns the Web UI endpoint of the Qdrant container (/dashboard
), using the default 6333
port.
webUI, err := ctr.WebUI(ctx)
gRPC Endpoint¶
- Since v0.29.0
This method returns the gRPC endpoint of the Qdrant container, using the default 6334
port.
grpcEndpoint, err := ctr.GRPCEndpoint(ctx)
Full Example¶
Here you can find a full example on how to use the qdrant-go module to perform operations with Qdrant, as seen in the examples provided by the module itself:
qdrantContainer, err := qdrant.Run(context.Background(), "qdrant/qdrant:v1.7.4")
defer func() {
if err := testcontainers.TerminateContainer(qdrantContainer); err != nil {
log.Printf("failed to terminate container: %s", err)
}
}()
if err != nil {
log.Printf("failed to start container: %s", err)
return
}
grpcEndpoint, err := qdrantContainer.GRPCEndpoint(context.Background())
if err != nil {
log.Printf("failed to get gRPC endpoint: %s", err)
return
}
// Set up a connection to the server.
conn, err := grpc.NewClient(grpcEndpoint, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Printf("did not connect: %v", err)
return
}
defer conn.Close()
collectionsClient := pb.NewCollectionsClient(conn)
const (
collectionName = "test_collection"
vectorSize uint64 = 4
distance = pb.Distance_Dot
)
// 1. create the collection
var defaultSegmentNumber uint64 = 2
_, err = collectionsClient.Create(context.Background(), &pb.CreateCollection{
CollectionName: collectionName,
VectorsConfig: &pb.VectorsConfig{Config: &pb.VectorsConfig_Params{
Params: &pb.VectorParams{
Size: vectorSize,
Distance: distance,
},
}},
OptimizersConfig: &pb.OptimizersConfigDiff{
DefaultSegmentNumber: &defaultSegmentNumber,
},
})
if err != nil {
log.Printf("Could not create collection: %v", err)
return
}
// 2. Contact the server and print out its response.
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := collectionsClient.List(ctx, &pb.ListCollectionsRequest{})
if err != nil {
log.Printf("could not get collections: %v", err)
return
}
fmt.Printf("List of collections: %s\n", r.GetCollections())
// 3. Create points grpc client
pointsClient := pb.NewPointsClient(conn)
// 4. Create keyword field index
fieldIndex1Type := pb.FieldType_FieldTypeKeyword
fieldIndex1Name := "city"
_, err = pointsClient.CreateFieldIndex(context.Background(), &pb.CreateFieldIndexCollection{
CollectionName: collectionName,
FieldName: fieldIndex1Name,
FieldType: &fieldIndex1Type,
})
if err != nil {
log.Printf("Could not create field index: %v", err)
return
}
// 5. Create integer field index
fieldIndex2Type := pb.FieldType_FieldTypeInteger
fieldIndex2Name := "count"
_, err = pointsClient.CreateFieldIndex(context.Background(), &pb.CreateFieldIndexCollection{
CollectionName: collectionName,
FieldName: fieldIndex2Name,
FieldType: &fieldIndex2Type,
})
if err != nil {
log.Printf("Could not create field index: %v", err)
return
}
// 6. Upsert points
waitUpsert := true
upsertPoints := []*pb.PointStruct{
{
// Point Id is number or UUID
Id: &pb.PointId{
PointIdOptions: &pb.PointId_Num{Num: 1},
},
Vectors: &pb.Vectors{VectorsOptions: &pb.Vectors_Vector{Vector: &pb.Vector{Data: []float32{0.05, 0.61, 0.76, 0.74}}}},
Payload: map[string]*pb.Value{
"city": {
Kind: &pb.Value_StringValue{StringValue: "Berlin"},
},
"country": {
Kind: &pb.Value_StringValue{StringValue: "Germany"},
},
"count": {
Kind: &pb.Value_IntegerValue{IntegerValue: 1000000},
},
"square": {
Kind: &pb.Value_DoubleValue{DoubleValue: 12.5},
},
},
},
{
Id: &pb.PointId{
PointIdOptions: &pb.PointId_Num{Num: 2},
},
Vectors: &pb.Vectors{VectorsOptions: &pb.Vectors_Vector{Vector: &pb.Vector{Data: []float32{0.19, 0.81, 0.75, 0.11}}}},
Payload: map[string]*pb.Value{
"city": {
Kind: &pb.Value_ListValue{
ListValue: &pb.ListValue{
Values: []*pb.Value{
{
Kind: &pb.Value_StringValue{StringValue: "Berlin"},
},
{
Kind: &pb.Value_StringValue{StringValue: "London"},
},
},
},
},
},
},
},
{
Id: &pb.PointId{
PointIdOptions: &pb.PointId_Num{Num: 3},
},
Vectors: &pb.Vectors{VectorsOptions: &pb.Vectors_Vector{Vector: &pb.Vector{Data: []float32{0.36, 0.55, 0.47, 0.94}}}},
Payload: map[string]*pb.Value{
"city": {
Kind: &pb.Value_ListValue{
ListValue: &pb.ListValue{
Values: []*pb.Value{
{
Kind: &pb.Value_StringValue{StringValue: "Berlin"},
},
{
Kind: &pb.Value_StringValue{StringValue: "Moscow"},
},
},
},
},
},
},
},
{
Id: &pb.PointId{
PointIdOptions: &pb.PointId_Num{Num: 4},
},
Vectors: &pb.Vectors{VectorsOptions: &pb.Vectors_Vector{Vector: &pb.Vector{Data: []float32{0.18, 0.01, 0.85, 0.80}}}},
Payload: map[string]*pb.Value{
"city": {
Kind: &pb.Value_ListValue{
ListValue: &pb.ListValue{
Values: []*pb.Value{
{
Kind: &pb.Value_StringValue{StringValue: "London"},
},
{
Kind: &pb.Value_StringValue{StringValue: "Moscow"},
},
},
},
},
},
},
},
{
Id: &pb.PointId{
PointIdOptions: &pb.PointId_Num{Num: 5},
},
Vectors: &pb.Vectors{VectorsOptions: &pb.Vectors_Vector{Vector: &pb.Vector{Data: []float32{0.24, 0.18, 0.22, 0.44}}}},
Payload: map[string]*pb.Value{
"count": {
Kind: &pb.Value_ListValue{
ListValue: &pb.ListValue{
Values: []*pb.Value{
{
Kind: &pb.Value_IntegerValue{IntegerValue: 0},
},
},
},
},
},
},
},
{
Id: &pb.PointId{
PointIdOptions: &pb.PointId_Num{Num: 6},
},
Vectors: &pb.Vectors{VectorsOptions: &pb.Vectors_Vector{Vector: &pb.Vector{Data: []float32{0.35, 0.08, 0.11, 0.44}}}},
Payload: map[string]*pb.Value{},
},
{
Id: &pb.PointId{
PointIdOptions: &pb.PointId_Uuid{Uuid: "58384991-3295-4e21-b711-fd3b94fa73e3"},
},
Vectors: &pb.Vectors{VectorsOptions: &pb.Vectors_Vector{Vector: &pb.Vector{Data: []float32{0.35, 0.08, 0.11, 0.44}}}},
Payload: map[string]*pb.Value{},
},
}
_, err = pointsClient.Upsert(context.Background(), &pb.UpsertPoints{
CollectionName: collectionName,
Wait: &waitUpsert,
Points: upsertPoints,
})
if err != nil {
log.Printf("Could not upsert points: %v", err)
return
}
// 7. Retrieve points by ids
pointsByID, err := pointsClient.Get(context.Background(), &pb.GetPoints{
CollectionName: collectionName,
Ids: []*pb.PointId{
{PointIdOptions: &pb.PointId_Num{Num: 1}},
{PointIdOptions: &pb.PointId_Num{Num: 2}},
},
})
if err != nil {
log.Printf("Could not retrieve points: %v", err)
return
}
fmt.Printf("Retrieved points: %d\n", len(pointsByID.GetResult()))
// 8. Unfiltered search
unfilteredSearchResult, err := pointsClient.Search(context.Background(), &pb.SearchPoints{
CollectionName: collectionName,
Vector: []float32{0.2, 0.1, 0.9, 0.7},
Limit: 3,
// Include all payload and vectors in the search result
WithVectors: &pb.WithVectorsSelector{SelectorOptions: &pb.WithVectorsSelector_Enable{Enable: true}},
WithPayload: &pb.WithPayloadSelector{SelectorOptions: &pb.WithPayloadSelector_Enable{Enable: true}},
})
if err != nil {
log.Printf("Could not search points: %v", err)
return
}
fmt.Printf("Found points: %d\n", len(unfilteredSearchResult.GetResult()))
// 9. filtered search
filteredSearchResult, err := pointsClient.Search(ctx, &pb.SearchPoints{
CollectionName: collectionName,
Vector: []float32{0.2, 0.1, 0.9, 0.7},
Limit: 3,
Filter: &pb.Filter{
Should: []*pb.Condition{
{
ConditionOneOf: &pb.Condition_Field{
Field: &pb.FieldCondition{
Key: "city",
Match: &pb.Match{
MatchValue: &pb.Match_Keyword{
Keyword: "London",
},
},
},
},
},
},
},
})
if err != nil {
log.Printf("Could not search points: %v", err)
return
}