Skip to content

DOC-5111 added hash search examples #3357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
104 changes: 104 additions & 0 deletions doctests/home_json_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,107 @@ func ExampleClient_search_json() {
// London - 1
// Tel Aviv - 2
}

func ExampleClient_search_hash() {
ctx := context.Background()

rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
Protocol: 2,
})

// REMOVE_START
rdb.Del(ctx, "huser:1", "huser:2", "huser:3")
rdb.FTDropIndex(ctx, "hash-idx:users")
// REMOVE_END

// STEP_START make_hash_index
_, err := rdb.FTCreate(
ctx,
"hash-idx:users",
// Options:
&redis.FTCreateOptions{
OnHash: true,
Prefix: []interface{}{"huser:"},
},
// Index schema fields:
&redis.FieldSchema{
FieldName: "name",
FieldType: redis.SearchFieldTypeText,
},
&redis.FieldSchema{
FieldName: "city",
FieldType: redis.SearchFieldTypeTag,
},
&redis.FieldSchema{
FieldName: "age",
FieldType: redis.SearchFieldTypeNumeric,
},
).Result()

if err != nil {
panic(err)
}
// STEP_END

user1 := map[string]interface{}{
"name": "Paul John",
"email": "[email protected]",
"age": 42,
"city": "London",
}

user2 := map[string]interface{}{
"name": "Eden Zamir",
"email": "[email protected]",
"age": 29,
"city": "Tel Aviv",
}

user3 := map[string]interface{}{
"name": "Paul Zamir",
"email": "[email protected]",
"age": 35,
"city": "Tel Aviv",
}

// STEP_START add_hash_data
_, err = rdb.HSet(ctx, "huser:1", user1).Result()

if err != nil {
panic(err)
}

_, err = rdb.HSet(ctx, "huser:2", user2).Result()

if err != nil {
panic(err)
}

_, err = rdb.HSet(ctx, "huser:3", user3).Result()

if err != nil {
panic(err)
}
// STEP_END

// STEP_START query1_hash
findPaulHashResult, err := rdb.FTSearch(
ctx,
"hash-idx:users",
"Paul @age:[30 40]",
).Result()

if err != nil {
panic(err)
}

fmt.Println(findPaulHashResult)
// >>> {1 [{huser:3 <nil> <nil> <nil> map[age:35 city:Tel Aviv...
// STEP_END

// Output:
// {1 [{huser:3 <nil> <nil> <nil> map[age:35 city:Tel Aviv email:[email protected] name:Paul Zamir]}]}
}
Loading