From 9817666eed8f7053a86340b9b6938e9783104547 Mon Sep 17 00:00:00 2001 From: dengliming Date: Sat, 4 Apr 2020 23:00:47 +0800 Subject: [PATCH] Support FT.CONFIG command --- redisearch/client.go | 37 ++++++++++++++++++++++++++++++++++--- redisearch/client_test.go | 29 ++++++++++++++++++++++------- 2 files changed, 56 insertions(+), 10 deletions(-) diff --git a/redisearch/client.go b/redisearch/client.go index 5f774b2..8caa664 100644 --- a/redisearch/client.go +++ b/redisearch/client.go @@ -228,8 +228,8 @@ func (i *Client) Aggregate(q *AggregateQuery) (aggregateReply [][]string, total } // has no cursor if !hasCursor { - total, aggregateReply,err = processAggReply(res) - // has cursor + total, aggregateReply, err = processAggReply(res) + // has cursor } else { var partialResults, err = redis.Values(res[0], nil) if err != nil { @@ -239,7 +239,7 @@ func (i *Client) Aggregate(q *AggregateQuery) (aggregateReply [][]string, total if err != nil { return aggregateReply, total, err } - total, aggregateReply,err = processAggReply(partialResults) + total, aggregateReply, err = processAggReply(partialResults) } return @@ -490,3 +490,34 @@ func (i *Client) Info() (*IndexInfo, error) { return &ret, nil } + +// Set runtime configuration option +func (i *Client) SetConfig(option string, value string) (string, error) { + conn := i.pool.Get() + defer conn.Close() + + args := redis.Args{"SET", option, value} + return redis.String(conn.Do("FT.CONFIG", args...)) +} + +// Get runtime configuration option value +func (i *Client) GetConfig(option string) (map[string]string, error) { + conn := i.pool.Get() + defer conn.Close() + + args := redis.Args{"GET", option} + values, err := redis.Values(conn.Do("FT.CONFIG", args...)) + if err != nil { + return nil, err + } + + m := make(map[string]string) + valLen := len(values) + for i := 0; i < valLen; i++ { + kvs, _ := redis.Strings(values[i], nil) + if kvs != nil && len(kvs) == 2 { + m[kvs[0]] = kvs[1] + } + } + return m, nil +} diff --git a/redisearch/client_test.go b/redisearch/client_test.go index 384e15d..99d5bab 100644 --- a/redisearch/client_test.go +++ b/redisearch/client_test.go @@ -214,10 +214,10 @@ func TestClient_DictAdd(t *testing.T) { wantNewTerms int wantErr bool }{ - {"empty-error", fields{pool: c.pool, name: c.name}, args{"dict1", []string{},}, 0, true}, - {"1-term", fields{pool: c.pool, name: c.name}, args{"dict1", []string{"term1"},}, 1, false}, - {"2nd-time-term", fields{pool: c.pool, name: c.name}, args{"dict1", []string{"term1"},}, 0, false}, - {"multi-term", fields{pool: c.pool, name: c.name}, args{"dict1", []string{"t1", "t2", "t3", "t4", "t5"},}, 5, false}, + {"empty-error", fields{pool: c.pool, name: c.name}, args{"dict1", []string{}}, 0, true}, + {"1-term", fields{pool: c.pool, name: c.name}, args{"dict1", []string{"term1"}}, 1, false}, + {"2nd-time-term", fields{pool: c.pool, name: c.name}, args{"dict1", []string{"term1"}}, 0, false}, + {"multi-term", fields{pool: c.pool, name: c.name}, args{"dict1", []string{"t1", "t2", "t3", "t4", "t5"}}, 5, false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -265,9 +265,9 @@ func TestClient_DictDel(t *testing.T) { wantDeletedTerms int wantErr bool }{ - {"empty-error", fields{pool: c.pool, name: c.name}, args{"dict1", []string{},}, 0, true}, - {"1-term", fields{pool: c.pool, name: c.name}, args{"dict1", []string{"term1"},}, 1, false}, - {"2nd-time-term", fields{pool: c.pool, name: c.name}, args{"dict1", []string{"term1"},}, 0, false}, + {"empty-error", fields{pool: c.pool, name: c.name}, args{"dict1", []string{}}, 0, true}, + {"1-term", fields{pool: c.pool, name: c.name}, args{"dict1", []string{"term1"}}, 1, false}, + {"2nd-time-term", fields{pool: c.pool, name: c.name}, args{"dict1", []string{"term1"}}, 0, false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -476,3 +476,18 @@ func TestClient_AliasUpdate(t *testing.T) { }) } } + +func TestClient_Config(t *testing.T) { + c := createClient("testconfigindex") + + ret, err := c.SetConfig("TIMEOUT", "100") + assert.Nil(t, err) + assert.Equal(t, "OK", ret) + + var kvs map[string]string + kvs, _ = c.GetConfig("TIMEOUT") + assert.Equal(t, "100", kvs["TIMEOUT"]) + + kvs, _ = c.GetConfig("*") + assert.Equal(t, "100", kvs["TIMEOUT"]) +}