Skip to content

Commit

Permalink
Support tair vector command
Browse files Browse the repository at this point in the history
  • Loading branch information
douchao.douchao authored and yangbodong22011 committed Nov 23, 2023
1 parent 8963454 commit 05020d0
Show file tree
Hide file tree
Showing 4 changed files with 1,251 additions and 0 deletions.
70 changes: 70 additions & 0 deletions example/tairvector/example_vector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package main

import (
"context"
"encoding/json"
"fmt"
"github.com/alibaba/tair-go/tair"
"github.com/go-redis/redis/v8"
"math/rand"
)

var ctx = context.Background()

var tairClient *tair.TairClient // 全局客户端

var ip = "127.0.0.1"

func init() {
tairClient = tair.NewTairClient(&redis.Options{
Addr: ip + ":" + "6379",
Password: "", // no password set
DB: 0, // use default DB
})
}

func ExampleTairVector() {
index := "vector-index"
dim := 4
tairClient.TvsCreateIndex(ctx, index, dim, "HNSW", "L2",
tair.TvsCreateIndexArgs{}.New().AutoGc(true).M(16).DataType("FLOAT16")).Result()

for i := 0; i < 100; i++ {
key := fmt.Sprintf("test_key_%d", i)
fields := make(map[string]interface{})
fields["field1"] = "value1"
fields["field2"] = rand.Intn(100)
fields["field3"] = rand.Float32()
fields["field4"] = rand.Intn(2) == 0
floats := make([]float32, dim)
for i := 0; i < dim; i++ {
floats[i] = rand.Float32()
}
b, _ := json.Marshal(floats)
fields["VECTOR"] = string(b)

result, err := tairClient.TvsHSet(ctx, index, key, tair.TvsHSetArgs{}.New().Fields(fields)).Result()
if err != nil {
fmt.Println(err.Error())
panic(err)
}

if result != 5 {
fmt.Println("tvsHSet failed")
panic(result)
}
}

result, err := tairClient.TvsKnnSearch(ctx, index, 10, "[0,0,0,0]", nil).Result()
if err != nil {
fmt.Println(err.Error())
panic(err)
}
fmt.Println(result)

tairClient.TvsDelIndex(ctx, index)
}

func main() {
ExampleTairVector()
}
26 changes: 26 additions & 0 deletions tair/taircommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,32 @@ type TairCmdable interface {
BfMExists(ctx context.Context, key string, items ...string) *redis.BoolSliceCmd
BfInsert(ctx context.Context, key string, bfInsertArgs *BfInsertArgs, items ...string) *redis.BoolSliceCmd
BfDebug(ctx context.Context, key string) *redis.StringSliceCmd
// TairVector
TvsCreateIndex(ctx context.Context, name string, dim int, indexType string, distanceType string, a *TvsCreateIndexArgs) *redis.StatusCmd
TvsGetIndex(ctx context.Context, name string) *redis.SliceCmd
TvsDelIndex(ctx context.Context, name string) *redis.IntCmd
TvsScanIndex(ctx context.Context, cursor string, a *TvsScanIndexArgs) *redis.SliceCmd
TvsHSet(ctx context.Context, index string, key string, a *TvsHSetArgs) *redis.IntCmd
TvsHGetAll(ctx context.Context, index string, key string) *redis.SliceCmd
TvsHMGet(ctx context.Context, index string, key string, fields []string) *redis.SliceCmd
TvsDel(ctx context.Context, index string, keys []string) *redis.IntCmd
TvsHDel(ctx context.Context, index string, key string, fields []string) *redis.IntCmd
TvsScan(ctx context.Context, index string, cursor string, a *TvsScanArgs) *redis.SliceCmd
TvsHIncrBy(ctx context.Context, index string, key string, field string, value int64) *redis.IntCmd
TvsHIncrByFloat(ctx context.Context, index string, key string, field string, value float64) *redis.FloatCmd
TvsHPExpire(ctx context.Context, index string, key string, milliseconds int) *redis.IntCmd
TvsHPExpireAt(ctx context.Context, index string, key string, milliUnixTime int) *redis.IntCmd
TvsHExpire(ctx context.Context, index string, key string, seconds int) *redis.IntCmd
TvsHExpireAt(ctx context.Context, index string, key string, unixTime int) *redis.IntCmd
TvsHPTTL(ctx context.Context, index string, key string) *redis.IntCmd
TvsHTTL(ctx context.Context, index string, key string) *redis.IntCmd
TvsHPExpireTime(ctx context.Context, index string, key string) *redis.IntCmd
TvsHExpireTime(ctx context.Context, index string, key string) *redis.IntCmd
TvsKnnSearch(ctx context.Context, index string, topN int, vector string, a *TvsKnnSearchArgs) *redis.SliceCmd
TvsGetDistance(ctx context.Context, index string, vector string, keys []string, a *TvsGetDistanceArgs) *redis.SliceCmd
TvsMKnnSearch(ctx context.Context, index string, topN int, vectors []string, a *TvsKnnSearchArgs) *redis.SliceCmd
TvsMIndexKnnSearch(ctx context.Context, indexes []string, topN int, vector string, a *TvsKnnSearchArgs) *redis.SliceCmd
TvsMIndexMKnnSearch(ctx context.Context, indexes []string, topN int, vectors []string, a *TvsKnnSearchArgs) *redis.SliceCmd
// TairPipeline
TairPipeline() TairPipeline
TairPipelined(ctx context.Context, fn func(redis.Pipeliner) error) ([]redis.Cmder, error)
Expand Down
Loading

0 comments on commit 05020d0

Please sign in to comment.