@@ -9,24 +9,45 @@ categories:
9
9
- oss
10
10
- kubernetes
11
11
- clients
12
- description : Learn how to use the Redis query engine with JSON
13
- linkTitle : Index and query JSON
14
- title : Example - Index and query JSON documents
12
+ description : Learn how to use the Redis query engine with JSON and hash documents.
13
+ linkTitle : Index and query documents
14
+ title : Index and query documents
15
15
weight : 2
16
16
---
17
+
17
18
This example shows how to create a
18
19
[ search index] ({{< relref "/develop/interact/search-and-query/indexing" >}})
19
- for [ JSON] ({{< relref "/develop/data-types/json" >}}) data and
20
- run queries against the index.
20
+ for [ JSON] ({{< relref "/develop/data-types/json" >}}) documents and
21
+ run queries against the index. It then goes on to show the slight differences
22
+ in the equivalent code for [ hash] ({{< relref "/develop/data-types/hashes" >}})
23
+ documents.
24
+
25
+ ## Initialize
21
26
22
- Make sure that you have Redis Community Edition and ` go-redis ` installed.
27
+ Make sure that you have [ Redis Community Edition] ({{< relref "/operate/oss_and_stack/" >}})
28
+ or another Redis server available. Also install the
29
+ [ ` go-redis ` ] ({{< relref "/develop/clients/go" >}}) client library if you
30
+ haven't already done so.
23
31
24
- Start by importing dependencies:
32
+ Add the following dependencies:
25
33
26
34
{{< clients-example go_home_json import >}}
27
35
{{< /clients-example >}}
28
36
29
- Connect to the database:
37
+ ## Create data
38
+
39
+ Create some test data to add to your database. The example data shown
40
+ below is compatible with both JSON and hash objects.
41
+
42
+ {{< clients-example go_home_json create_data >}}
43
+ {{< /clients-example >}}
44
+
45
+ ## Add the index
46
+
47
+ Connect to your Redis database. The code below shows the most
48
+ basic connection but see
49
+ [ Connect to the server] ({{< relref "/develop/clients/go/connect" >}})
50
+ to learn more about the available connection options.
30
51
31
52
{{< clients-example go_home_json connect >}}
32
53
{{< /clients-example >}}
@@ -60,12 +81,6 @@ val1 := client.FTSearchWithArgs(
60
81
```
61
82
{{< /note >}}
62
83
63
-
64
- Create some test data to add to the database:
65
-
66
- {{< clients-example go_home_json create_data >}}
67
- {{< /clients-example >}}
68
-
69
84
Use the code below to create a search index. The ` FTCreateOptions ` parameter enables
70
85
indexing only for JSON objects where the key has a ` user: ` prefix.
71
86
The
@@ -81,6 +96,8 @@ expression, instead of typing it in full:
81
96
{{< clients-example go_home_json make_index >}}
82
97
{{< /clients-example >}}
83
98
99
+ ## Add the data
100
+
84
101
Add the three sets of user data to the database as
85
102
[ JSON] ({{< relref "/develop/data-types/json" >}}) objects.
86
103
If you use keys with the ` user: ` prefix then Redis will index the
@@ -89,6 +106,8 @@ objects automatically as you add them:
89
106
{{< clients-example go_home_json add_data >}}
90
107
{{< /clients-example >}}
91
108
109
+ ## Query the data
110
+
92
111
You can now use the index to search the JSON objects. The
93
112
[ query] ({{< relref "/develop/interact/search-and-query/query" >}})
94
113
below searches for objects that have the text "Paul" in any field
@@ -116,5 +135,39 @@ to count all users in each city.
116
135
{{< clients-example go_home_json query3 >}}
117
136
{{< /clients-example >}}
118
137
138
+ ## Differences with hash documents
139
+
140
+ Indexing for hash documents is very similar to JSON indexing but you
141
+ need to specify some slightly different options.
142
+
143
+ When you create the schema for a hash index, you don't need to
144
+ add aliases for the fields, since you use the basic names to access
145
+ the fields anyway. Also, you must set ` OnHash ` to ` true ` in the ` FTCreateOptions `
146
+ object when you create the index. The code below shows these changes with
147
+ a new index called ` hash-idx:users ` , which is otherwise the same as
148
+ the ` idx:users ` index used for JSON documents in the previous examples.
149
+
150
+ {{< clients-example go_home_json make_hash_index >}}
151
+ {{< /clients-example >}}
152
+
153
+ You use [ ` HSet() ` ] ({{< relref "/commands/hset" >}}) to add the hash
154
+ documents instead of [ ` JSONSet() ` ] ({{< relref "/commands/json.set" >}}),
155
+ but the same flat ` userX ` maps work equally well with either
156
+ hash or JSON:
157
+
158
+ {{< clients-example go_home_json add_hash_data >}}
159
+ {{< /clients-example >}}
160
+
161
+ The query commands work the same here for hash as they do for JSON (but
162
+ the name of the hash index is different). The format of the result is
163
+ almost the same except that the fields are returned directly in the
164
+ ` Document ` object map of the result (for JSON, the fields are all enclosed
165
+ in a string under the key "$"):
166
+
167
+ {{< clients-example go_home_json query1_hash >}}
168
+ {{< /clients-example >}}
169
+
170
+ ## More information
171
+
119
172
See the [ Redis query engine] ({{< relref "/develop/interact/search-and-query" >}}) docs
120
- for a full description of all query features with examples.
173
+ for a full description of all query features with examples.
0 commit comments