diff --git a/redisearch/client.go b/redisearch/client.go index 645e20f..da19004 100644 --- a/redisearch/client.go +++ b/redisearch/client.go @@ -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) diff --git a/redisearch/client_test.go b/redisearch/client_test.go index bbf6d72..88c49bb 100644 --- a/redisearch/client_test.go +++ b/redisearch/client_test.go @@ -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) } @@ -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). @@ -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 { @@ -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 { diff --git a/redisearch/example_client_test.go b/redisearch/example_client_test.go index dd93ea8..1d0949c 100644 --- a/redisearch/example_client_test.go +++ b/redisearch/example_client_test.go @@ -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() @@ -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