@@ -19,56 +19,22 @@ This example shows how to create a
19
19
for [ JSON] ({{< relref "/develop/data-types/json" >}}) data and
20
20
run queries against the index.
21
21
22
- Make sure that you have Redis Stack and ` NRedisStack ` installed.
23
-
24
- Start by connecting to the Redis server:
25
-
26
- ``` go
27
- import (
28
- " context"
29
- " fmt"
30
-
31
- " github.com/redis/go-redis/v9"
32
- )
33
-
34
- func main () {
35
- ctx := context.Background ()
36
-
37
- rdb := redis.NewClient (&redis.Options {
38
- Addr: " localhost:6379" ,
39
- Password: " " ,
40
- DB: 0 ,
41
- Protocol: 2 ,
42
- })
43
-
44
- // ...
45
- }
46
- ```
47
-
48
- Add some ` map ` objects to store in JSON format in the database:
49
-
50
- ``` go
51
- user1 := map [string ]interface {}{
52
- " name" : " Paul John" ,
53
- " email" : " paul.john@example.com" ,
54
- " age" : 42 ,
55
- " city" : " London" ,
56
- }
57
-
58
- user2 := map [string ]interface {}{
59
- " name" : " Eden Zamir" ,
60
- " email" : " eden.zamir@example.com" ,
61
- " age" : 29 ,
62
- " city" : " Tel Aviv" ,
63
- }
64
-
65
- user3 := map [string ]interface {}{
66
- " name" : " Paul Zamir" ,
67
- " email" : " paul.zamir@example.com" ,
68
- " age" : 35 ,
69
- " city" : " Tel Aviv" ,
70
- }
71
- ```
22
+ Make sure that you have Redis Stack and ` go-redis ` installed.
23
+
24
+ Start by importing dependencies:
25
+
26
+ {{< clients-example go_home_json import >}}
27
+ {{< /clients-example >}}
28
+
29
+ Connect to the database:
30
+
31
+ {{< clients-example go_home_json connect >}}
32
+ {{< /clients-example >}}
33
+
34
+ Create some test data to add to the database:
35
+
36
+ {{< clients-example go_home_json create_data >}}
37
+ {{< /clients-example >}}
72
38
73
39
Use the code below to create a search index. The ` FTCreateOptions ` parameter enables
74
40
indexing only for JSON objects where the key has a ` user: ` prefix.
@@ -82,79 +48,36 @@ to provide an alias for the JSON path expression. You can use
82
48
the alias in queries as a short and intuitive way to refer to the
83
49
expression, instead of typing it in full:
84
50
85
- ``` go
86
- _ , err := rdb.FTCreate (
87
- ctx,
88
- " idx:users" ,
89
- // Options:
90
- &redis.FTCreateOptions {
91
- OnJSON : true ,
92
- Prefix : []interface {}{" user:" },
93
- },
94
- // Index schema fields:
95
- &redis.FieldSchema {
96
- FieldName : " $.name" ,
97
- As : " name" ,
98
- FieldType : redis.SearchFieldTypeText ,
99
- },
100
- &redis.FieldSchema {
101
- FieldName : " $.city" ,
102
- As : " city" ,
103
- FieldType : redis.SearchFieldTypeTag ,
104
- },
105
- &redis.FieldSchema {
106
- FieldName : " $.age" ,
107
- As : " age" ,
108
- FieldType : redis.SearchFieldTypeNumeric ,
109
- },
110
- ).Result ()
111
-
112
- if err != nil {
113
- panic (err)
114
- }
115
- ```
51
+ {{< clients-example go_home_json make_index >}}
52
+ {{< /clients-example >}}
116
53
117
54
Add the three sets of user data to the database as
118
55
[ JSON] ({{< relref "/develop/data-types/json" >}}) objects.
119
56
If you use keys with the ` user: ` prefix then Redis will index the
120
57
objects automatically as you add them:
121
58
122
- ``` go
123
- _, err = rdb.JSONSet (ctx, " user:1" , " $" , user1).Result ()
124
-
125
- if err != nil {
126
- panic (err)
127
- }
128
-
129
- _, err = rdb.JSONSet (ctx, " user:2" , " $" , user2).Result ()
130
-
131
- if err != nil {
132
- panic (err)
133
- }
134
-
135
- _, err = rdb.JSONSet (ctx, " user:3" , " $" , user3).Result ()
136
-
137
- if err != nil {
138
- panic (err)
139
- }
140
- ```
59
+ {{< clients-example go_home_json add_data >}}
60
+ {{< /clients-example >}}
141
61
142
62
You can now use the index to search the JSON objects. The
143
63
[ query] ({{< relref "/develop/interact/search-and-query/query" >}})
144
64
below searches for objects that have the text "Paul" in any field
145
65
and have an ` age ` value in the range 30 to 40:
146
66
147
- ``` go
148
- searchResult , err := rdb.FTSearch (
149
- ctx,
150
- " idx:users" ,
151
- " Paul @age:[30 40]" ,
152
- ).Result ()
67
+ {{< clients-example go_home_json query1 >}}
68
+ {{< /clients-example >}}
69
+
70
+ Specify query options to return only the ` city ` field:
71
+
72
+ {{< clients-example go_home_json query2 >}}
73
+ {{< /clients-example >}}
74
+
75
+ Use an
76
+ [ aggregation query] ({{< relref "/develop/interact/search-and-query/query/aggregation" >}})
77
+ to count all users in each city.
153
78
154
- if err != nil {
155
- panic (err)
156
- }
79
+ {{< clients-example go_home_json query3 >}}
80
+ {{< /clients-example >}}
157
81
158
- fmt.Println (searchResult)
159
- // >>> {1 [{user:3 <nil> <nil> <nil> map[$:{"age":35,"city":"Tel Aviv"...
160
- ```
82
+ See the [ Redis query engine] ({{< relref "/develop/interact/search-and-query" >}}) docs
83
+ for a full description of all query features with examples.
0 commit comments