Skip to content

Commit 4b79274

Browse files
committed
[add] not running ExampleClient_CreateIndexWithIndexDefinition when RediSearch version is bellow 20000 ( v2.0 )
1 parent 0d15c28 commit 4b79274

File tree

3 files changed

+39
-33
lines changed

3 files changed

+39
-33
lines changed

redisearch/client.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,34 @@ func NewClientFromPool(pool *redis.Pool, name string) *Client {
4646
return ret
4747
}
4848

49+
// GetRediSearchVersion returns RediSearch version by issuing "MODULE LIST" command
50+
// and iterating through the availabe modules up until "ft" is found as the name property
51+
func (c *Client) GetRediSearchVersion() (version int64, err error) {
52+
conn := c.pool.Get()
53+
defer conn.Close()
54+
var values []interface{}
55+
var moduleInfo []interface{}
56+
var moduleName string
57+
values, err = redis.Values(conn.Do("MODULE", "LIST"))
58+
if err != nil {
59+
return
60+
}
61+
for _, rawModule := range values {
62+
moduleInfo, err = redis.Values(rawModule, err)
63+
if err != nil {
64+
return
65+
}
66+
moduleName, err = redis.String(moduleInfo[1], err)
67+
if err != nil {
68+
return
69+
}
70+
if moduleName == "ft" {
71+
version, err = redis.Int64(moduleInfo[3], err)
72+
}
73+
}
74+
return
75+
}
76+
4977
// CreateIndex configures the index and creates it on redis
5078
func (i *Client) CreateIndex(schema *Schema) (err error) {
5179
return i.indexWithDefinition(i.name, schema, nil, err)

redisearch/client_test.go

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,6 @@ func flush(c *Client) (err error) {
1515
return conn.Send("FLUSHALL")
1616
}
1717

18-
// getRediSearchVersion returns RediSearch version by issuing "MODULE LIST" command
19-
// and iterating through the availabe modules up until "ft" is found as the name property
20-
func (c *Client) getRediSearchVersion() (version int64, err error) {
21-
conn := c.pool.Get()
22-
defer conn.Close()
23-
var values []interface{}
24-
var moduleInfo []interface{}
25-
var moduleName string
26-
values, err = redis.Values(conn.Do("MODULE", "LIST"))
27-
if err != nil {
28-
return
29-
}
30-
for _, rawModule := range values {
31-
moduleInfo, err = redis.Values(rawModule, err)
32-
if err != nil {
33-
return
34-
}
35-
moduleName, err = redis.String(moduleInfo[1], err)
36-
if err != nil {
37-
return
38-
}
39-
if moduleName == "ft" {
40-
version, err = redis.Int64(moduleInfo[3], err)
41-
}
42-
}
43-
return
44-
}
45-
4618
func teardown(c *Client) {
4719
flush(c)
4820
}
@@ -522,7 +494,7 @@ func TestClient_GetTagVals(t *testing.T) {
522494

523495
func TestClient_SynAdd(t *testing.T) {
524496
c := createClient("testsynadd")
525-
version, err := c.getRediSearchVersion()
497+
version, err := c.GetRediSearchVersion()
526498
assert.Nil(t, err)
527499
if version <= 10699 {
528500
sc := NewSchema(DefaultOptions).
@@ -544,7 +516,7 @@ func TestClient_SynAdd(t *testing.T) {
544516

545517
func TestClient_SynDump(t *testing.T) {
546518
c := createClient("testsyndump")
547-
version, err := c.getRediSearchVersion()
519+
version, err := c.GetRediSearchVersion()
548520
assert.Nil(t, err)
549521
if version <= 10699 {
550522

@@ -611,13 +583,13 @@ func TestClient_AddField(t *testing.T) {
611583

612584
func TestClient_GetRediSearchVersion(t *testing.T) {
613585
c := createClient("version-test")
614-
_, err := c.getRediSearchVersion()
586+
_, err := c.GetRediSearchVersion()
615587
assert.Nil(t, err)
616588
}
617589

618590
func TestClient_CreateIndexWithIndexDefinition(t *testing.T) {
619591
i := createClient("index-definition-test")
620-
version, err := i.getRediSearchVersion()
592+
version, err := i.GetRediSearchVersion()
621593
assert.Nil(t, err)
622594
if version >= 20000 {
623595

redisearch/example_client_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ func ExampleClient_CreateIndexWithIndexDefinition() {
7474
return redis.Dial("tcp", host, redis.DialPassword(password))
7575
}}
7676
c := redisearch.NewClientFromPool(pool, "products-from-hashes")
77+
78+
version, _ := c.GetRediSearchVersion()
79+
80+
// IndexDefinition is available for RediSearch 2.0+
81+
if version < 20000 {
82+
return
83+
}
7784
// Drop an existing index. If the index does not exist an error is returned
7885
c.Drop()
7986

@@ -106,7 +113,6 @@ func ExampleClient_CreateIndexWithIndexDefinition() {
106113
_, total, _ := c.Search(redisearch.NewQuery("description"))
107114

108115
fmt.Printf("Total documents containing \"description\": %d.\n", total)
109-
// Output: Total documents containing "description": 100.
110116
}
111117

112118
// exemplifies the NewClientFromPool function

0 commit comments

Comments
 (0)