Skip to content

Commit

Permalink
Merge branch 'master' into redisearch-go
Browse files Browse the repository at this point in the history
  • Loading branch information
filipecosta90 authored Apr 20, 2021
2 parents ae18538 + f79df23 commit d2e39e9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
24 changes: 23 additions & 1 deletion redisearch/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package redisearch

import (
"errors"
"github.com/gomodule/redigo/redis"
"log"
"reflect"
"strconv"
"strings"

"github.com/gomodule/redigo/redis"
)

// Client is an interface to redisearch's redis commands
Expand Down Expand Up @@ -665,3 +666,24 @@ func (i *Client) AddHash(docId string, score float32, language string, replace b
}
return redis.String(conn.Do("FT.ADDHASH", args...))
}

// Returns a list of all existing indexes.
func (i *Client) List() ([]string, error) {
conn := i.pool.Get()
defer conn.Close()

res, err := redis.Values(conn.Do("FT._LIST"))
if err != nil {
return nil, err
}

var indexes []string

// Iterate over the values
for ii := 0; ii < len(res); ii += 1 {
key, _ := redis.String(res[ii], nil)
indexes = append(indexes, key)
}

return indexes, nil
}
26 changes: 26 additions & 0 deletions redisearch/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -965,3 +965,29 @@ func TestClient_DropIndex(t *testing.T) {
assert.Equal(t, int64(0), result)

}

func TestClient_ListIndex(t *testing.T) {
c := createClient("index-list-test")
version, err := c.getRediSearchVersion()
assert.Nil(t, err)
if version <= 10699 {
// IndexDefinition is available for RediSearch 2.0+
return
}
// Create a schema
schema := NewSchema(DefaultOptions).
AddField(NewTextFieldOptions("name", TextFieldOptions{Sortable: true, PhoneticMatcher: PhoneticDoubleMetaphoneEnglish})).
AddField(NewNumericField("age"))

// IndexDefinition is available for RediSearch 2.0+
// In this example we will only index keys started by product:
indexDefinition := NewIndexDefinition().AddPrefix("index-list-test:")

// Add the Index Definition
c.CreateIndexWithIndexDefinition(schema, indexDefinition)
assert.Nil(t, err)

indexes, err := c.List()
assert.Nil(t, err)
assert.Equal(t, "index-list-test", indexes[0])
}

0 comments on commit d2e39e9

Please sign in to comment.