github.com/milvus-io/milvus-sdk-go/v2@v2.4.1/examples/auth/auth.go (about) 1 package main 2 3 import ( 4 "context" 5 "encoding/csv" 6 "fmt" 7 "log" 8 "os" 9 "strconv" 10 "strings" 11 12 "github.com/milvus-io/milvus-sdk-go/v2/client" 13 "github.com/milvus-io/milvus-sdk-go/v2/entity" 14 ) 15 16 const ( 17 addr = "localhost:19530" 18 username = "username" 19 password = "password" 20 ) 21 22 func main() { 23 ctx := context.Background() 24 // create grpc client that tls is enabled 25 c, err := client.NewClient( 26 ctx, 27 client.Config{ 28 Address: addr, 29 Username: username, 30 Password: password, 31 }, 32 ) 33 if err != nil { 34 log.Fatal("failed to connect:", err) 35 } 36 37 // here is the collection name we use in this example 38 collectionName := `gosdk_insert_example` 39 40 has, err := c.HasCollection(ctx, collectionName) 41 if err != nil { 42 log.Fatal("failed to check whether collection exists:", err.Error()) 43 } 44 if has { 45 // collection with same name exist, clean up mess 46 _ = c.DropCollection(ctx, collectionName) 47 } 48 49 // define collection schema, see film.csv 50 schema := &entity.Schema{ 51 CollectionName: collectionName, 52 Description: "this is the example collection for inser and search", 53 AutoID: false, 54 Fields: []*entity.Field{ 55 { 56 Name: "ID", 57 DataType: entity.FieldTypeInt64, // int64 only for now 58 PrimaryKey: true, 59 AutoID: false, 60 }, 61 { 62 Name: "Year", 63 DataType: entity.FieldTypeInt32, 64 PrimaryKey: false, 65 AutoID: false, 66 }, 67 { 68 Name: "Vector", 69 DataType: entity.FieldTypeFloatVector, 70 TypeParams: map[string]string{ 71 entity.TypeParamDim: "8", 72 }, 73 }, 74 }, 75 } 76 77 err = c.CreateCollection(ctx, schema, 1) // only 1 shard 78 if err != nil { 79 log.Fatal("failed to create collection:", err.Error()) 80 } 81 82 films, err := loadFilmCSV() 83 if err != nil { 84 log.Fatal("failed to load film data csv:", err.Error()) 85 } 86 87 // row-base covert to column-base 88 ids := make([]int64, 0, len(films)) 89 years := make([]int32, 0, len(films)) 90 vectors := make([][]float32, 0, len(films)) 91 // string field is not supported yet 92 idTitle := make(map[int64]string) 93 for idx, film := range films { 94 ids = append(ids, film.ID) 95 idTitle[film.ID] = film.Title 96 years = append(years, film.Year) 97 vectors = append(vectors, films[idx].Vector[:]) // prevent same vector 98 } 99 idColumn := entity.NewColumnInt64("ID", ids) 100 yearColumn := entity.NewColumnInt32("Year", years) 101 vectorColumn := entity.NewColumnFloatVector("Vector", 8, vectors) 102 103 // insert into default partition 104 _, err = c.Insert(ctx, collectionName, "", idColumn, yearColumn, vectorColumn) 105 if err != nil { 106 log.Fatal("failed to insert film data:", err.Error()) 107 } 108 log.Println("insert completed") 109 110 err = c.Flush(ctx, collectionName, false) 111 if err != nil { 112 log.Fatal("failed to flush collection:", err.Error()) 113 } 114 log.Println("flush completed") 115 116 // load collection with async=false 117 err = c.LoadCollection(ctx, collectionName, false) 118 if err != nil { 119 log.Fatal("failed to load collection:", err.Error()) 120 } 121 log.Println("load collection completed") 122 123 searchFilm := films[0] // use first fim to search 124 vector := entity.FloatVector(searchFilm.Vector[:]) 125 // Use flat search param 126 sp, _ := entity.NewIndexFlatSearchParam() 127 sr, err := c.Search(ctx, collectionName, []string{}, "Year > 1990", []string{"ID"}, []entity.Vector{vector}, "Vector", 128 entity.L2, 10, sp) 129 if err != nil { 130 log.Fatal("fail to search collection:", err.Error()) 131 } 132 for _, result := range sr { 133 var idColumn *entity.ColumnInt64 134 for _, field := range result.Fields { 135 if field.Name() == "ID" { 136 c, ok := field.(*entity.ColumnInt64) 137 if ok { 138 idColumn = c 139 } 140 } 141 } 142 if idColumn == nil { 143 log.Fatal("result field not math") 144 } 145 for i := 0; i < result.ResultCount; i++ { 146 id, err := idColumn.ValueByIdx(i) 147 if err != nil { 148 log.Fatal(err.Error()) 149 } 150 title := idTitle[id] 151 fmt.Printf("file id: %d title: %s scores: %f\n", id, title, result.Scores[i]) 152 } 153 } 154 155 // clean up 156 _ = c.DropCollection(ctx, collectionName) 157 } 158 159 type film struct { 160 ID int64 161 Title string 162 Year int32 163 Vector [8]float32 // fix length array 164 } 165 166 func loadFilmCSV() ([]film, error) { 167 f, err := os.Open("../films.csv") // assume you are in examples/insert folder, if not, please change the path 168 if err != nil { 169 return []film{}, err 170 } 171 r := csv.NewReader(f) 172 raw, err := r.ReadAll() 173 if err != nil { 174 return []film{}, err 175 } 176 films := make([]film, 0, len(raw)) 177 for _, line := range raw { 178 if len(line) < 4 { // insuffcient column 179 continue 180 } 181 fi := film{} 182 // ID 183 v, err := strconv.ParseInt(line[0], 10, 64) 184 if err != nil { 185 continue 186 } 187 fi.ID = v 188 // Title 189 fi.Title = line[1] 190 // Year 191 v, err = strconv.ParseInt(line[2], 10, 64) 192 if err != nil { 193 continue 194 } 195 fi.Year = int32(v) 196 // Vector 197 vectorStr := strings.ReplaceAll(line[3], "[", "") 198 vectorStr = strings.ReplaceAll(vectorStr, "]", "") 199 parts := strings.Split(vectorStr, ",") 200 if len(parts) != 8 { // dim must be 8 201 continue 202 } 203 for idx, part := range parts { 204 part = strings.TrimSpace(part) 205 v, err := strconv.ParseFloat(part, 32) 206 if err != nil { 207 continue 208 } 209 fi.Vector[idx] = float32(v) 210 } 211 films = append(films, fi) 212 } 213 return films, nil 214 }