Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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])
}