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
63 changes: 58 additions & 5 deletions redisearch/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,8 @@ func TestClient_CreateIndexWithIndexDefinition1(t *testing.T) {
}

func TestClient_CreateIndex(t *testing.T) {
c := createClient("create-index-phonetic")
c := createClient("create-index-info")
flush(c)
version, err := c.getRediSearchVersion()
assert.Nil(t, err)
if version <= 10699 {
Expand All @@ -868,24 +869,26 @@ func TestClient_CreateIndex(t *testing.T) {

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

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

// Create docs with a name that has the same phonetic matcher
vanillaConnection := c.pool.Get()
vanillaConnection.Do("HSET", "create-index-phonetic:doc1", "name", "Jon", "age", 25)
vanillaConnection.Do("HSET", "create-index-phonetic:doc2", "name", "John", "age", 20)
vanillaConnection.Do("HSET", "create-index-info:doc1", "name", "Jon", "age", 25)
vanillaConnection.Do("HSET", "create-index-info:doc2", "name", "John", "age", 20)

// Wait for all documents to be indexed
info, _ := c.Info()
for info.IsIndexing {
time.Sleep(time.Second)
info, _ = c.Info()
}

assert.Equal(t, uint64(2), info.DocCount)
assert.Equal(t, false, info.IsIndexing)
assert.Equal(t, uint64(0), info.HashIndexingFailures)
docs, total, err := c.Search(NewQuery("Jon").
SetReturnFields("name"))
assert.Nil(t, err)
Expand All @@ -895,6 +898,56 @@ func TestClient_CreateIndex(t *testing.T) {
assert.Equal(t, "John", docs[1].Properties["name"])
}

func TestClient_CreateIndex_failure(t *testing.T) {
c := createClient("create-index-failure")
flush(c)
version, err := c.getRediSearchVersion()
assert.Nil(t, err)
if version <= 10699 {
// IndexDefinition is available for RediSearch 2.0+
return
}
c.DropIndex(true)

// Create a schema
schema := NewSchema(DefaultOptions).
AddField(NewTextFieldOptions("name", TextFieldOptions{Sortable: true, PhoneticMatcher: PhoneticDoubleMetaphoneEnglish})).
AddField(NewNumericFieldOptions("age", NumericFieldOptions{Sortable: true}))

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

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

// Create docs with a name that has the same phonetic matcher
vanillaConnection := c.pool.Get()
vanillaConnection.Do("HSET", "create-index-failure:doc1", "name", "Jon", "age", "abc")
vanillaConnection.Do("HSET", "create-index-failure:doc2", "name", "John", "age", 20)

// Wait for all documents to be indexed
info, _ := c.Info()
for info.IsIndexing {
time.Sleep(time.Second)
info, _ = c.Info()
}
assert.Equal(t, uint64(1), info.DocCount)
assert.Equal(t, false, info.IsIndexing)
assert.Equal(t, uint64(1), info.HashIndexingFailures)
docs, total, err := c.Search(NewQuery("Jon").
SetReturnFields("name"))
assert.Nil(t, err)
// Verify that the we've received 1 document ( John )
assert.Equal(t, 1, total)
assert.Equal(t, "John", docs[0].Properties["name"])

// Drop index but keep docs
err = c.DropIndex(true)
assert.Nil(t, err)
}

func TestClient_DropIndex(t *testing.T) {
c := createClient("drop-index-example")
version, err := c.getRediSearchVersion()
Expand Down
1 change: 1 addition & 0 deletions redisearch/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type IndexInfo struct {
OffsetBitsPerTermAvg float64 `redis:"offset_bits_per_record_avg"`
IsIndexing bool `redis:"indexing"`
PercentIndexed float64 `redis:"percent_indexed"`
HashIndexingFailures uint64 `redis:"hash_indexing_failures"`
}

// IndexDefinition is used to define a index definition for automatic indexing on Hash update
Expand Down