Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 22 additions & 137 deletions content/develop/clients/php/queryjson.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,48 +48,15 @@ haven't already done so.

Add the following dependencies:

```php
<?php

require 'vendor/autoload.php';

use Predis\Client as PredisClient;

use Predis\Command\Argument\Search\AggregateArguments;
use Predis\Command\Argument\Search\CreateArguments;
use Predis\Command\Argument\Search\SearchArguments;
use Predis\Command\Argument\Search\SchemaFields\NumericField;
use Predis\Command\Argument\Search\SchemaFields\TextField;
use Predis\Command\Argument\Search\SchemaFields\TagField;
use Predis\Command\Argument\Search\SchemaFields\VectorField;
```
{{< clients-example set="php_home_json" step="import" >}}
{{< /clients-example >}}

## Create data

Create some test data to add to your database:

```php
$user1 = json_encode([
'name' => 'Paul John',
'email' => 'paul.john@example.com',
'age' => 42,
'city' => 'London',
], JSON_THROW_ON_ERROR);

$user2 = json_encode([
'name' => 'Eden Zamir',
'email' => 'eden.zamir@example.com',
'age' => 29,
'city' => 'Tel Aviv',
], JSON_THROW_ON_ERROR);

$user3 = json_encode([
'name' => 'Paul Zamir',
'email' => 'paul.zamir@example.com',
'age' => 35,
'city' => 'Tel Aviv',
], JSON_THROW_ON_ERROR);
```
{{< clients-example set="php_home_json" step="create_data" >}}
{{< /clients-example >}}

## Add the index

Expand All @@ -98,39 +65,17 @@ basic connection but see
[Connect to the server]({{< relref "/develop/clients/php/connect" >}})
to learn more about the available connection options.

```php
$r = new PredisClient([
'scheme' => 'tcp',
'host' => '127.0.0.1',
'port' => 6379,
'password' => '',
'database' => 0,
]);
```
{{< clients-example set="php_home_json" step="connect" >}}
{{< /clients-example >}}

Create an
[index]({{< relref "/develop/ai/search-and-query/indexing" >}}).
In this example, only JSON documents with the key prefix `user:` are indexed.
For more information, see
[Query syntax]({{< relref "/develop/ai/search-and-query/query/" >}}).

```php
$schema = [
new TextField('$.name', 'name'),
new TagField('$.city', 'city'),
new NumericField('$.age', "age"),
];

try {
$r->ftCreate("idx:users", $schema,
(new CreateArguments())
->on('JSON')
->prefix(["user:"]));
}
catch (Exception $e) {
echo $e->getMessage(), PHP_EOL;
}
```
{{< clients-example set="php_home_json" step="make_index" >}}
{{< /clients-example >}}

## Add the data

Expand All @@ -139,11 +84,8 @@ Add the three sets of user data to the database as
If you use keys with the `user:` prefix then Redis will index the
objects automatically as you add them:

```php
$r->jsonset('user:1', '$', $user1);
$r->jsonset('user:2', '$', $user2);
$r->jsonset('user:3', '$', $user3);
```
{{< clients-example set="php_home_json" step="add_data" >}}
{{< /clients-example >}}

## Query the data

Expand All @@ -152,39 +94,20 @@ You can now use the index to search the JSON objects. The
below searches for objects that have the text "Paul" in any field
and have an `age` value in the range 30 to 40:

```php
$res = $r->ftSearch("idx:users", "Paul @age:[30 40]");
echo json_encode($res), PHP_EOL;
// >>> [1,"user:3",["$","{\"name\":\"Paul Zamir\",\"email\":\"paul.zamir@example.com\",\"age\":35,\"city\":\"London\"}"]]
```
{{< clients-example set="php_home_json" step="query1" >}}
{{< /clients-example >}}

Specify query options to return only the `city` field:

```php
$arguments = new SearchArguments();
$arguments->addReturn(3, '$.city', true, 'thecity');
$arguments->dialect(2);
$arguments->limit(0, 5);

$res = $r->ftSearch("idx:users", "Paul", $arguments);

echo json_encode($res), PHP_EOL;
// >>> [2,"user:1",["thecity","London"],"user:3",["thecity","Tel Aviv"]]
```
{{< clients-example set="php_home_json" step="query2" >}}
{{< /clients-example >}}

Use an
[aggregation query]({{< relref "/develop/ai/search-and-query/query/aggregation" >}})
to count all users in each city.

```php
$ftAggregateArguments = (new AggregateArguments())
->groupBy('@city')
->reduce('COUNT', true, 'count');

$res = $r->ftAggregate('idx:users', '*', $ftAggregateArguments);
echo json_encode($res), PHP_EOL;
// >>> [2,["city","London","count","1"],["city","Tel Aviv","count","2"]]
```
{{< clients-example set="php_home_json" step="query3" >}}
{{< /clients-example >}}

## Differences with hash documents

Expand All @@ -198,62 +121,24 @@ when you create the index. The code below shows these changes with
a new index called `hash-idx:users`, which is otherwise the same as
the `idx:users` index used for JSON documents in the previous examples.

```php
$hashSchema = [
new TextField('name'),
new TagField('city'),
new NumericField('age'),
];

try {
$r->ftCreate("hash-idx:users", $hashSchema,
(new CreateArguments())
->on('HASH')
->prefix(["huser:"]));
}
catch (Exception $e) {
echo $e->getMessage(), PHP_EOL;
}
```
{{< clients-example set="php_home_json" step="make_hash_index" >}}
{{< /clients-example >}}

You use [`hmset()`]({{< relref "/commands/hset" >}}) to add the hash
documents instead of [`jsonset()`]({{< relref "/commands/json.set" >}}).
Supply the fields as an array directly, without using
[`json_encode()`](https://www.php.net/manual/en/function.json-encode.php).

```php
$r->hmset('huser:1', [
'name' => 'Paul John',
'email' => 'paul.john@example.com',
'age' => 42,
'city' => 'London',
]);

$r->hmset('huser:2', [
'name' => 'Eden Zamir',
'email' => 'eden.zamir@example.com',
'age' => 29,
'city' => 'Tel Aviv',
]);

$r->hmset('huser:3', [
'name' => 'Paul Zamir',
'email' => 'paul.zamir@example.com',
'age' => 35,
'city' => 'Tel Aviv',
]);
```
{{< clients-example set="php_home_json" step="add_hash_data" >}}
{{< /clients-example >}}

The query commands work the same here for hash as they do for JSON (but
the name of the hash index is different). The format of the result is
almost the same except that the fields are returned directly in the
result array rather than in a JSON string with `$` as its key:

```php
$res = $r->ftSearch("hash-idx:users", "Paul @age:[30 40]");
echo json_encode($res), PHP_EOL;
// >>> [1,"huser:3",["age","35","city","Tel Aviv","email","paul.zamir@example.com","name","Paul Zamir"]]
```
{{< clients-example set="php_home_json" step="query1_hash" >}}
{{< /clients-example >}}

## More information

Expand Down
Loading