Skip to content

Commit

Permalink
Merge branch 'feat/add-index-interface' into 'main' (merge request !59)
Browse files Browse the repository at this point in the history
feat/add-index-interface
feat: add new index/add interface
  • Loading branch information
rogersqsliu committed Nov 6, 2024
2 parents 451f92e + ff30f21 commit c5b9aa4
Show file tree
Hide file tree
Showing 16 changed files with 940 additions and 227 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Changelog

## v1.4.4
* 新增/index/add接口实现

## v1.0.0

### DatabaseInterface
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
## Getting started

### Prerequisites
Go 1.15 or higher
1. Go 1.15 or higher
2. Only support to use bm25 tcvdbtext package when the system is macos or linux, and you should install gcc firstly. Because gojieba in tcvdbtext package use cgo feature in go.


### Install TencentCloud VectorDB Go SDK

Expand Down Expand Up @@ -31,4 +33,4 @@ db, err := cli.CreateDatabase(context.Background(), "DATABASE NAME")

### Examples

See [example](example) about how to use this package to communicate with TencentCloud VectorDB
See [example](example) about how to use this package to communicate with TencentCloud VectorDB
12 changes: 12 additions & 0 deletions tcvectordb/api/index/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,15 @@ type RebuildRes struct {
api.CommonRes
TaskIds []string `json:"task_ids,omitempty"`
}

type AddReq struct {
api.Meta `path:"/index/add" tags:"Index" method:"Post" summary:"新增collection的索引"`
Database string `json:"database,omitempty"`
Collection string `json:"collection,omitempty"`
Indexes []*api.IndexColumn `json:"indexes,omitempty"`
BuildExistedData *bool `json:"buildExistedData,omitempty"`
}

