@@ -48,48 +48,15 @@ haven't already done so.
4848
4949Add the following dependencies:
5050
51- ``` php
52- <?php
53-
54- require 'vendor/autoload.php';
55-
56- use Predis\Client as PredisClient;
57-
58- use Predis\Command\Argument\Search\AggregateArguments;
59- use Predis\Command\Argument\Search\CreateArguments;
60- use Predis\Command\Argument\Search\SearchArguments;
61- use Predis\Command\Argument\Search\SchemaFields\NumericField;
62- use Predis\Command\Argument\Search\SchemaFields\TextField;
63- use Predis\Command\Argument\Search\SchemaFields\TagField;
64- use Predis\Command\Argument\Search\SchemaFields\VectorField;
65- ```
51+ {{< clients-example set="php_home_json" step="import" >}}
52+ {{< /clients-example >}}
6653
6754## Create data
6855
6956Create some test data to add to your database:
7057
71- ``` php
72- $user1 = json_encode([
73- 'name' => 'Paul John',
74- 75- 'age' => 42,
76- 'city' => 'London',
77- ], JSON_THROW_ON_ERROR);
78-
79- $user2 = json_encode([
80- 'name' => 'Eden Zamir',
81- 82- 'age' => 29,
83- 'city' => 'Tel Aviv',
84- ], JSON_THROW_ON_ERROR);
85-
86- $user3 = json_encode([
87- 'name' => 'Paul Zamir',
88- 89- 'age' => 35,
90- 'city' => 'Tel Aviv',
91- ], JSON_THROW_ON_ERROR);
92- ```
58+ {{< clients-example set="php_home_json" step="create_data" >}}
59+ {{< /clients-example >}}
9360
9461## Add the index
9562
@@ -98,39 +65,17 @@ basic connection but see
9865[ Connect to the server] ({{< relref "/develop/clients/php/connect" >}})
9966to learn more about the available connection options.
10067
101- ``` php
102- $r = new PredisClient([
103- 'scheme' => 'tcp',
104- 'host' => '127.0.0.1',
105- 'port' => 6379,
106- 'password' => '',
107- 'database' => 0,
108- ]);
109- ```
68+ {{< clients-example set="php_home_json" step="connect" >}}
69+ {{< /clients-example >}}
11070
11171Create an
11272[ index] ({{< relref "/develop/ai/search-and-query/indexing" >}}).
11373In this example, only JSON documents with the key prefix ` user: ` are indexed.
11474For more information, see
11575[ Query syntax] ({{< relref "/develop/ai/search-and-query/query/" >}}).
11676
117- ``` php
118- $schema = [
119- new TextField('$.name', 'name'),
120- new TagField('$.city', 'city'),
121- new NumericField('$.age', "age"),
122- ];
123-
124- try {
125- $r->ftCreate("idx:users", $schema,
126- (new CreateArguments())
127- ->on('JSON')
128- ->prefix(["user:"]));
129- }
130- catch (Exception $e) {
131- echo $e->getMessage(), PHP_EOL;
132- }
133- ```
77+ {{< clients-example set="php_home_json" step="make_index" >}}
78+ {{< /clients-example >}}
13479
13580## Add the data
13681
@@ -139,11 +84,8 @@ Add the three sets of user data to the database as
13984If you use keys with the ` user: ` prefix then Redis will index the
14085objects automatically as you add them:
14186
142- ``` php
143- $r->jsonset('user:1', '$', $user1);
144- $r->jsonset('user:2', '$', $user2);
145- $r->jsonset('user:3', '$', $user3);
146- ```
87+ {{< clients-example set="php_home_json" step="add_data" >}}
88+ {{< /clients-example >}}
14789
14890## Query the data
14991
@@ -152,39 +94,20 @@ You can now use the index to search the JSON objects. The
15294below searches for objects that have the text "Paul" in any field
15395and have an ` age ` value in the range 30 to 40:
15496
155- ``` php
156- $res = $r->ftSearch("idx:users", "Paul @age:[30 40]");
157- echo json_encode($res), PHP_EOL;
158- // >>> [1,"user:3",["$","{\"name\":\"Paul Zamir\",\"email\":\"
[email protected] \",\"age\":35,\"city\":\"London\"}"]]
159- ```
97+ {{< clients-example set="php_home_json" step="query1" >}}
98+ {{< /clients-example >}}
16099
161100Specify query options to return only the ` city ` field:
162101
163- ``` php
164- $arguments = new SearchArguments();
165- $arguments->addReturn(3, '$.city', true, 'thecity');
166- $arguments->dialect(2);
167- $arguments->limit(0, 5);
168-
169- $res = $r->ftSearch("idx:users", "Paul", $arguments);
170-
171- echo json_encode($res), PHP_EOL;
172- // >>> [2,"user:1",["thecity","London"],"user:3",["thecity","Tel Aviv"]]
173- ```
102+ {{< clients-example set="php_home_json" step="query2" >}}
103+ {{< /clients-example >}}
174104
175105Use an
176106[ aggregation query] ({{< relref "/develop/ai/search-and-query/query/aggregation" >}})
177107to count all users in each city.
178108
179- ``` php
180- $ftAggregateArguments = (new AggregateArguments())
181- ->groupBy('@city')
182- ->reduce('COUNT', true, 'count');
183-
184- $res = $r->ftAggregate('idx:users', '*', $ftAggregateArguments);
185- echo json_encode($res), PHP_EOL;
186- // >>> [2,["city","London","count","1"],["city","Tel Aviv","count","2"]]
187- ```
109+ {{< clients-example set="php_home_json" step="query3" >}}
110+ {{< /clients-example >}}
188111
189112## Differences with hash documents
190113
@@ -198,62 +121,24 @@ when you create the index. The code below shows these changes with
198121a new index called ` hash-idx:users ` , which is otherwise the same as
199122the ` idx:users ` index used for JSON documents in the previous examples.
200123
201- ``` php
202- $hashSchema = [
203- new TextField('name'),
204- new TagField('city'),
205- new NumericField('age'),
206- ];
207-
208- try {
209- $r->ftCreate("hash-idx:users", $hashSchema,
210- (new CreateArguments())
211- ->on('HASH')
212- ->prefix(["huser:"]));
213- }
214- catch (Exception $e) {
215- echo $e->getMessage(), PHP_EOL;
216- }
217- ```
124+ {{< clients-example set="php_home_json" step="make_hash_index" >}}
125+ {{< /clients-example >}}
218126
219127You use [ ` hmset() ` ] ({{< relref "/commands/hset" >}}) to add the hash
220128documents instead of [ ` jsonset() ` ] ({{< relref "/commands/json.set" >}}).
221129Supply the fields as an array directly, without using
222130[ ` json_encode() ` ] ( https://www.php.net/manual/en/function.json-encode.php ) .
223131
224- ``` php
225- $r->hmset('huser:1', [
226- 'name' => 'Paul John',
227- 228- 'age' => 42,
229- 'city' => 'London',
230- ]);
231-
232- $r->hmset('huser:2', [
233- 'name' => 'Eden Zamir',
234- 235- 'age' => 29,
236- 'city' => 'Tel Aviv',
237- ]);
238-
239- $r->hmset('huser:3', [
240- 'name' => 'Paul Zamir',
241- 242- 'age' => 35,
243- 'city' => 'Tel Aviv',
244- ]);
245- ```
132+ {{< clients-example set="php_home_json" step="add_hash_data" >}}
133+ {{< /clients-example >}}
246134
247135The query commands work the same here for hash as they do for JSON (but
248136the name of the hash index is different). The format of the result is
249137almost the same except that the fields are returned directly in the
250138result array rather than in a JSON string with ` $ ` as its key:
251139
252- ``` php
253- $res = $r->ftSearch("hash-idx:users", "Paul @age:[30 40]");
254- echo json_encode($res), PHP_EOL;
255- // >>> [1,"huser:3",["age","35","city","Tel Aviv","email","
[email protected] ","name","Paul Zamir"]]
256- ```
140+ {{< clients-example set="php_home_json" step="query1_hash" >}}
141+ {{< /clients-example >}}
257142
258143## More information
259144
0 commit comments