@@ -9,18 +9,27 @@ 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 : 20
16
16
---
17
17
18
- This example shows how to index and query Redis JSON data using ` predis ` .
18
+ This example shows how to create a
19
+ [ search index] ({{< relref "/develop/interact/search-and-query/indexing" >}})
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.
19
24
20
- Make sure that you have Redis Community Edition and ` predis ` installed, as described
21
- in the [ Install] ( #install ) section above.
25
+ ## Initialize
22
26
23
- Start by importing dependencies:
27
+ Make sure that you have [ Redis Community Edition] ({{< relref "/operate/oss_and_stack/" >}})
28
+ or another Redis server available. Also install the
29
+ [ ` Predis ` ] ({{< relref "/develop/clients/php" >}}) client library if you
30
+ haven't already done so.
31
+
32
+ Add the following dependencies:
24
33
25
34
``` php
26
35
<?php
@@ -38,19 +47,9 @@ use Predis\Command\Argument\Search\SchemaFields\TagField;
38
47
use Predis\Command\Argument\Search\SchemaFields\VectorField;
39
48
```
40
49
41
- Connect to the Redis server:
42
-
43
- ``` php
44
- $r = new PredisClient([
45
- 'scheme' => 'tcp',
46
- 'host' => '127.0.0.1',
47
- 'port' => 6379,
48
- 'password' => '',
49
- 'database' => 0,
50
- ]);
51
- ```
50
+ ## Create data
52
51
53
- Create some test data to add to the database:
52
+ Create some test data to add to your database:
54
53
55
54
``` php
56
55
$user1 = json_encode([
@@ -75,6 +74,23 @@ $user3 = json_encode([
75
74
], JSON_THROW_ON_ERROR);
76
75
```
77
76
77
+ ## Add the index
78
+
79
+ Connect to your Redis database. The code below shows the most
80
+ basic connection but see
81
+ [ Connect to the server] ({{< relref "/develop/clients/php/connect" >}})
82
+ to learn more about the available connection options.
83
+
84
+ ``` php
85
+ $r = new PredisClient([
86
+ 'scheme' => 'tcp',
87
+ 'host' => '127.0.0.1',
88
+ 'port' => 6379,
89
+ 'password' => '',
90
+ 'database' => 0,
91
+ ]);
92
+ ```
93
+
78
94
Create an
79
95
[ index] ({{< relref "/develop/interact/search-and-query/indexing" >}}).
80
96
In this example, only JSON documents with the key prefix ` user: ` are indexed.
@@ -99,6 +115,8 @@ catch (Exception $e) {
99
115
}
100
116
```
101
117
118
+ ## Add the data
119
+
102
120
Add the three sets of user data to the database as
103
121
[ JSON] ({{< relref "/develop/data-types/json" >}}) objects.
104
122
If you use keys with the ` user: ` prefix then Redis will index the
@@ -110,6 +128,8 @@ $r->jsonset('user:2', '$', $user2);
110
128
$r->jsonset('user:3', '$', $user3);
111
129
```
112
130
131
+ ## Query the data
132
+
113
133
You can now use the index to search the JSON objects. The
114
134
[ query] ({{< relref "/develop/interact/search-and-query/query" >}})
115
135
below searches for objects that have the text "Paul" in any field
@@ -149,5 +169,76 @@ echo json_encode($res), PHP_EOL;
149
169
// >>> [2,["city","London","count","1"],["city","Tel Aviv","count","2"]]
150
170
```
151
171
172
+ ## Differences with hash documents
173
+
174
+ Indexing for hash documents is very similar to JSON indexing but you
175
+ need to specify some slightly different options.
176
+
177
+ When you create the schema for a hash index, you don't need to
178
+ add aliases for the fields, since you use the basic names to access
179
+ the fields anyway. Also, you must use ` HASH ` for the ` On() ` option
180
+ when you create the index. The code below shows these changes with
181
+ a new index called ` hash-idx:users ` , which is otherwise the same as
182
+ the ` idx:users ` index used for JSON documents in the previous examples.
183
+
184
+ ``` php
185
+ $hashSchema = [
186
+ new TextField('name'),
187
+ new TagField('city'),
188
+ new NumericField('age'),
189
+ ];
190
+
191
+ try {
192
+ $r->ftCreate("hash-idx:users", $hashSchema,
193
+ (new CreateArguments())
194
+ ->on('HASH')
195
+ ->prefix(["huser:"]));
196
+ }
197
+ catch (Exception $e) {
198
+ echo $e->getMessage(), PHP_EOL;
199
+ }
200
+ ```
201
+
202
+ You use [ ` hmset() ` ] ({{< relref "/commands/hset" >}}) to add the hash
203
+ documents instead of [ ` jsonset() ` ] ({{< relref "/commands/json.set" >}}).
204
+ Supply the fields as an array directly, without using
205
+ [ ` json_encode() ` ] ( https://www.php.net/manual/en/function.json-encode.php ) .
206
+
207
+ ``` php
208
+ $r->hmset('huser:1', [
209
+ 'name' => 'Paul John',
210
+ 'email' => 'paul.john@example.com',
211
+ 'age' => 42,
212
+ 'city' => 'London',
213
+ ]);
214
+
215
+ $r->hmset('huser:2', [
216
+ 'name' => 'Eden Zamir',
217
+ 'email' => 'eden.zamir@example.com',
218
+ 'age' => 29,
219
+ 'city' => 'Tel Aviv',
220
+ ]);
221
+
222
+ $r->hmset('huser:3', [
223
+ 'name' => 'Paul Zamir',
224
+ 'email' => 'paul.zamir@example.com',
225
+ 'age' => 35,
226
+ 'city' => 'Tel Aviv',
227
+ ]);
228
+ ```
229
+
230
+ The query commands work the same here for hash as they do for JSON (but
231
+ the name of the hash index is different). The format of the result is
232
+ almost the same except that the fields are returned directly in the
233
+ result array rather than in a JSON string with ` $ ` as its key:
234
+
235
+ ``` php
236
+ $res = $r->ftSearch("hash-idx:users", "Paul @age:[30 40]");
237
+ echo json_encode($res), PHP_EOL;
238
+ // >>> [1,"huser:3",["age","35","city","Tel Aviv","email","paul.zamir@example.com","name","Paul Zamir"]]
239
+ ```
240
+
241
+ ## More information
242
+
152
243
See the [ Redis query engine] ({{< relref "/develop/interact/search-and-query" >}}) docs
153
244
for a full description of all query features with examples.
0 commit comments