From 93a4ada3967748f59e6643978eeea700ad677c0f Mon Sep 17 00:00:00 2001 From: "lvyanqi.lyq" Date: Mon, 10 Apr 2023 10:23:04 +0800 Subject: [PATCH] TairSearch: add tft.explaincost --- tair/taircommands.go | 1 + tair/tairsearch.go | 10 ++++++++++ tair/tairsearch_test.go | 7 +++++++ 3 files changed, 18 insertions(+) diff --git a/tair/taircommands.go b/tair/taircommands.go index 829cd39..0938807 100644 --- a/tair/taircommands.go +++ b/tair/taircommands.go @@ -90,6 +90,7 @@ type TairCmdable interface { TftScanDocIdArgs(ctx context.Context, index string, cursor string, a *TftScanArgs) *redis.SliceCmd TftAnalyzer(ctx context.Context, analyzerName string, text string) *redis.StringCmd TftAnalyzerWithArgs(ctx context.Context, analyzerName string, text string, a *TftAnalyzerArgs) *redis.StringCmd + TftExplaincost(ctx context.Context, index string, request string) *redis.StringCmd TftAddSug(ctx context.Context, index string, textWeight map[string]int64) *redis.IntCmd TftDelSug(ctx context.Context, index string, text ...string) *redis.IntCmd TftSugSum(ctx context.Context, index string) *redis.IntCmd diff --git a/tair/tairsearch.go b/tair/tairsearch.go index 115a817..c7471f8 100644 --- a/tair/tairsearch.go +++ b/tair/tairsearch.go @@ -410,6 +410,16 @@ func (tc tairCmdable) TftAnalyzerWithArgs(ctx context.Context, analyzerName stri return cmd } +func (tc tairCmdable) TftExplaincost(ctx context.Context, index string, request string) *redis.StringCmd { + a := make([]interface{}, 3) + a[0] = "TFT.EXPLAINCOST" + a[1] = index + a[2] = request + cmd := redis.NewStringCmd(ctx, a...) + _ = tc(ctx, cmd) + return cmd +} + func (tc tairCmdable) TftAddSug(ctx context.Context, index string, textWeight map[string]int64) *redis.IntCmd { args := make([]interface{}, 2) args[0] = "TFT.ADDSUG" diff --git a/tair/tairsearch_test.go b/tair/tairsearch_test.go index ca6dadb..fabe9e1 100644 --- a/tair/tairsearch_test.go +++ b/tair/tairsearch_test.go @@ -1,6 +1,7 @@ package tair_test import ( + "encoding/json" "github.com/alibaba/tair-go/tair" "github.com/go-redis/redis/v8" "github.com/stretchr/testify/assert" @@ -92,6 +93,12 @@ func (suite *TairSearchTestSuite) TestTftAddDoc() { 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\"}}}}}") + result5, err5 := suite.tairClient.TftExplaincost(ctx, "tftkey", "{\"query\":{\"match\":{\"f1\":\"3\"}}}").Result() + assert.NoError(suite.T(), err5) + var results map[string]interface{} + json.Unmarshal([]byte(result5), &results) + _, ok := results["QUERY_COST"] + assert.Equal(suite.T(), ok, true) } func (suite *TairSearchTestSuite) TestTftMSearch() {