Skip to content

Commit d2e39e9

Browse files
Merge branch 'master' into redisearch-go
2 parents ae18538 + f79df23 commit d2e39e9

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

redisearch/client.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package redisearch
22

33
import (
44
"errors"
5-
"github.com/gomodule/redigo/redis"
65
"log"
76
"reflect"
87
"strconv"
98
"strings"
9+
10+
"github.com/gomodule/redigo/redis"
1011
)
1112

1213
// Client is an interface to redisearch's redis commands
@@ -665,3 +666,24 @@ func (i *Client) AddHash(docId string, score float32, language string, replace b
665666
}
666667
return redis.String(conn.Do("FT.ADDHASH", args...))
667668
}
669+
670+
// Returns a list of all existing indexes.
671+
func (i *Client) List() ([]string, error) {
672+
conn := i.pool.Get()
673+
defer conn.Close()
674+
675+
res, err := redis.Values(conn.Do("FT._LIST"))
676+
if err != nil {
677+
return nil, err
678+
}
679+
680+
var indexes []string
681+
682+
// Iterate over the values
683+
for ii := 0; ii < len(res); ii += 1 {
684+
key, _ := redis.String(res[ii], nil)
685+
indexes = append(indexes, key)
686+
}
687+
688+
return indexes, nil
689+
}

redisearch/client_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,3 +965,29 @@ func TestClient_DropIndex(t *testing.T) {
965965
assert.Equal(t, int64(0), result)
966966

967967
}
968+
969+
func TestClient_ListIndex(t *testing.T) {
970+
c := createClient("index-list-test")
971+
version, err := c.getRediSearchVersion()
972+
assert.Nil(t, err)
973+
if version <= 10699 {
974+
// IndexDefinition is available for RediSearch 2.0+
975+
return
976+
}
977+
// Create a schema
978+
schema := NewSchema(DefaultOptions).
979+
AddField(NewTextFieldOptions("name", TextFieldOptions{Sortable: true, PhoneticMatcher: PhoneticDoubleMetaphoneEnglish})).
980+
AddField(NewNumericField("age"))
981+
982+
// IndexDefinition is available for RediSearch 2.0+
983+
// In this example we will only index keys started by product:
984+
indexDefinition := NewIndexDefinition().AddPrefix("index-list-test:")
985+
986+
// Add the Index Definition
987+
c.CreateIndexWithIndexDefinition(schema, indexDefinition)
988+
assert.Nil(t, err)
989+
990+
indexes, err := c.List()
991+
assert.Nil(t, err)
992+
assert.Equal(t, "index-list-test", indexes[0])
993+
}

0 commit comments

Comments
 (0)