Skip to content

Commit 7c30381

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

File tree

1 file changed

+56
-3
lines changed

1 file changed

+56
-3
lines changed

redisearch/client_test.go

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -914,11 +914,14 @@ func TestClient_CreateIndex(t *testing.T) {
914914

915915
// Create docs with a name that has the same phonetic matcher
916916
vanillaConnection := c.pool.Get()
917-
vanillaConnection.Do("HSET", "create-index-info:doc1", "name", "Jon", "age", 25)
918-
vanillaConnection.Do("HSET", "create-index-info:doc2", "name", "John", "age", 20)
917+
_, err = vanillaConnection.Do("HSET", "create-index-info:doc1", "name", "Jon", "age", 25)
918+
assert.Nil(t, err)
919+
_, err = vanillaConnection.Do("HSET", "create-index-info:doc2", "name", "John", "age", 20)
920+
assert.Nil(t, err)
919921

920922
// Wait for all documents to be indexed
921-
info, _ := c.Info()
923+
info, err := c.Info()
924+
assert.Nil(t, err)
922925
for info.IsIndexing {
923926
time.Sleep(time.Second)
924927
info, _ = c.Info()
@@ -935,6 +938,56 @@ func TestClient_CreateIndex(t *testing.T) {
935938
assert.Equal(t, "John", docs[1].Properties["name"])
936939
}
937940

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

0 commit comments

Comments
 (0)