Skip to content

Commit 3374a1b

Browse files
authored
Fix issue #133 types not being recogized properly (#134)
* Fix issue #133 types not being recogized properly * turns out that the options were not being picked up at all - added tests for all field type options * remove duplicat assignement
1 parent 8fe4935 commit 3374a1b

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

redisearch/client.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,17 +499,25 @@ func (info *IndexInfo) loadSchema(values []interface{}, options []string) {
499499

500500
f := Field{Name: spec[0]}
501501
switch strings.ToUpper(spec[2]) {
502+
case "TAG":
503+
f.Type = TagField
504+
tfOptions := TagFieldOptions{}
505+
if wIdx := sliceIndex(options, "SEPARATOR"); wIdx != -1 {
506+
tfOptions.Separator = options[wIdx+1][0]
507+
}
508+
f.Options = tfOptions
509+
case "GEO":
510+
f.Type = GeoField
502511
case "NUMERIC":
503512
f.Type = NumericField
504513
nfOptions := NumericFieldOptions{}
505-
f.Options = nfOptions
506514
if sliceIndex(options, "SORTABLE") != -1 {
507515
nfOptions.Sortable = true
508516
}
517+
f.Options = nfOptions
509518
case "TEXT":
510519
f.Type = TextField
511520
tfOptions := TextFieldOptions{}
512-
f.Options = tfOptions
513521
if sliceIndex(options, "SORTABLE") != -1 {
514522
tfOptions.Sortable = true
515523
}
@@ -518,6 +526,7 @@ func (info *IndexInfo) loadSchema(values []interface{}, options []string) {
518526
weight64, _ := strconv.ParseFloat(weightString, 32)
519527
tfOptions.Weight = float32(weight64)
520528
}
529+
f.Options = tfOptions
521530
}
522531
sc = sc.AddField(f)
523532
}

redisearch/client_test.go

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -901,8 +901,22 @@ func TestClient_CreateIndex(t *testing.T) {
901901

902902
// Create a schema
903903
schema := NewSchema(DefaultOptions).
904-
AddField(NewTextFieldOptions("name", TextFieldOptions{Sortable: true, PhoneticMatcher: PhoneticDoubleMetaphoneEnglish})).
905-
AddField(NewNumericField("age"))
904+
AddField(NewGeoField("geo")). // There are no useful opttions for geo field at this time
905+
AddField(
906+
NewTagFieldOptions(
907+
"tags",
908+
TagFieldOptions{Separator: '|'},
909+
)).
910+
AddField(
911+
NewTextFieldOptions(
912+
"name",
913+
TextFieldOptions{Sortable: true, PhoneticMatcher: PhoneticDoubleMetaphoneEnglish},
914+
)).
915+
AddField(
916+
NewNumericFieldOptions(
917+
"age",
918+
NumericFieldOptions{Sortable: true},
919+
))
906920

907921
// IndexDefinition is available for RediSearch 2.0+
908922
// In this example we will only index keys started by product:
@@ -914,9 +928,9 @@ func TestClient_CreateIndex(t *testing.T) {
914928

915929
// Create docs with a name that has the same phonetic matcher
916930
vanillaConnection := c.pool.Get()
917-
_, err = vanillaConnection.Do("HSET", "create-index-info:doc1", "name", "Jon", "age", 25)
931+
_, err = vanillaConnection.Do("HSET", "create-index-info:doc1", "name", "Jon", "age", 25, "tags", "tag1|tag2", "geo", "40.7222756,-73.9977894")
918932
assert.Nil(t, err)
919-
_, err = vanillaConnection.Do("HSET", "create-index-info:doc2", "name", "John", "age", 20)
933+
_, err = vanillaConnection.Do("HSET", "create-index-info:doc2", "name", "John", "age", 20, "tags", "tag1|tag2", "geo", "40.7222756,-73.9977894")
920934
assert.Nil(t, err)
921935

922936
// Wait for all documents to be indexed
@@ -937,6 +951,24 @@ func TestClient_CreateIndex(t *testing.T) {
937951
assert.Equal(t, 2, total)
938952
assert.Equal(t, "Jon", docs[0].Properties["name"])
939953
assert.Equal(t, "John", docs[1].Properties["name"])
954+
955+
// Verify that each of the field types is correct
956+
for _, f := range info.Schema.Fields {
957+
switch {
958+
case f.Name == "age":
959+
assert.Equal(t, 1, int(f.Type))
960+
assert.Equal(t, true, f.Options.(NumericFieldOptions).Sortable)
961+
case f.Name == "name":
962+
assert.Equal(t, 0, int(f.Type))
963+
assert.Equal(t, true, f.Options.(TextFieldOptions).Sortable)
964+
case f.Name == "tags":
965+
assert.Equal(t, 3, int(f.Type))
966+
// Verify that the tags separators is | or 0x7c
967+
assert.Equal(t, uint8(0x7c), f.Options.(TagFieldOptions).Separator)
968+
case f.Name == "geo":
969+
assert.Equal(t, 2, int(f.Type))
970+
}
971+
}
940972
}
941973

942974
func TestClient_CreateJsonIndex(t *testing.T) {

0 commit comments

Comments
 (0)