Skip to content

Commit c13ac87

Browse files
merge
2 parents 65badb8 + e0864a7 commit c13ac87

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

redisearch/client_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,56 @@ func TestClient_CreateIndex(t *testing.T) {
926926
time.Sleep(time.Second)
927927
info, _ = c.Info()
928928
}
929+
930+
assert.Equal(t, uint64(2), info.DocCount)
931+
assert.Equal(t, false, info.IsIndexing)
932+
assert.Equal(t, uint64(0), info.HashIndexingFailures)
933+
934+
docs, total, err := c.Search(NewQuery("Jon").SetReturnFields("name"))
935+
assert.Nil(t, err)
936+
// Verify that the we've received 2 documents ( Jon and John )
937+
assert.Equal(t, 2, total)
938+
assert.Equal(t, "Jon", docs[0].Properties["name"])
939+
assert.Equal(t, "John", docs[1].Properties["name"])
940+
}
941+
942+
func TestClient_CreateJsonIndex(t *testing.T) {
943+
c := createClient("create-json-index")
944+
flush(c)
945+
version, _ := c.getRediSearchVersion()
946+
if version < 20200 {
947+
// IndexDefinition is available for RediSearch 2.0+
948+
return
949+
}
950+
951+
// Create a schema
952+
schema := NewSchema(DefaultOptions).
953+
AddField(NewTextFieldOptions("$.name", TextFieldOptions{Sortable: true, PhoneticMatcher: PhoneticDoubleMetaphoneEnglish, As: "name"})).
954+
AddField(NewNumericFieldOptions("$.age", NumericFieldOptions{As: "age"}))
955+
956+
// IndexDefinition is available for RediSearch 2.0+
957+
// In this example we will only index keys started by product:
958+
indexDefinition := NewIndexDefinition().SetIndexOn(JSON).AddPrefix("create-json-index:")
959+
960+
// Add the Index Definition
961+
err := c.CreateIndexWithIndexDefinition(schema, indexDefinition)
962+
assert.Nil(t, err)
963+
964+
// Create docs with a name that has the same phonetic matcher
965+
vanillaConnection := c.pool.Get()
966+
_, err = vanillaConnection.Do("JSON.SET", "create-json-index:doc1", "$", "{\"name\":\"Jon\", \"age\": 25}")
967+
assert.Nil(t, err)
968+
_, err = vanillaConnection.Do("JSON.SET", "create-json-index:doc2", "$", "{\"name\":\"John\", \"age\": 25}")
969+
assert.Nil(t, err)
970+
971+
// Wait for all documents to be indexed
972+
info, err := c.Info()
973+
assert.Nil(t, err)
974+
for info.IsIndexing {
975+
time.Sleep(time.Second)
976+
info, _ = c.Info()
977+
}
978+
929979
assert.Equal(t, uint64(2), info.DocCount)
930980
assert.Equal(t, false, info.IsIndexing)
931981
assert.Equal(t, uint64(0), info.HashIndexingFailures)

0 commit comments

Comments
 (0)