-
Notifications
You must be signed in to change notification settings - Fork 114
/
hello_milvus.go
211 lines (186 loc) · 6.29 KB
/
hello_milvus.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"context"
"fmt"
"log"
"math/rand"
"time"
"github.com/milvus-io/milvus-sdk-go/v2/client"
"github.com/milvus-io/milvus-sdk-go/v2/entity"
)
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 := client.NewClient(ctx, client.Config{
Address: milvusAddr,
})
if err != nil {
log.Fatal("failed to connect to milvus, err: ", err.Error())
}
defer c.Close()
// delete collection if exists
has, err := c.HasCollection(ctx, collectionName)
if err != nil {
log.Fatalf("failed to check collection exists, err: %v", err)
}
if has {
c.DropCollection(ctx, collectionName)
}
// create collection
log.Printf(msgFmt, fmt.Sprintf("create collection, `%s`", collectionName))
schema := entity.NewSchema().WithName(collectionName).WithDescription("hello_milvus is the simplest demo to introduce the APIs").
WithField(entity.NewField().WithName(idCol).WithDataType(entity.FieldTypeInt64).WithIsPrimaryKey(true).WithIsAutoID(false)).
WithField(entity.NewField().WithName(randomCol).WithDataType(entity.FieldTypeDouble)).
WithField(entity.NewField().WithName(embeddingCol).WithDataType(entity.FieldTypeFloatVector).WithDim(dim))
if err := c.CreateCollection(ctx, schema, entity.DefaultShardNumber); err != nil { // use default shard number
log.Fatalf("create collection failed, err: %v", err)
}
// insert data
log.Printf(msgFmt, "start inserting random entities")
idList, randomList := make([]int64, 0, nEntities), make([]float64, 0, nEntities)
embeddingList := make([][]float32, 0, nEntities)
rand.Seed(time.Now().UnixNano())
// generate data
for i := 0; i < nEntities; i++ {
idList = append(idList, int64(i))
}
for i := 0; i < nEntities; i++ {
randomList = append(randomList, rand.Float64())
}
for i := 0; i < nEntities; i++ {
vec := make([]float32, 0, dim)
for j := 0; j < dim; j++ {
vec = append(vec, rand.Float32())
}
embeddingList = append(embeddingList, vec)
}
idColData := entity.NewColumnInt64(idCol, idList)
randomColData := entity.NewColumnDouble(randomCol, randomList)
embeddingColData := entity.NewColumnFloatVector(embeddingCol, dim, embeddingList)
if _, err := c.Insert(ctx, collectionName, "", idColData, randomColData, embeddingColData); err != nil {
log.Fatalf("failed to insert random data into `hello_milvus, err: %v", err)
}
if err := c.Flush(ctx, collectionName, false); err != nil {
log.Fatalf("failed to flush data, err: %v", err)
}
// build index
log.Printf(msgFmt, "start creating index IVF_FLAT")
idx, err := entity.NewIndexIvfFlat(entity.L2, 128)
if err != nil {
log.Fatalf("failed to create ivf flat index, err: %v", err)
}
if err := c.CreateIndex(ctx, collectionName, embeddingCol, idx, false); err != nil {
log.Fatalf("failed to create index, err: %v", err)
}
log.Printf(msgFmt, "start loading collection")
err = c.LoadCollection(ctx, collectionName, false)
if err != nil {
log.Fatalf("failed to load collection, err: %v", err)
}
log.Printf(msgFmt, "start searcching based on vector similarity")
vec2search := []entity.Vector{
entity.FloatVector(embeddingList[len(embeddingList)-2]),
entity.FloatVector(embeddingList[len(embeddingList)-1]),
}
begin := time.Now()
sp, _ := entity.NewIndexIvfFlatSearchParam(16)
sRet, err := c.Search(ctx, collectionName, nil, "", []string{randomCol}, vec2search,
embeddingCol, entity.L2, topK, sp)
end := time.Now()
if err != nil {
log.Fatalf("failed to search collection, err: %v", err)
}
log.Println("results:")
for _, res := range sRet {
printResult(&res)
}
log.Printf("\tsearch latency: %dms\n", end.Sub(begin)/time.Millisecond)
// hybrid search
log.Printf(msgFmt, "start hybrid searching with `random > 0.5`")
begin = time.Now()
sRet2, err := c.Search(ctx, collectionName, nil, "random > 0.5",
[]string{randomCol}, vec2search, embeddingCol, entity.L2, topK, sp)
end = time.Now()
if err != nil {
log.Fatalf("failed to search collection, err: %v", err)
}
log.Println("results:")
for _, res := range sRet2 {
printResult(&res)
}
log.Printf("\tsearch latency: %dms\n", end.Sub(begin)/time.Millisecond)
// delete data
log.Printf(msgFmt, "start deleting with expr ``")
pks := entity.NewColumnInt64(idCol, []int64{0, 1})
sRet3, err := c.QueryByPks(ctx, collectionName, nil, pks, []string{randomCol})
if err != nil {
log.Fatalf("failed to query result, err: %v", err)
}
log.Println("results:")
idlist := make([]int64, 0)
randList := make([]float64, 0)
for _, col := range sRet3 {
if col.Name() == idCol {
idColumn := col.(*entity.ColumnInt64)
for i := 0; i < col.Len(); i++ {
val, err := idColumn.ValueByIdx(i)
if err != nil {
log.Fatal(err)
}
idlist = append(idlist, val)
}
} else {
randColumn := col.(*entity.ColumnDouble)
for i := 0; i < col.Len(); i++ {
val, err := randColumn.ValueByIdx(i)
if err != nil {
log.Fatal(err)
}
randList = append(randList, val)
}
}
}
log.Printf("\tids: %#v, randoms: %#v\n", idlist, randList)
if err := c.DeleteByPks(ctx, collectionName, "", pks); err != nil {
log.Fatalf("failed to delete by pks, err: %v", err)
}
_, err = c.QueryByPks(ctx, collectionName, nil, pks, []string{randomCol}, client.WithSearchQueryConsistencyLevel(entity.ClStrong))
if err != nil {
log.Printf("failed to query result, err: %v", err)
}
// drop collection
log.Printf(msgFmt, "drop collection `hello_milvus`")
if err := c.DropCollection(ctx, collectionName); err != nil {
log.Fatalf("failed to drop collection, err: %v", err)
}
}
func printResult(sRet *client.SearchResult) {
randoms := make([]float64, 0, sRet.ResultCount)
scores := make([]float32, 0, sRet.ResultCount)
var randCol *entity.ColumnDouble
for _, field := range sRet.Fields {
if field.Name() == randomCol {
c, ok := field.(*entity.ColumnDouble)
if ok {
randCol = c
}
}
}
for i := 0; i < sRet.ResultCount; i++ {
val, err := randCol.ValueByIdx(i)
if err != nil {
log.Fatal(err)
}
randoms = append(randoms, val)
scores = append(scores, sRet.Scores[i])
}
log.Printf("\trandoms: %v, scores: %v\n", randoms, scores)
}