Skip to content

Commit

Permalink
enhance: Sync go.mod version & dep to main pkg (#32973)
Browse files Browse the repository at this point in the history
Related to #31293
See also #32951 #32952 #32953 #32954

---------

Signed-off-by: Congqi Xia <congqi.xia@zilliz.com>
  • Loading branch information
congqixia authored May 13, 2024
1 parent 0e5765b commit ef41f80
Show file tree
Hide file tree
Showing 7 changed files with 373 additions and 44 deletions.
1 change: 1 addition & 0 deletions .github/workflows/code-checker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ on:
- 'scripts/**'
- 'internal/**'
- 'pkg/**'
- 'client/**'
- 'cmd/**'
- 'build/**'
- 'tests/integration/**'
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/mac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ on:
- 'scripts/**'
- 'internal/**'
- 'pkg/**'
- 'client/**'
- 'cmd/**'
- 'build/**'
- 'tests/integration/**'
Expand Down
37 changes: 37 additions & 0 deletions client/example/database/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"context"
"log"

milvusclient "github.com/milvus-io/milvus/client/v2"
)

const (
milvusAddr = `localhost:19530`
nEntities, dim = 3000, 128
collectionName = "hello_milvus"

msgFmt = "==== %s ====\n"
idCol, randomCol, embeddingCol = "ID", "random", "embeddings"
topK = 3
)

func main() {
ctx := context.Background()

log.Printf(msgFmt, "start connecting to Milvus")
c, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: milvusAddr,
})
if err != nil {
log.Fatal("failed to connect to milvus, err: ", err.Error())
}
defer c.Close(ctx)

dbNames, err := c.ListDatabase(ctx, milvusclient.NewListDatabaseOption())
if err != nil {
log.Fatal("failed to list databases", err.Error())
}
log.Println("=== Databases: ", dbNames)
}
240 changes: 240 additions & 0 deletions client/example/playground/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
package main

import (
"context"
"flag"
"log"
"math/rand"
"time"

milvusclient "github.com/milvus-io/milvus/client/v2"
"github.com/milvus-io/milvus/client/v2/entity"
"github.com/milvus-io/milvus/client/v2/index"
)

var cmd = flag.String("cmd", helloMilvusCmd, "command to run")

const (
helloMilvusCmd = `hello_milvus`
partitionsCmd = `partitions`
indexCmd = `indexes`

milvusAddr = `localhost:19530`
nEntities, dim = 3000, 128
collectionName = "hello_milvus"

msgFmt = "==== %s ====\n"
idCol, randomCol, embeddingCol = "ID", "random", "embeddings"
topK = 3
)

func main() {
flag.Parse()

switch *cmd {
case helloMilvusCmd:
HelloMilvus()
case partitionsCmd:
Partitions()
case indexCmd:
Indexes()
}
}

func HelloMilvus() {
ctx := context.Background()

log.Printf(msgFmt, "start connecting to Milvus")
c, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: milvusAddr,
})
if err != nil {
log.Fatal("failed to connect to milvus, err: ", err.Error())
}
defer c.Close(ctx)

if has, err := c.HasCollection(ctx, milvusclient.NewHasCollectionOption(collectionName)); err != nil {
log.Fatal("failed to check collection exists or not", err.Error())
} else if has {
c.DropCollection(ctx, milvusclient.NewDropCollectionOption(collectionName))
}

err = c.CreateCollection(ctx, milvusclient.SimpleCreateCollectionOptions(collectionName, dim).WithVarcharPK(true, 128))
if err != nil {
log.Fatal("failed to create collection", err.Error())
}

collections, err := c.ListCollections(ctx, milvusclient.NewListCollectionOption())
if err != nil {
log.Fatal("failed to list collections,", err.Error())
}

for _, collectionName := range collections {
collection, err := c.DescribeCollection(ctx, milvusclient.NewDescribeCollectionOption(collectionName))
if err != nil {
log.Fatal(err.Error())
}
log.Println(collection.Name)
for _, field := range collection.Schema.Fields {
log.Println("=== Field: ", field.Name, field.DataType, field.AutoID)
}
}

// randomData := make([]int64, 0, nEntities)
vectorData := make([][]float32, 0, nEntities)
// generate data
for i := 0; i < nEntities; i++ {
// randomData = append(randomData, rand.Int63n(1000))
vec := make([]float32, 0, dim)
for j := 0; j < dim; j++ {
vec = append(vec, rand.Float32())
}
vectorData = append(vectorData, vec)
}

err = c.Insert(ctx, milvusclient.NewColumnBasedInsertOption(collectionName).WithFloatVectorColumn("vector", dim, vectorData))
if err != nil {
log.Fatal("failed to insert data")
}

log.Println("start flush collection")
flushTask, err := c.Flush(ctx, milvusclient.NewFlushOption(collectionName))
if err != nil {
log.Fatal("failed to flush", err.Error())
}
start := time.Now()
err = flushTask.Await(ctx)
if err != nil {
log.Fatal("failed to flush", err.Error())
}
log.Println("flush done, elasped", time.Since(start))

indexTask, err := c.CreateIndex(ctx, milvusclient.NewCreateIndexOption(collectionName, "vector", index.NewHNSWIndex(entity.L2, 16, 100)))
if err != nil {
log.Fatal("failed to create index, err: ", err.Error())
}
err = indexTask.Await(ctx)
if err != nil {
log.Fatal("failed to wait index construction complete")
}

