Skip to content

Commit

Permalink
Merge branch support-msearch into main
Browse files Browse the repository at this point in the history
        Title: [Tair-go]add msearch and getindex api

1. 添加msearch和getindex api
2. 添加searchUseCache
3. 删除delall多余参数
  • Loading branch information
yangbodong22011 committed Mar 2, 2023
2 parents d0ce1de + d81c1c4 commit 549ec30
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 44 deletions.
6 changes: 4 additions & 2 deletions tair/taircommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ type TairCmdable interface {
TftCreateIndex(ctx context.Context, index, request string) *redis.StringCmd
TftUpdateIndex(ctx context.Context, index, request string) *redis.StringCmd
TftGetIndexMappings(ctx context.Context, index string) *redis.StringCmd
TftGetIndex(ctx context.Context, index string) *redis.StringCmd
TftGetIndexSettings(ctx context.Context, index string) *redis.StringCmd
TftAddDoc(ctx context.Context, index string, request string) *redis.StringCmd
TftAddDocWithId(ctx context.Context, index string, request string, docId string) *redis.StringCmd
TftMAddDoc(ctx context.Context, index string, docs map[string]string) *redis.StringCmd
Expand All @@ -77,11 +79,11 @@ type TairCmdable interface {
TftIncrFloatDocField(ctx context.Context, index, docId, docContent string, value float64) *redis.FloatCmd
TftDelDocField(ctx context.Context, index, docId string, field ...string) *redis.IntCmd
TftGetDoc(ctx context.Context, index, docId string) *redis.StringCmd
TftGetDocWithFilter(ctx context.Context, index, docId, request string) *redis.StringCmd
TftDelDoc(ctx context.Context, index string, docId ...string) *redis.StringCmd
TftDelAll(ctx context.Context, index string, docId ...string) *redis.StringCmd
TftDelAll(ctx context.Context, index string) *redis.StringCmd
TftSearch(ctx context.Context, index string, request string) *redis.StringCmd
TftSearchUseCache(ctx context.Context, index string, request string, useCache bool) *redis.StringCmd
TftMSearch(ctx context.Context, indexCount int64, request string, index ...string) *redis.StringCmd
TftExists(ctx context.Context, index string, docId string) *redis.IntCmd
TftDocNum(ctx context.Context, index string) *redis.IntCmd
TftScanDocId(ctx context.Context, index string, cursor string) *redis.SliceCmd
Expand Down
97 changes: 58 additions & 39 deletions tair/tairsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ const (
NONE protocolType = iota
ProtoMatch
ProtoCount
ProtoMappings
ProtoSettings
)

func (p protocolType) String() string {
switch p {
case NONE:
return "NONE"
case ProtoMatch:
return "Match"
return "MATCH"
case ProtoCount:
return "Count"
return "COUNT"
default:
return "NA"
}
Expand Down Expand Up @@ -100,7 +102,7 @@ func (a *TftScanArgs) Count(count int64) *TftScanArgs {

func (tc tairCmdable) TftMappingIndex(ctx context.Context, index, request string) *redis.StringCmd {
a := make([]interface{}, 3)
a[0] = "tft.mappingindex"
a[0] = "TFT.MAPPINGINDEX"
a[1] = index
a[2] = request
cmd := redis.NewStringCmd(ctx, a...)
Expand All @@ -110,7 +112,7 @@ func (tc tairCmdable) TftMappingIndex(ctx context.Context, index, request string

func (tc tairCmdable) TftCreateIndex(ctx context.Context, index, request string) *redis.StringCmd {
a := make([]interface{}, 3)
a[0] = "tft.createindex"
a[0] = "TFT.CREATEINDEX"
a[1] = index
a[2] = request
cmd := redis.NewStringCmd(ctx, a...)
Expand All @@ -120,7 +122,7 @@ func (tc tairCmdable) TftCreateIndex(ctx context.Context, index, request string)

func (tc tairCmdable) TftUpdateIndex(ctx context.Context, index, request string) *redis.StringCmd {
a := make([]interface{}, 3)
a[0] = "tft.updateindex"
a[0] = "TFT.UPDATEINDEX"
a[1] = index
a[2] = request
cmd := redis.NewStringCmd(ctx, a...)
Expand All @@ -130,17 +132,36 @@ func (tc tairCmdable) TftUpdateIndex(ctx context.Context, index, request string)

func (tc tairCmdable) TftGetIndexMappings(ctx context.Context, index string) *redis.StringCmd {
a := make([]interface{}, 3)
a[0] = "tft.getindex"
a[0] = "TFT.GETINDEX"
a[1] = index
a[2] = "mappings"
a[2] = "MAPPINGS"
cmd := redis.NewStringCmd(ctx, a...)
_ = tc(ctx, cmd)
return cmd
}

func (tc tairCmdable) TftGetIndex(ctx context.Context, index string) *redis.StringCmd {
a := make([]interface{}, 2)
a[0] = "TFT.GETINDEX"
a[1] = index
cmd := redis.NewStringCmd(ctx, a...)
_ = tc(ctx, cmd)
return cmd
}

func (tc tairCmdable) TftGetIndexSettings(ctx context.Context, index string) *redis.StringCmd {
a := make([]interface{}, 3)
a[0] = "TFT.GETINDEX"
a[1] = index
a[2] = "SETTINGS"
cmd := redis.NewStringCmd(ctx, a...)
_ = tc(ctx, cmd)
return cmd
}

func (tc tairCmdable) TftAddDoc(ctx context.Context, index string, request string) *redis.StringCmd {
a := make([]interface{}, 3)
a[0] = "tft.adddoc"
a[0] = "TFT.ADDDOC"
a[1] = index
a[2] = request
cmd := redis.NewStringCmd(ctx, a...)
Expand All @@ -150,7 +171,7 @@ func (tc tairCmdable) TftAddDoc(ctx context.Context, index string, request strin

func (tc tairCmdable) TftAddDocWithId(ctx context.Context, index string, request string, docId string) *redis.StringCmd {
a := make([]interface{}, 5)
a[0] = "tft.adddoc"
a[0] = "TFT.ADDDOC"
a[1] = index
a[2] = request
a[3] = "WITH_ID"
Expand All @@ -162,7 +183,7 @@ func (tc tairCmdable) TftAddDocWithId(ctx context.Context, index string, request

func (tc tairCmdable) TftMAddDoc(ctx context.Context, index string, docs map[string]string) *redis.StringCmd {
a := make([]interface{}, 1)
a[0] = "tft.madddoc"
a[0] = "TFT.MADDDOC"
a = append(a, TftAddDocArgs{}.New().JoinArgs(index, docs)...)
cmd := redis.NewStringCmd(ctx, a...)
_ = tc(ctx, cmd)
Expand All @@ -171,7 +192,7 @@ func (tc tairCmdable) TftMAddDoc(ctx context.Context, index string, docs map[str

func (tc tairCmdable) TftUpdateDocField(ctx context.Context, index, docId, docContent string) *redis.StringCmd {
a := make([]interface{}, 4)
a[0] = "tft.updatedocfield"
a[0] = "TFT.UPDATEDOCFIELD"
a[1] = index
a[2] = docId
a[3] = docContent
Expand All @@ -181,7 +202,7 @@ func (tc tairCmdable) TftUpdateDocField(ctx context.Context, index, docId, docCo
}
func (tc tairCmdable) TftIncrLongDocField(ctx context.Context, index, docId, docContent string, value int64) *redis.IntCmd {
a := make([]interface{}, 5)
a[0] = "tft.incrlongdocfield"
a[0] = "TFT.INCRLONGDOCFIELD"
a[1] = index
a[2] = docId
a[3] = docContent
Expand All @@ -192,7 +213,7 @@ func (tc tairCmdable) TftIncrLongDocField(ctx context.Context, index, docId, doc
}
func (tc tairCmdable) TftIncrFloatDocField(ctx context.Context, index, docId, docContent string, value float64) *redis.FloatCmd {
a := make([]interface{}, 5)
a[0] = "tft.incrfloatdocfield"
a[0] = "TFT.INCRFLOATDOCFIELD"
a[1] = index
a[2] = docId
a[3] = docContent
Expand All @@ -204,7 +225,7 @@ func (tc tairCmdable) TftIncrFloatDocField(ctx context.Context, index, docId, do

func (tc tairCmdable) TftDelDocField(ctx context.Context, index, docId string, field ...string) *redis.IntCmd {
a := make([]interface{}, 3)
a[0] = "tft.deldocfield"
a[0] = "TFT.DELDOCFIELD"
a[1] = index
a[2] = docId
for _, f := range field {
Expand All @@ -217,28 +238,17 @@ func (tc tairCmdable) TftDelDocField(ctx context.Context, index, docId string, f

func (tc tairCmdable) TftGetDoc(ctx context.Context, index, docId string) *redis.StringCmd {
a := make([]interface{}, 3)
a[0] = "tft.getdoc"
a[1] = index
a[2] = docId
cmd := redis.NewStringCmd(ctx, a...)
_ = tc(ctx, cmd)
return cmd
}

func (tc tairCmdable) TftGetDocWithFilter(ctx context.Context, index, docId, request string) *redis.StringCmd {
a := make([]interface{}, 2)
a[0] = "tft.getdoc"
a[0] = "TFT.GETDOC"
a[1] = index
a[2] = docId
a[3] = request
cmd := redis.NewStringCmd(ctx, a...)
_ = tc(ctx, cmd)
return cmd
}

func (tc tairCmdable) TftDelDoc(ctx context.Context, index string, docId ...string) *redis.StringCmd {
a := make([]interface{}, 2)
a[0] = "tft.deldoc"
a[0] = "TFT.DELDOC"
a[1] = index
for _, d := range docId {
a = append(a, d)
Expand All @@ -248,21 +258,18 @@ func (tc tairCmdable) TftDelDoc(ctx context.Context, index string, docId ...stri
return cmd
}

func (tc tairCmdable) TftDelAll(ctx context.Context, index string, docId ...string) *redis.StringCmd {
func (tc tairCmdable) TftDelAll(ctx context.Context, index string) *redis.StringCmd {
a := make([]interface{}, 2)
a[0] = "tft.delall"
a[0] = "TFT.DELALL"
a[1] = index
for _, d := range docId {
a = append(a, d)
}
cmd := redis.NewStringCmd(ctx, a...)
_ = tc(ctx, cmd)
return cmd
}

func (tc tairCmdable) TftSearch(ctx context.Context, index string, request string) *redis.StringCmd {
a := make([]interface{}, 3)
a[0] = "tft.search"
a[0] = "TFT.SEARCH"
a[1] = index
a[2] = request
cmd := redis.NewStringCmd(ctx, a...)
Expand All @@ -272,21 +279,33 @@ func (tc tairCmdable) TftSearch(ctx context.Context, index string, request strin

func (tc tairCmdable) TftSearchUseCache(ctx context.Context, index string, request string, useCache bool) *redis.StringCmd {
a := make([]interface{}, 3)
a[0] = "tft.search"
a[0] = "TFT.SEARCH"
a[1] = index
a[2] = request
if useCache {
a[3] = []byte(`use_cache`)
a = append(a, "USE_CACHE")
}
cmd := redis.NewStringCmd(ctx, a...)
_ = tc(ctx, cmd)
return cmd
}

func (tc tairCmdable) TftMSearch(ctx context.Context, indexCount int64, request string, index ...string) *redis.StringCmd {
a := make([]interface{}, 2)
a[0] = "TFT.MSEARCH"
a[1] = indexCount
for _, d := range index {
a = append(a, d)
}
a = append(a, request)
cmd := redis.NewStringCmd(ctx, a...)
_ = tc(ctx, cmd)
return cmd
}

func (tc tairCmdable) TftExists(ctx context.Context, index string, docId string) *redis.IntCmd {
a := make([]interface{}, 3)
a[0] = "tft.exists"
a[0] = "TFT.EXISTS"
a[1] = index
a[2] = docId
cmd := redis.NewIntCmd(ctx, a...)
Expand All @@ -296,7 +315,7 @@ func (tc tairCmdable) TftExists(ctx context.Context, index string, docId string)

func (tc tairCmdable) TftDocNum(ctx context.Context, index string) *redis.IntCmd {
a := make([]interface{}, 2)
a[0] = "tft.docnum"
a[0] = "TFT.DOCNUM"
a[1] = index
cmd := redis.NewIntCmd(ctx, a...)
_ = tc(ctx, cmd)
Expand All @@ -305,7 +324,7 @@ func (tc tairCmdable) TftDocNum(ctx context.Context, index string) *redis.IntCmd

func (tc tairCmdable) TftScanDocId(ctx context.Context, index string, cursor string) *redis.SliceCmd {
a := make([]interface{}, 3)
a[0] = "tft.scandocid"
a[0] = "TFT.SCANDOCID"
a[1] = index
a[2] = cursor
cmd := redis.NewSliceCmd(ctx, a...)
Expand All @@ -315,7 +334,7 @@ func (tc tairCmdable) TftScanDocId(ctx context.Context, index string, cursor str

func (tc tairCmdable) TftScanDocIdArgs(ctx context.Context, index string, cursor string, a *TftScanArgs) *redis.SliceCmd {
args := make([]interface{}, 3)
args[0] = "tft.scandocid"
args[0] = "TFT.SCANDOCID"
args[1] = index
args[2] = cursor
args = append(args, a.GetArgs()...)
Expand Down
39 changes: 36 additions & 3 deletions tair/tairsearch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ func (suite *TairSearchTestSuite) TestTftUpdateIndex() {
result3, err3 := suite.tairClient.TftGetIndexMappings(ctx, "tftkey").Result()
assert.NoError(suite.T(), err3)
assert.Equal(suite.T(), result3, "{\"tftkey\":{\"mappings\":{\"_source\":{\"enabled\":true,\"excludes\":[],\"includes\":[]},\"dynamic\":\"false\",\"properties\":{\"f0\":{\"boost\":1.0,\"enabled\":true,\"ignore_above\":-1,\"index\":true,\"similarity\":\"classic\",\"type\":\"text\"},\"f1\":{\"boost\":1.0,\"enabled\":true,\"ignore_above\":-1,\"index\":true,\"similarity\":\"classic\",\"type\":\"text\"}}}}}")

result4, err4 := suite.tairClient.TftGetIndex(ctx, "tftkey").Result()
assert.NoError(suite.T(), err4)
assert.Equal(suite.T(), result4, "{\"tftkey\":{\"mappings\":{\"_source\":{\"enabled\":true,\"excludes\":[],\"includes\":[]},\"dynamic\":\"false\",\"properties\":{\"f0\":{\"boost\":1.0,\"enabled\":true,\"ignore_above\":-1,\"index\":true,\"similarity\":\"classic\",\"type\":\"text\"},\"f1\":{\"boost\":1.0,\"enabled\":true,\"ignore_above\":-1,\"index\":true,\"similarity\":\"classic\",\"type\":\"text\"}}},\"settings\":{}}}")

result5, err5 := suite.tairClient.TftGetIndexSettings(ctx, "tftkey").Result()
assert.NoError(suite.T(), err5)
assert.Equal(suite.T(), result5, "{\"tftkey\":{\"settings\":{}}}")

result6, err6 := suite.tairClient.TftGetIndexMappings(ctx, "tftkey").Result()
assert.NoError(suite.T(), err6)
assert.Equal(suite.T(), result6, "{\"tftkey\":{\"mappings\":{\"_source\":{\"enabled\":true,\"excludes\":[],\"includes\":[]},\"dynamic\":\"false\",\"properties\":{\"f0\":{\"boost\":1.0,\"enabled\":true,\"ignore_above\":-1,\"index\":true,\"similarity\":\"classic\",\"type\":\"text\"},\"f1\":{\"boost\":1.0,\"enabled\":true,\"ignore_above\":-1,\"index\":true,\"similarity\":\"classic\",\"type\":\"text\"}}}}}")

}

func (suite *TairSearchTestSuite) TestTftAddDoc() {
Expand All @@ -59,6 +72,10 @@ func (suite *TairSearchTestSuite) TestTftAddDoc() {
assert.NoError(suite.T(), err1)
assert.Equal(suite.T(), result1, "{\"hits\":{\"hits\":[{\"_id\":\"1\",\"_index\":\"tftkey\",\"_score\":1.223144,\"_source\":{\"f0\":\"v0\",\"f1\":\"3\"}},{\"_id\":\"2\",\"_index\":\"tftkey\",\"_score\":1.223144,\"_source\":{\"f0\":\"v1\",\"f1\":\"3\"}},{\"_id\":\"3\",\"_index\":\"tftkey\",\"_score\":1.223144,\"_source\":{\"f0\":\"v3\",\"f1\":\"3\"}}],\"max_score\":1.223144,\"total\":{\"relation\":\"eq\",\"value\":3}}}")

result, err := suite.tairClient.TftSearchUseCache(ctx, "tftkey", "{\"query\":{\"match\":{\"f1\":\"3\"}}}", true).Result()
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), result, "{\"hits\":{\"hits\":[{\"_id\":\"1\",\"_index\":\"tftkey\",\"_score\":1.223144,\"_source\":{\"f0\":\"v0\",\"f1\":\"3\"}},{\"_id\":\"2\",\"_index\":\"tftkey\",\"_score\":1.223144,\"_source\":{\"f0\":\"v1\",\"f1\":\"3\"}},{\"_id\":\"3\",\"_index\":\"tftkey\",\"_score\":1.223144,\"_source\":{\"f0\":\"v3\",\"f1\":\"3\"}}],\"max_score\":1.223144,\"total\":{\"relation\":\"eq\",\"value\":3}}}")

result2, err2 := suite.tairClient.TftGetDoc(ctx, "tftkey", "3").Result()
assert.NoError(suite.T(), err2)
assert.Equal(suite.T(), result2, "{\"_id\":\"3\",\"_source\":{\"f0\":\"v3\",\"f1\":\"3\"}}")
Expand All @@ -77,6 +94,22 @@ func (suite *TairSearchTestSuite) TestTftAddDoc() {

}

func (suite *TairSearchTestSuite) TestTftMSearch() {
suite.tairClient.TftCreateIndex(ctx, "tftkey1", "{\"mappings\":{\"dynamic\":\"false\",\"properties\":{\"f0\":{\"type\":\"text\"},\"f1\":{\"type\":\"text\"}}}}")
suite.tairClient.TftCreateIndex(ctx, "tftkey2", "{\"mappings\":{\"dynamic\":\"false\",\"properties\":{\"f0\":{\"type\":\"text\"},\"f1\":{\"type\":\"text\"}}}}")
suite.tairClient.TftCreateIndex(ctx, "tftkey3", "{\"mappings\":{\"dynamic\":\"false\",\"properties\":{\"f0\":{\"type\":\"text\"},\"f1\":{\"type\":\"text\"}}}}")
suite.tairClient.TftAddDocWithId(ctx, "tftkey1", "{\"f0\":\"v0\",\"f1\":\"3\"}", "1")
suite.tairClient.TftAddDocWithId(ctx, "tftkey2", "{\"f0\":\"v1\",\"f1\":\"3\"}", "2")
suite.tairClient.TftAddDocWithId(ctx, "tftkey3", "{\"f0\":\"v3\",\"f1\":\"3\"}", "3")
suite.tairClient.TftAddDocWithId(ctx, "tftkey1", "{\"f0\":\"v3\",\"f1\":\"4\"}", "4")
suite.tairClient.TftAddDocWithId(ctx, "tftkey2", "{\"f0\":\"v3\",\"f1\":\"5\"}", "5")

result1, err1 := suite.tairClient.TftMSearch(ctx, 3, "{\"query\":{\"match\":{\"f1\":\"3\"}}}", "tftkey1", "tftkey2", "tftkey3").Result()
assert.NoError(suite.T(), err1)
assert.Equal(suite.T(), result1, "{\"hits\":{\"hits\":[{\"_id\":\"1\",\"_index\":\"tftkey1\",\"_score\":1.0,\"_source\":{\"f0\":\"v0\",\"f1\":\"3\"}},{\"_id\":\"2\",\"_index\":\"tftkey2\",\"_score\":1.0,\"_source\":{\"f0\":\"v1\",\"f1\":\"3\"}},{\"_id\":\"3\",\"_index\":\"tftkey3\",\"_score\":0.306853,\"_source\":{\"f0\":\"v3\",\"f1\":\"3\"}}],\"max_score\":1.0,\"total\":{\"relation\":\"eq\",\"value\":3}},\"aux_info\":{\"index_crc64\":52600736426816810}}")

}

func (suite *TairSearchTestSuite) TestTftUpdateDocField() {
result1, err := suite.tairClient.TftCreateIndex(ctx, "tftkey", "{\"mappings\":{\"dynamic\":\"false\",\"properties\":{\"f0\":{\"type\":\"text\"}}}}").Result()
assert.NoError(suite.T(), err)
Expand All @@ -95,7 +128,7 @@ func (suite *TairSearchTestSuite) TestTftUpdateDocField() {
suite.tairClient.TftUpdateDocField(ctx, "tftkey", "1", "{\"f1\":\"mysql is a dbms\"}")
result4, err4 := suite.tairClient.TftSearch(ctx, "tftkey", "{\"query\":{\"term\":{\"f1\":\"mysql\"}}}").Result()
assert.NoError(suite.T(), err4)
assert.Equal(suite.T(), result4, "{\"hits\":{\"hits\":[{\"_id\":\"1\",\"_index\":\"tftkey\",\"_score\":0.191783,\"_source\":{\"f0\":\"redis is a nosql database\",\"f1\":\"mysql is a dbms\"}}],\"max_score\":0.191783,\"total\":{\"relation\":\"eq\",\"value\":1}}}")
assert.Equal(suite.T(), result4, "{\"hits\":{\"hits\":[{\"_id\":\"1\",\"_index\":\"tftkey\",\"_score\":0.191783,\"_source\":{\"f1\":\"mysql is a dbms\",\"f0\":\"redis is a nosql database\"}}],\"max_score\":0.191783,\"total\":{\"relation\":\"eq\",\"value\":1}}}")

}

Expand All @@ -110,7 +143,7 @@ func (suite *TairSearchTestSuite) TestTftIncrLongDocField() {

_, err2 := suite.tairClient.TftIncrLongDocField(ctx, "tftkey", "1", "f0", 1).Result()
assert.Error(suite.T(), err2)
assert.Contains(suite.T(), err2, "failed to parse field")
assert.Contains(suite.T(), err2, "ERR incrlongdocfield only supports field of int or long type")

suite.tairClient.Del(ctx, "tftkey")

Expand Down Expand Up @@ -143,7 +176,7 @@ func (suite *TairSearchTestSuite) TestTftIncrFloatDocField() {

_, err3 := suite.tairClient.TftIncrFloatDocField(ctx, "tftkey", "1", "f0", 1.1).Result()
assert.Error(suite.T(), err3)
assert.Contains(suite.T(), err3, "failed to parse field")
assert.Contains(suite.T(), err3, "ERR incrfloatdocfield only supports field of double type")

suite.tairClient.Del(ctx, "tftkey")

Expand Down

0 comments on commit 549ec30

Please sign in to comment.