type AddRes struct {
api.CommonRes
}
10 changes: 10 additions & 0 deletions tcvectordb/base_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,9 @@ func (i *implementerCollection) Collection(name string) *Collection {
flatImpl := new(implementerFlatDocument)
flatImpl.SdkClient = i.SdkClient

flatIndexImpl := new(implementerFlatIndex)
flatIndexImpl.SdkClient = i.SdkClient

docImpl := new(implementerDocument)
docImpl.SdkClient = i.SdkClient
docImpl.database = i.database
Expand All @@ -290,6 +293,7 @@ func (i *implementerCollection) Collection(name string) *Collection {
indexImpl.SdkClient = i.SdkClient
indexImpl.database = i.database
indexImpl.collection = coll
indexImpl.flat = flatIndexImpl

coll.DocumentInterface = docImpl
coll.IndexInterface = indexImpl
Expand Down Expand Up @@ -382,16 +386,22 @@ func (i *implementerCollection) toCollection(collectionItem *collection.Describe

flatImpl := new(implementerFlatDocument)
flatImpl.SdkClient = i.SdkClient

flatIdexImpl := new(implementerFlatIndex)
flatIdexImpl.SdkClient = i.SdkClient

docImpl := new(implementerDocument)
docImpl.SdkClient = i.SdkClient
docImpl.database = i.database
docImpl.collection = coll
docImpl.flat = flatImpl
coll.DocumentInterface = docImpl

indexImpl := new(implementerIndex)
indexImpl.SdkClient = i.SdkClient
indexImpl.database = i.database
indexImpl.collection = coll
indexImpl.flat = flatIdexImpl
coll.IndexInterface = indexImpl
return coll
}
Expand Down
33 changes: 6 additions & 27 deletions tcvectordb/base_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,52 +20,31 @@ package tcvectordb

import (
"context"

"github.com/tencent/vectordatabase-sdk-go/tcvectordb/api/index"
)

var _ IndexInterface = &implementerIndex{}

type IndexInterface interface {
SdkClient
RebuildIndex(ctx context.Context, params ...*RebuildIndexParams) (result *RebuildIndexResult, err error)
AddIndex(ctx context.Context, params ...*AddIndexParams) (err error)
}

type implementerIndex struct {
SdkClient
flat FlatIndexInterface
database *Database
collection *Collection
}

type RebuildIndexParams struct {
DropBeforeRebuild bool
Throttle int
}

type RebuildIndexResult struct {
TaskIds []string
}

func (i *implementerIndex) RebuildIndex(ctx context.Context, params ...*RebuildIndexParams) (*RebuildIndexResult, error) {
if i.database.IsAIDatabase() {
return nil, AIDbTypeError
}
req := new(index.RebuildReq)
req.Database = i.database.DatabaseName
req.Collection = i.collection.CollectionName

if len(params) != 0 && params[0] != nil {
param := params[0]
req.DropBeforeRebuild = param.DropBeforeRebuild
req.Throttle = int32(param.Throttle)
}
return i.flat.RebuildIndex(ctx, i.database.DatabaseName, i.collection.CollectionName, params...)
}

res := new(index.RebuildRes)
err := i.Request(ctx, req, &res)
if err != nil {
return nil, err
}
result := new(RebuildIndexResult)
result.TaskIds = res.TaskIds
return result, nil
func (i *implementerIndex) AddIndex(ctx context.Context, params ...*AddIndexParams) error {
return i.flat.AddIndex(ctx, i.database.DatabaseName, i.collection.CollectionName, params...)
}
75 changes: 75 additions & 0 deletions tcvectordb/base_index_flat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package tcvectordb

import (
"context"

"github.com/tencent/vectordatabase-sdk-go/tcvectordb/api"
"github.com/tencent/vectordatabase-sdk-go/tcvectordb/api/index"
)

var _ FlatIndexInterface = &implementerFlatIndex{}

type FlatIndexInterface interface {
SdkClient
RebuildIndex(ctx context.Context, databaseName, collectionName string, params ...*RebuildIndexParams) (result *RebuildIndexResult, err error)
AddIndex(ctx context.Context, databaseName, collectionName string, params ...*AddIndexParams) (err error)
}

type implementerFlatIndex struct {
SdkClient
}

type RebuildIndexParams struct {
DropBeforeRebuild bool
Throttle int
}

type AddIndexParams struct {
FilterIndexs []FilterIndex
BuildExistedData *bool
}

func (i *implementerFlatIndex) RebuildIndex(ctx context.Context, databaseName, collectionName string, params ...*RebuildIndexParams) (*RebuildIndexResult, error) {
req := new(index.RebuildReq)
req.Database = databaseName
req.Collection = collectionName

if len(params) != 0 && params[0] != nil {
param := params[0]
req.DropBeforeRebuild = param.DropBeforeRebuild
req.Throttle = int32(param.Throttle)
}

res := new(index.RebuildRes)
err := i.Request(ctx, req, &res)
if err != nil {
return nil, err
}
result := new(RebuildIndexResult)
result.TaskIds = res.TaskIds
return result, nil
}

func (i *implementerFlatIndex) AddIndex(ctx context.Context, databaseName, collectionName string, params ...*AddIndexParams) error {
req := new(index.AddReq)
req.Database = databaseName
req.Collection = collectionName
if len(params) != 0 && params[0] != nil {
param := params[0]
for _, index := range param.FilterIndexs {
req.Indexes = append(req.Indexes, &api.IndexColumn{
FieldName: index.FieldName,
FieldType: string(index.FieldType),
IndexType: string(index.IndexType),
})
}
req.BuildExistedData = param.BuildExistedData
}

res := new(index.AddRes)
err := i.Request(ctx, req, res)
if err != nil {
return err
}
return nil
}
4 changes: 4 additions & 0 deletions tcvectordb/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type ClientOption struct {
type Client struct {
DatabaseInterface
FlatInterface
FlatIndexInterface

cli *http.Client
url string
Expand Down Expand Up @@ -123,9 +124,12 @@ func newClient(url, username, key string, option ClientOption) (*Client, error)
databaseImpl.SdkClient = cli
flatImpl := new(implementerFlatDocument)
flatImpl.SdkClient = cli
flatIndexImpl := new(implementerFlatIndex)
flatIndexImpl.SdkClient = cli

cli.DatabaseInterface = databaseImpl
cli.FlatInterface = flatImpl
cli.FlatIndexInterface = flatIndexImpl
return cli, nil
}

Expand Down
Loading

0 comments on commit c5b9aa4

Please sign in to comment.