本页面提供一个向量数据里 VikingDB 通过 Go SDK 创建数据集、写入数据、创建索引和检索查询的完整请求示例。
//创建一个Collection,collectionName命名为go。 fields := []vikingdb.Field{ { FieldName: "doc_id", FieldType: vikingdb.String, IsPrimaryKey: true, }, { FieldName: "text_vector", FieldType: vikingdb.Vector, Dim: 12, }, { FieldName: "like", FieldType: vikingdb.Int64, DefaultVal: 0, }, { FieldName: "price", FieldType: vikingdb.Float32, Dim: 12, }, { FieldName: "author", FieldType: vikingdb.ListString, DefaultVal: []string{}, }, { FieldName: "aim", FieldType: vikingdb.Bool, DefaultVal: true, }, } collection, err := service.CreateCollection("go", fields, "this is a go example") if err != nil { print(err.Error()) } //为当前collection写入数据。 collection, _ := service.GetCollection("go") field1 := map[string]interface{}{ "doc_id": "111", "text_vector": genRandomVector(12), "like": 1, "price": 1.11, "author": []string{"gy"}, "aim": true, } field2 := map[string]interface{}{ "doc_id": "222", "text_vector": genRandomVector(12), "like": 2, "price": 2.22, "author": []string{"gy", "xjq"}, "aim": false, } field3 := map[string]interface{}{ "doc_id": "333", "text_vector": genRandomVector(12), "like": 3, "price": 3.33, "author": []string{"gy"}, "aim": true, } field4 := map[string]interface{}{ "doc_id": "444", "text_vector": genRandomVector(12), "like": 4, "price": 4.44, "author": []string{"gy"}, "aim": true, } data1 := vikingdb.Data{ Fields: field1, TTL: 100000, } data2 := vikingdb.Data{ Fields: field2, TTL: 200000, } data3 := vikingdb.Data{ Fields: field3, TTL: 100000, } data4 := vikingdb.Data{ Fields: field4, } datas := []vikingdb.Data{ data1, data2, data3, data4, } err := collection.UpsertData(datas) if err != nil { print(err.Error()) } //在当前collection查询数据 collection, _ := service.GetCollection("go") res, err := collection.FetchData([]string{"111", "222", "333", "444"}) if err != nil { fmt.Println(err) } for _, item := range res { fmt.Println(item) } //为指定Collection创建Index,indexName命名为goIndex。 vectorIndex := &vikingdb.VectorIndexParams{ Distance: vikingdb.COSINE, IndexType: vikingdb.HNSW, } indexOptions := vikingdb.NewIndexOptions().SetVectorIndex(vectorIndex).SetCpuQuota(2).SetDescription("this is an index").SetPartitionBy("").SetScalarIndex([]string{"price", "like"}) index, err := service.CreateIndex("go", "goIndex", indexOptions) if err != nil { fmt.Println(err) } fmt.Println(index) //混合检索 index, _ := service.GetIndex("go", "goIndex") searchOption := vikingdb.NewSearchOptions().SetFilter(map[string]interface{}{"op": "range", "field": "price", "lt": 3.5}).SetLimit(5).SetOutputFields([]string{"doc_id", "like", "text_vector", "price"}) res, err := index.Search(vikingdb.VectorOrder{Id: "111"}, searchOption) if err != nil { fmt.Println(err) } for _, item := range res { fmt.Println(item) } // 或者 index, _ := service.GetIndex("go", "goIndex") searchOption := vikingdb.NewSearchOptions().SetFilter(map[string]interface{}{"op": "range", "field": "price", "lt": 3.5}).SetLimit(5).SetOutputFields([]string{"doc_id", "like", "text_vector", "price"}) res, err := index.Search(vikingdb.ScalarOrder{FieldName: "price", Order: vikingdb.Desc}, searchOption) if err != nil { fmt.Println(err) } for _, item := range res { fmt.Println(item) }