Skip to content

Commit 819e782

Browse files
add new test to check FT.SERCH after JSON.SET
1 parent 8c86ddb commit 819e782

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

redisearch/client_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,50 @@ func TestClient_CreateIndex(t *testing.T) {
935935
assert.Equal(t, "John", docs[1].Properties["name"])
936936
}
937937

938+
func TestClient_CreateJsonIndex(t *testing.T) {
939+
c := createClient("create-json-index")
940+
flush(c)
941+
version, err := c.getRediSearchVersion()
942+
assert.Nil(t, err)
943+
if version <= 10699 {
944+
// IndexDefinition is available for RediSearch 2.0+
945+
return
946+
}
947+
// Create a schema
948+
schema := NewSchema(DefaultOptions).
949+
AddField(NewTextFieldOptions("name", TextFieldOptions{Sortable: true, PhoneticMatcher: PhoneticDoubleMetaphoneEnglish})).
950+
AddField(NewNumericField("age"))
951+
// IndexDefinition is available for RediSearch 2.0+
952+
// In this example we will only index keys started by product:
953+
indexDefinition := NewIndexDefinition().SetIndexOn(JSON).AddPrefix("create-json-index:")
954+
// Add the Index Definition
955+
err = c.CreateIndexWithIndexDefinition(schema, indexDefinition)
956+
assert.Nil(t, err)
957+
// Create docs with a name that has the same phonetic matcher
958+
vanillaConnection := c.pool.Get()
959+
_, err = vanillaConnection.Do("JSON.SET", "create-json-index:doc1", ".", "{\"name\":\"Jon\", \"age\": 25}")
960+
assert.Nil(t, err)
961+
_, err = vanillaConnection.Do("JSON.SET", "create-json-index:doc2", ".", "{\"name\":\"John\", \"age\": 25}")
962+
assert.Nil(t, err)
963+
// Wait for all documents to be indexed
964+
info, err := c.Info()
965+
assert.Nil(t, err)
966+
for info.IsIndexing {
967+
time.Sleep(time.Second)
968+
info, _ = c.Info()
969+
}
970+
assert.Equal(t, uint64(2), info.DocCount)
971+
assert.Equal(t, false, info.IsIndexing)
972+
assert.Equal(t, uint64(0), info.HashIndexingFailures)
973+
docs, total, err := c.Search(NewQuery("\"Jon\"").
974+
SetReturnFields("name"))
975+
assert.Nil(t, err)
976+
// Verify that the we've received 2 documents ( Jon and John )
977+
assert.Equal(t, 2, total)
978+
assert.Equal(t, "Jon", docs[0].Properties["name"])
979+
assert.Equal(t, "John", docs[1].Properties["name"])
980+
}
981+
938982
func TestClient_CreateIndex_failure(t *testing.T) {
939983
c := createClient("create-index-failure")
940984
flush(c)

0 commit comments

Comments
 (0)