接口定义
ExistsCollection() 用于检查指定的数据库及其对应的集合是否已经存在。
CreateCollectionIfNotExists() 用于检查指定的数据库及其对应的集合是否已经存在,若不存在直接创建集合。
func (tcvectordb.CollectionInterface) ExistsCollection(ctx context.Context, name string) (bool, error)
func (tcvectordb.CollectionInterface) CreateCollectionIfNotExists(ctx context.Context, name string, shardNum uint32, replicasNum uint32, description string, indexes tcvectordb.Indexes, params ...*tcvectordb.CreateCollectionParams) (*tcvectordb.Collection, error)
使用示例
var (ctx = context.Background()database = "go-sdk-test-db"collectionName = "book-vector")db := client.Database(database)collExists, err := db.ExistsCollection(ctx, collectionName)printErr(err)log.Printf("collection %v exists: %v", collectionName, collExists)
参数名 | 是否必须 | 参数含义 | 配置方法 |
name | 是 | 检查的集合名。 | Collection 命名要求如下: 只能使用英文字母,数字,下划线_、中划线-,并以英文字母开头。 长度要求:[1,128]。 |
var (ctx = context.Background()database = "go-sdk-test-db"collectionName = "book-vector")db := client.Database(database)index := tcvectordb.Indexes{VectorIndex: []tcvectordb.VectorIndex{{FilterIndex: tcvectordb.FilterIndex{FieldName: "vector",FieldType: tcvectordb.Vector,IndexType: tcvectordb.HNSW,},Dimension: 3,MetricType: tcvectordb.COSINE,Params: &tcvectordb.HNSWParam{M: 16,EfConstruction: 200,},},},FilterIndex: []tcvectordb.FilterIndex{{FieldName: "id", FieldType: tcvectordb.String, IndexType: tcvectordb.PRIMARY},{FieldName: "bookName", FieldType: tcvectordb.String, IndexType: tcvectordb.FILTER},},}param := &tcvectordb.CreateCollectionParams{TtlConfig: &tcvectordb.TtlConfig{Enable: true,imeField: "expire_at",},}coll, err := db.CreateCollectionIfNotExists(ctx, collectionName, 3, 1, "test collection", index, param)printErr(err)log.Printf("create collection if not exists success: %v: %v", coll.DatabaseName, coll.CollectionName)
参数名 | 是否必须 | 参数含义 | 配置方法 |
name | 是 | 检查的集合名。 | Collection 命名要求如下: 只能使用英文字母,数字,下划线_、中划线-,并以英文字母开头。 长度要求:[1,128]。 说明: |