@@ -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/rc" >}})
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
@@ -123,8 +142,8 @@ need to specify some slightly different options.
123
142
124
143
When you create the schema for a hash index, you don't need to
125
144
add aliases for the fields, since you use the basic names to access
126
- the fields anyway. Also, you must use ` HASH ` for the ` IndexType `
127
- when you create the index. The code below shows these changes with
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
128
147
a new index called ` hash-idx:users ` , which is otherwise the same as
129
148
the ` idx:users ` index used for JSON documents in the previous examples.
130
149
@@ -142,8 +161,8 @@ hash or JSON:
142
161
The query commands work the same here for hash as they do for JSON (but
143
162
the name of the hash index is different). The format of the result is
144
163
also almost the same except that the fields are returned directly in the
145
- result ` Document ` object map instead of in an enclosing ` json ` string
146
- under the key "$":
164
+ ` Document ` object map of the result (for JSON, the fields are all enclosed
165
+ in a string under the key "$") :
147
166
148
167
{{< clients-example go_home_json query1_hash >}}
149
168
{{< /clients-example >}}
0 commit comments