/
MilvusClientExample.go
314 lines (281 loc) · 8.78 KB
/
MilvusClientExample.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package main
import (
"context"
"log"
"strconv"
"time"
"github.com/milvus-io/milvus-sdk-go/milvus"
)
var collectionName string = "test_go2"
var dimension int64 = 128
var indexFileSize int64 = 1024
var metricType int64 = int64(milvus.L2)
var nq int64 = 100
var nprobe int64 = 64
var nb int64 = 100000
var topk int64 = 100
var nlist int64 = 16384
func example(address string, port string) {
var i, j int64
connectParam := milvus.ConnectParam{IPAddress: address, Port: port}
ctx := context.TODO()
client, err := milvus.NewMilvusClient(ctx, connectParam)
if err != nil {
log.Fatalf("Client connect failed: %v", err)
}
//Client version
println("Client version: " + client.GetClientVersion(ctx))
if client.IsConnected(ctx) == false {
println("client: not connected: ")
return
}
println("Server status: connected")
//Get server version
var version string
var status milvus.Status
version, status, err = client.ServerVersion(ctx)
if err != nil {
println("Cmd rpc failed: " + err.Error())
}
if !status.Ok() {
println("Get server version failed: " + status.GetMessage())
return
}
println("Server version: " + version)
//test create collection
collectionParam := milvus.CollectionParam{collectionName, dimension, indexFileSize, metricType}
var hasCollection bool
//hasCollection, status, err = client.HasCollection(collectionName)
if err != nil {
println("HasCollection rpc failed: " + err.Error())
}
if hasCollection == false {
status, err = client.CreateCollection(ctx, collectionParam)
if err != nil {
println("CreateCollection rpc failed: " + err.Error())
return
}
if !status.Ok() {
println("Create collection failed: " + status.GetMessage())
return
}
println("Create collection " + collectionName + " success")
}
hasCollection, status, err = client.HasCollection(ctx, collectionName)
if err != nil {
println("HasCollection rpc failed: " + err.Error())
return
}
if hasCollection == false {
println("Create collection failed: " + status.GetMessage())
return
}
println("Collection: " + collectionName + " exist")
println("**************************************************")
//test show collections
var collections []string
collections, status, err = client.ListCollections(ctx)
if err != nil {
println("ShowCollections rpc failed: " + err.Error())
return
}
if !status.Ok() {
println("Show collections failed: " + status.GetMessage())
return
}
println("ShowCollections: ")
for i = 0; i < int64(len(collections)); i++ {
println(" - " + collections[i])
}
//test insert vectors
records := make([]milvus.Entity, nb)
recordArray := make([][]float32, nb)
for i = 0; i < nb; i++ {
recordArray[i] = make([]float32, dimension)
for j = 0; j < dimension; j++ {
recordArray[i][j] = float32(i % (j + 1))
}
records[i].FloatData = recordArray[i]
}
insertParam := milvus.InsertParam{collectionName, "", records, nil}
id_array, status, err := client.Insert(ctx, &insertParam)
if err != nil {
println("Insert rpc failed: " + err.Error())
return
}
if !status.Ok() {
println("Insert vector failed: " + status.GetMessage())
return
}
if len(id_array) != int(nb) {
println("ERROR: return id array is null")
}
println("Insert vectors success!")
time.Sleep(3 * time.Second)
//test describe collection
collectionParam, status, err = client.GetCollectionInfo(ctx, collectionName)
if err != nil {
println("DescribeCollection rpc failed: " + err.Error())
return
}
if !status.Ok() {
println("Create index failed: " + status.GetMessage())
return
}
println("CollectionName:" + collectionParam.CollectionName + "----Dimension:" + strconv.Itoa(int(collectionParam.Dimension)) +
"----IndexFileSize:" + strconv.Itoa(int(collectionParam.IndexFileSize)))
//Construct query vectors
queryRecords := make([]milvus.Entity, nq)
queryVectors := make([][]float32, nq)
for i = 0; i < nq; i++ {
queryVectors[i] = make([]float32, dimension)
for j = 0; j < dimension; j++ {
queryVectors[i][j] = float32(i % (j + 1))
}
queryRecords[i].FloatData = queryVectors[i]
}
println("**************************************************")
//Search without create index
var topkQueryResult milvus.TopkQueryResult
extraParams := "{\"nprobe\" : 32}"
searchParam := milvus.SearchParam{collectionName, queryRecords, topk, nil, extraParams}
topkQueryResult, status, err = client.Search(ctx, searchParam)
if err != nil {
println("Search rpc failed: " + err.Error())
}
println("Search without index results: ")
for i = 0; i < 10; i++ {
print(topkQueryResult.QueryResultList[i].Ids[0])
print(" ")
println(topkQueryResult.QueryResultList[i].Distances[0])
}
println("**************************************************")
//test CountCollection
var collectionCount int64
collectionCount, status, err = client.CountEntities(ctx, collectionName)
if err != nil {
println("CountCollection rpc failed: " + err.Error())
return
}
if !status.Ok() {
println("Get collection count failed: " + status.GetMessage())
return
}
println("Collection count:" + strconv.Itoa(int(collectionCount)))
//Create index
println("Start create index...")
extraParams = "{\"nlist\" : 16384}"
indexParam := milvus.IndexParam{collectionName, milvus.IVFFLAT, extraParams}
status, err = client.CreateIndex(ctx, &indexParam)
if err != nil {
println("CreateIndex rpc failed: " + err.Error())
return
}
if !status.Ok() {
println("Create index failed: " + status.GetMessage())
return
}
println("Create index success!")
//Describe index
indexParam, status, err = client.GetIndexInfo(ctx, collectionName)
if err != nil {
println("DescribeIndex rpc failed: " + err.Error())
return
}
if !status.Ok() {
println("Describe index failed: " + status.GetMessage())
}
println(indexParam.CollectionName + "----index type:" + strconv.Itoa(int(indexParam.IndexType)))
//Preload collection
loadCollectionParam := milvus.LoadCollectionParam{collectionName, nil}
status, err = client.LoadCollection(ctx, loadCollectionParam)
if err != nil {
println("PreloadCollection rpc failed: " + err.Error())
return
}
if !status.Ok() {
println(status.GetMessage())
}
println("Preload collection success")
println("**************************************************")
//Search with IVFSQ8 index
extraParams = "{\"nprobe\" : 32}"
searchParam = milvus.SearchParam{collectionName, queryRecords, topk, nil, extraParams}
topkQueryResult, status, err = client.Search(ctx, searchParam)
if err != nil {
println("Search rpc failed: " + err.Error())
return
}
if !status.Ok() {
println("Search vectors failed: " + status.GetMessage())
}
println("Search with index results: ")
for i = 0; i < 10; i++ {
print(topkQueryResult.QueryResultList[i].Ids[0])
print(" ")
println(topkQueryResult.QueryResultList[i].Distances[0])
}
println("**************************************************")
//Drop index
status, err = client.DropIndex(ctx, collectionName)
if err != nil {
println("DropIndex rpc failed: " + err.Error())
return
}
if !status.Ok() {
println("Drop index failed: " + status.GetMessage())
}
//Drop collection
status, err = client.DropCollection(ctx, collectionName)
hasCollection, status1, err := client.HasCollection(ctx, collectionName)
if !status.Ok() || !status1.Ok() || hasCollection == true {
println("Drop collection failed: " + status.GetMessage())
return
}
println("Drop collection " + collectionName + " success!")
//GetConfig
var configInfo string
configInfo, status, err = client.GetConfig(ctx, "*")
if !status.Ok() {
println("Get config failed: " + status.GetMessage())
}
println("config: ")
println(configInfo)
//Disconnect
err = client.Disconnect(ctx)
if err != nil {
println("Disconnect failed!")
return
}
println("Client disconnect server success!")
//Server status
var serverStatus string
serverStatus, status, err = client.ServerStatus(ctx)
if !status.Ok() {
println("Get server status failed: " + status.GetMessage())
}
println("Server status: " + serverStatus)
}
func main() {
address := "localhost"
port := "19530"
example(address, port)
}