Qdrant
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/qdrantUsage example
Creating a Qdrant container
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
WithExposedPortsSince v0.37.0WithEnvSince v0.29.0WithWaitStrategySince v0.20.0WithAdditionalWaitStrategySince v0.38.0WithWaitStrategyAndDeadlineSince v0.20.0WithAdditionalWaitStrategyAndDeadlineSince v0.38.0WithEntrypointSince v0.37.0WithEntrypointArgsSince v0.37.0WithCmdSince v0.37.0WithCmdArgsSince v0.37.0WithLabelsSince v0.37.0
Lifecycle Options
WithLifecycleHooksSince v0.38.0WithAdditionalLifecycleHooksSince v0.38.0WithStartupCommandSince v0.25.0WithAfterReadyCommandSince v0.28.0
Files & Mounts Options
WithFilesSince v0.37.0WithMountsSince v0.37.0WithTmpfsSince v0.37.0WithImageMountSince v0.37.0
Build Options
WithDockerfileSince v0.37.0
Logging Options
WithLogConsumersSince v0.28.0WithLogConsumerConfigSince v0.38.0WithLoggerSince v0.29.0
Image Options
WithAlwaysPullSince v0.38.0WithImageSubstitutorsSince v0.26.0WithImagePlatformSince v0.38.0
Networking Options
WithNetworkSince v0.27.0WithNetworkByNameSince v0.38.0WithBridgeNetworkSince v0.38.0WithNewNetworkSince v0.27.0
Advanced Options
WithHostPortAccessSince v0.31.0WithConfigModifierSince v0.20.0 · Chainable since mainWithHostConfigModifierSince v0.20.0 · Chainable since mainWithEndpointSettingsModifierSince v0.20.0 · Chainable since mainCustomizeRequestSince v0.20.0WithNameSince v0.38.0WithNoStartSince v0.38.0WithProviderSince v0.39.0
Experimental Options
WithReuseByNameSince 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.
Get REST endpoint
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.
Get Web UI endpoint
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.
Get gRPC endpoint
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:
Full Example
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
}评论
登录后参与评论
KnowForge