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