loadTask, err := c.LoadCollection(ctx, milvusclient.NewLoadCollectionOption(collectionName))
if err != nil {
log.Fatal("failed to load collection", err.Error())
}
loadTask.Await(ctx)

vec2search := []entity.Vector{
entity.FloatVector(vectorData[len(vectorData)-2]),
entity.FloatVector(vectorData[len(vectorData)-1]),
}

resultSets, err := c.Search(ctx, milvusclient.NewSearchOption(collectionName, 3, vec2search).WithConsistencyLevel(entity.ClEventually))
if err != nil {
log.Fatal("failed to search collection", err.Error())
}
for _, resultSet := range resultSets {
for i := 0; i < resultSet.ResultCount; i++ {
log.Print(resultSet.IDs.Get(i))
}
log.Println()
}

err = c.DropCollection(ctx, milvusclient.NewDropCollectionOption(collectionName))
if err != nil {
log.Fatal("=== Failed to drop collection", err.Error())
}
}

func Partitions() {
ctx := context.Background()

log.Printf(msgFmt, "start connecting to Milvus")
c, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: milvusAddr,
})
if err != nil {
log.Fatal("failed to connect to milvus, err: ", err.Error())
}
defer c.Close(ctx)

has, err := c.HasCollection(ctx, milvusclient.NewHasCollectionOption(collectionName))
if err != nil {
log.Fatal(err)
}
if has {
c.DropCollection(ctx, milvusclient.NewDropCollectionOption(collectionName))
}

err = c.CreateCollection(ctx, milvusclient.SimpleCreateCollectionOptions(collectionName, dim))
if err != nil {
log.Fatal("failed to create collection", err.Error())
}

partitions, err := c.ListPartitions(ctx, milvusclient.NewListPartitionOption(collectionName))
if err != nil {
log.Fatal("failed to create collection", err.Error())
}

for _, partitionName := range partitions {
err := c.DropPartition(ctx, milvusclient.NewDropPartitionOption(collectionName, partitionName))
if err != nil {
log.Println(err.Error())
}
}

c.CreatePartition(ctx, milvusclient.NewCreatePartitionOption(collectionName, "new_partition"))
partitions, err = c.ListPartitions(ctx, milvusclient.NewListPartitionOption(collectionName))
if err != nil {
log.Fatal("failed to create collection", err.Error())
}
log.Println(partitions)

err = c.DropCollection(ctx, milvusclient.NewDropCollectionOption(collectionName))
if err != nil {
log.Fatal("=== Failed to drop collection", err.Error())
}
}

func Indexes() {
ctx := context.Background()

log.Printf(msgFmt, "start connecting to Milvus")
c, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: milvusAddr,
})
if err != nil {
log.Fatal("failed to connect to milvus, err: ", err.Error())
}
defer c.Close(ctx)

has, err := c.HasCollection(ctx, milvusclient.NewHasCollectionOption(collectionName))
if err != nil {
log.Fatal(err)
}
if has {
c.DropCollection(ctx, milvusclient.NewDropCollectionOption(collectionName))
}

err = c.CreateCollection(ctx, milvusclient.SimpleCreateCollectionOptions(collectionName, dim))
if err != nil {
log.Fatal("failed to create collection", err.Error())
}

index := index.NewHNSWIndex(entity.COSINE, 16, 64)

createIdxOpt := milvusclient.NewCreateIndexOption(collectionName, "vector", index)
task, err := c.CreateIndex(ctx, createIdxOpt)
if err != nil {
log.Fatal("failed to create index", err.Error())
}
task.Await(ctx)

indexes, err := c.ListIndexes(ctx, milvusclient.NewListIndexOption(collectionName))
if err != nil {
log.Fatal("failed to list indexes", err.Error())
}
for _, indexName := range indexes {
log.Println(indexName)
}
}
22 changes: 12 additions & 10 deletions client/go.mod
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
module github.com/milvus-io/milvus/client/v2

go 1.21.8
go 1.20

require (
github.com/blang/semver/v4 v4.0.0
github.com/cockroachdb/errors v1.9.1
github.com/gogo/status v1.1.0
github.com/golang/protobuf v1.5.3
github.com/golang/protobuf v1.5.4
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
github.com/milvus-io/milvus-proto/go-api/v2 v2.3.4-0.20240228061649-a922b16f2a46
github.com/milvus-io/milvus-proto/go-api/v2 v2.3.4-0.20240430035521-259ae1d10016
github.com/milvus-io/milvus/pkg v0.0.2-0.20240317152703-17b4938985f3
github.com/samber/lo v1.27.0
github.com/stretchr/testify v1.8.4
github.com/tidwall/gjson v1.17.1
go.uber.org/atomic v1.10.0
google.golang.org/grpc v1.54.0
google.golang.org/grpc v1.57.1
)

require (
Expand Down Expand Up @@ -106,15 +106,17 @@ require (
go.uber.org/automaxprocs v1.5.2 // indirect
go.uber.org/multierr v1.7.0 // indirect
go.uber.org/zap v1.20.0 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/genproto v0.0.0-20230331144136-dcfb400f0633 // indirect
google.golang.org/protobuf v1.31.0 // indirect
google.golang.org/genproto v0.0.0-20230526161137-0005af68ea54 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230525234035-dd9d682886f9 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
Expand Down
Loading

0 comments on commit ef41f80

Please sign in to comment.