-
Notifications
You must be signed in to change notification settings - Fork 110
/
basic.go
98 lines (84 loc) · 3 KB
/
basic.go
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"context"
"log"
"time"
"github.com/milvus-io/milvus-sdk-go/v2/client"
"github.com/milvus-io/milvus-sdk-go/v2/entity"
)
const (
// Milvus instance proxy address, may verify in your env/settings
milvusAddr = `localhost:19530`
collectionName = `gosdk_basic_collection`
dim = 128
idCol, embeddingCol = "ID", "embeddings"
)
// basic milvus operation example
func main() {
// setup context for client creation, use 10 seconds here
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
c, err := client.NewClient(ctx, client.Config{
Address: milvusAddr,
})
if err != nil {
// handling error and exit, to make example simple here
log.Fatal("failed to connect to milvus:", err.Error())
}
// first, lets check the collection exists
collExists, err := c.HasCollection(ctx, collectionName)
if err != nil {
log.Fatal("failed to check collection exists:", err.Error())
}
if collExists {
// let's say the example collection is only for sampling the API
// drop old one in case early crash or something
_ = c.DropCollection(ctx, collectionName)
}
// define collection schema
schema := entity.NewSchema().WithName(collectionName).WithDescription("this is the basic example collection").
// currently primary key field is compulsory, and only int64 is allowed
WithField(entity.NewField().WithName(idCol).WithDataType(entity.FieldTypeInt64).WithIsPrimaryKey(true).WithIsAutoID(false)).
// also the vector field is needed
WithField(entity.NewField().WithName(embeddingCol).WithDataType(entity.FieldTypeFloatVector).WithDim(dim))
err = c.CreateCollection(ctx, schema, entity.DefaultShardNumber)
if err != nil {
log.Fatal("failed to create collection:", err.Error())
}
collections, err := c.ListCollections(ctx)
if err != nil {
log.Fatal("failed to list collections:", err.Error())
}
for _, collection := range collections {
// print all the collections, id & name
log.Printf("Collection id: %d, name: %s\n", collection.ID, collection.Name)
}
// show collection partitions
partitions, err := c.ShowPartitions(ctx, collectionName)
if err != nil {
log.Fatal("failed to show partitions:", err.Error())
}
for _, partition := range partitions {
// print partition info, the shall be a default partition for out collection
log.Printf("partition id: %d, name: %s\n", partition.ID, partition.Name)
}
partitionName := "new_partition"
// now let's try to create a partition
err = c.CreatePartition(ctx, collectionName, partitionName)
if err != nil {
log.Fatal("failed to create partition:", err.Error())
}
log.Println("After create partition")
// show collection partitions, check creation
partitions, err = c.ShowPartitions(ctx, collectionName)
if err != nil {
log.Fatal("failed to show partitions:", err.Error())
}
for _, partition := range partitions {
log.Printf("partition id: %d, name: %s\n", partition.ID, partition.Name)
}
// clean up our mess
_ = c.DropCollection(ctx, collectionName)
c.Close()
}