forked from milvus-io/milvus-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.go
206 lines (188 loc) · 5.59 KB
/
index.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
package main
import (
"context"
"encoding/csv"
"fmt"
"log"
"os"
"strconv"
"strings"
"time"
"github.com/milvus-io/milvus-sdk-go/v2/client"
"github.com/milvus-io/milvus-sdk-go/v2/entity"
)
func main() {
// Milvus instance proxy address, may verify in your env/settings
milvusAddr := `localhost:19530`
// setup context
ctx, cancel := context.WithCancel(context.Background())
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())
}
// in a main func, remember to close the client
defer c.Close()
// here is the collection name we use in this example
collectionName := `gosdk_index_example`
has, err := c.HasCollection(ctx, collectionName)
if err != nil {
log.Fatal("failed to check whether collection exists:", err.Error())
}
if has {
// collection with same name exist, clean up mess
_ = c.DropCollection(ctx, collectionName)
}
// define collection schema, see film.csv
schema := entity.NewSchema().WithName(collectionName).WithDescription("this is the example collection for indexing").
WithField(entity.NewField().WithName("ID").WithDataType(entity.FieldTypeInt64).WithIsPrimaryKey(true)).
WithField(entity.NewField().WithName("Year").WithDataType(entity.FieldTypeInt32)).
WithField(entity.NewField().WithName("Vector").WithDataType(entity.FieldTypeFloatVector).WithDim(8))
err = c.CreateCollection(ctx, schema, entity.DefaultShardNumber)
if err != nil {
log.Fatal("failed to create collection:", err.Error())
}
films, err := loadFilmCSV()
if err != nil {
log.Fatal("failed to load film data csv:", err.Error())
}
// row-base covert to column-base
ids := make([]int64, 0, len(films))
years := make([]int32, 0, len(films))
vectors := make([][]float32, 0, len(films))
// string field is not supported yet
idTitle := make(map[int64]string)
for idx, film := range films {
ids = append(ids, film.ID)
idTitle[film.ID] = film.Title
years = append(years, film.Year)
vectors = append(vectors, films[idx].Vector[:]) // prevent same vector
}
idColumn := entity.NewColumnInt64("ID", ids)
yearColumn := entity.NewColumnInt32("Year", years)
vectorColumn := entity.NewColumnFloatVector("Vector", 8, vectors)
// insert into default partition
_, err = c.Insert(ctx, collectionName, "", idColumn, yearColumn, vectorColumn)
if err != nil {
log.Fatal("failed to insert film data:", err.Error())
}
log.Println("insert completed")
ctx, cancel = context.WithTimeout(context.Background(), time.Second*120)
defer cancel()
err = c.Flush(ctx, collectionName, false)
if err != nil {
log.Fatal("failed to flush collection:", err.Error())
}
log.Println("flush completed")
// Now add index
idx, err := entity.NewIndexIvfFlat(entity.L2, 2)
if err != nil {
log.Fatal("fail to create ivf flat index:", err.Error())
}
err = c.CreateIndex(ctx, collectionName, "Vector", idx, false)
if err != nil {
log.Fatal("fail to create index:", err.Error())
}
sidx := entity.NewScalarIndex()
if err := c.CreateIndex(ctx, collectionName, "Year", sidx, false); err != nil {
log.Fatal("failed to create scalar index", err.Error())
}
// load collection with async=false
err = c.LoadCollection(ctx, collectionName, false)
if err != nil {
log.Fatal("failed to load collection:", err.Error())
}
log.Println("load collection completed")
searchFilm := films[0] // use first fim to search
vector := entity.FloatVector(searchFilm.Vector[:])
sp, _ := entity.NewIndexFlatSearchParam()
start := time.Now()
sr, err := c.Search(ctx, collectionName, []string{}, "Year > 1990", []string{"ID"}, []entity.Vector{vector}, "Vector",
entity.L2, 10, sp)
if err != nil {
log.Fatal("fail to search collection:", err.Error())
}
log.Println("search without index time elapsed:", time.Since(start))
for _, result := range sr {
var idColumn *entity.ColumnInt64
for _, field := range result.Fields {
if field.Name() == "ID" {
c, ok := field.(*entity.ColumnInt64)
if ok {
idColumn = c
}
}
}
if idColumn == nil {
log.Fatal("result field not math")
}
for i := 0; i < result.ResultCount; i++ {
id, err := idColumn.ValueByIdx(i)
if err != nil {
log.Fatal(err.Error())
}
title := idTitle[id]
fmt.Printf("file id: %d title: %s scores: %f\n", id, title, result.Scores[i])
}
}
// clean up
_ = c.DropCollection(ctx, collectionName)
}
type film struct {
ID int64
Title string
Year int32
Vector [8]float32 // fix length array
}
func loadFilmCSV() ([]film, error) {
f, err := os.Open("../films.csv") // assume you are in examples/insert folder, if not, please change the path
if err != nil {
return []film{}, err
}
r := csv.NewReader(f)
raw, err := r.ReadAll()
if err != nil {
return []film{}, err
}
films := make([]film, 0, len(raw))
for _, line := range raw {
if len(line) < 4 { // insuffcient column
continue
}
fi := film{}
// ID
v, err := strconv.ParseInt(line[0], 10, 64)
if err != nil {
continue
}
fi.ID = v
// Title
fi.Title = line[1]
// Year
v, err = strconv.ParseInt(line[2], 10, 64)
if err != nil {
continue
}
fi.Year = int32(v)
// Vector
vectorStr := strings.ReplaceAll(line[3], "[", "")
vectorStr = strings.ReplaceAll(vectorStr, "]", "")
parts := strings.Split(vectorStr, ",")
if len(parts) != 8 { // dim must be 8
continue
}
for idx, part := range parts {
part = strings.TrimSpace(part)
v, err := strconv.ParseFloat(part, 32)
if err != nil {
continue
}
fi.Vector[idx] = float32(v)
}
films = append(films, fi)
}
return films, nil
}