Skip to content

Commit

Permalink
[add] not running ExampleClient_CreateIndexWithIndexDefinition when R…
Browse files Browse the repository at this point in the history
…ediSearch version is bellow 20000 ( v2.0 )
  • Loading branch information
filipecosta90 committed Aug 9, 2020
1 parent 0d15c28 commit 4b79274
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 33 deletions.
28 changes: 28 additions & 0 deletions redisearch/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,34 @@ func NewClientFromPool(pool *redis.Pool, name string) *Client {
return ret
}

// GetRediSearchVersion returns RediSearch version by issuing "MODULE LIST" command
// and iterating through the availabe modules up until "ft" is found as the name property
func (c *Client) GetRediSearchVersion() (version int64, err error) {
conn := c.pool.Get()
defer conn.Close()
var values []interface{}
var moduleInfo []interface{}
var moduleName string
values, err = redis.Values(conn.Do("MODULE", "LIST"))
if err != nil {
return
}
for _, rawModule := range values {
moduleInfo, err = redis.Values(rawModule, err)
if err != nil {
return
}
moduleName, err = redis.String(moduleInfo[1], err)
if err != nil {
return
}
if moduleName == "ft" {
version, err = redis.Int64(moduleInfo[3], err)
}
}
return
}

// CreateIndex configures the index and creates it on redis
func (i *Client) CreateIndex(schema *Schema) (err error) {
return i.indexWithDefinition(i.name, schema, nil, err)
Expand Down
36 changes: 4 additions & 32 deletions redisearch/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,6 @@ func flush(c *Client) (err error) {
return conn.Send("FLUSHALL")
}

// getRediSearchVersion returns RediSearch version by issuing "MODULE LIST" command
// and iterating through the availabe modules up until "ft" is found as the name property
func (c *Client) getRediSearchVersion() (version int64, err error) {
conn := c.pool.Get()
defer conn.Close()
var values []interface{}
var moduleInfo []interface{}
var moduleName string
values, err = redis.Values(conn.Do("MODULE", "LIST"))
if err != nil {
return
}
for _, rawModule := range values {
moduleInfo, err = redis.Values(rawModule, err)
if err != nil {
return
}
moduleName, err = redis.String(moduleInfo[1], err)
if err != nil {
return
}
if moduleName == "ft" {
version, err = redis.Int64(moduleInfo[3], err)
}
}
return
}

func teardown(c *Client) {
flush(c)
}
Expand Down Expand Up @@ -522,7 +494,7 @@ func TestClient_GetTagVals(t *testing.T) {

func TestClient_SynAdd(t *testing.T) {
c := createClient("testsynadd")
version, err := c.getRediSearchVersion()
version, err := c.GetRediSearchVersion()
assert.Nil(t, err)
if version <= 10699 {
sc := NewSchema(DefaultOptions).
Expand All @@ -544,7 +516,7 @@ func TestClient_SynAdd(t *testing.T) {

func TestClient_SynDump(t *testing.T) {
c := createClient("testsyndump")
version, err := c.getRediSearchVersion()
version, err := c.GetRediSearchVersion()
assert.Nil(t, err)
if version <= 10699 {

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

func TestClient_GetRediSearchVersion(t *testing.T) {
c := createClient("version-test")
_, err := c.getRediSearchVersion()
_, err := c.GetRediSearchVersion()
assert.Nil(t, err)
}

func TestClient_CreateIndexWithIndexDefinition(t *testing.T) {
i := createClient("index-definition-test")
version, err := i.getRediSearchVersion()
version, err := i.GetRediSearchVersion()
assert.Nil(t, err)
if version >= 20000 {

Expand Down
8 changes: 7 additions & 1 deletion redisearch/example_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ func ExampleClient_CreateIndexWithIndexDefinition() {
return redis.Dial("tcp", host, redis.DialPassword(password))
}}
c := redisearch.NewClientFromPool(pool, "products-from-hashes")

version, _ := c.GetRediSearchVersion()

// IndexDefinition is available for RediSearch 2.0+
if version < 20000 {
return
}
// Drop an existing index. If the index does not exist an error is returned
c.Drop()

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

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

// exemplifies the NewClientFromPool function
Expand Down

0 comments on commit 4b79274

Please sign in to comment.