Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
12 changes: 12 additions & 0 deletions content/develop/clients/dotnet/queryjson.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ to learn more about the available connection options.
{{< clients-example cs_home_json connect >}}
{{< /clients-example >}}

Delete any existing index called `idx:users` and any keys that start with `user:`.

{{< clients-example cs_home_json cleanup_json >}}
{{< /clients-example >}}

Create an index. 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/" >}}).

{{< clients-example cs_home_json make_index >}}
Expand Down Expand Up @@ -109,6 +114,13 @@ in the `FTCreateParams` object 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.

First, delete any existing index called `hash-idx:users` and any keys that start with `huser:`.

{{< clients-example cs_home_json cleanup_hash >}}
{{< /clients-example >}}

Now create the new index:

{{< clients-example cs_home_json make_hash_index >}}
{{< /clients-example >}}

Expand Down
12 changes: 12 additions & 0 deletions content/develop/clients/jedis/queryjson.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ to learn more about the available connection options.
{{< clients-example java_home_json connect >}}
{{< /clients-example >}}

Delete any existing index called `idx:users` and any keys that start with `user:`.

{{< clients-example java_home_json cleanup_json >}}
{{< /clients-example >}}

Create an index. 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/" >}}).

{{< clients-example java_home_json make_index >}}
Expand Down Expand Up @@ -112,6 +117,13 @@ option of `FTCreateParams` 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.

First, delete any existing index called `hash-idx:users` and any keys that start with `huser:`.

{{< clients-example java_home_json cleanup_hash >}}
{{< /clients-example >}}

Now create the new index:

{{< clients-example java_home_json make_hash_index >}}
{{< /clients-example >}}

Expand Down
176 changes: 37 additions & 139 deletions content/develop/clients/nodejs/queryjson.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,42 +40,16 @@ haven't already done so.

Add the following dependencies:

```js
import {
createClient,
SCHEMA_FIELD_TYPE,
FT_AGGREGATE_GROUP_BY_REDUCERS,
FT_AGGREGATE_STEPS,
} from 'redis';
```
{{< clients-example set="js_home_query" step="import" lang_filter="Node.js" >}}
{{< /clients-example >}}

## Create data

Create some test data to add to your database. The example data shown
below is compatible with both JSON and hash objects.

```js
const user1 = {
name: 'Paul John',
email: '[email protected]',
age: 42,
city: 'London'
};

const user2 = {
name: 'Eden Zamir',
email: '[email protected]',
age: 29,
city: 'Tel Aviv'
};

const user3 = {
name: 'Paul Zamir',
email: '[email protected]',
age: 35,
city: 'Tel Aviv'
};
```
{{< clients-example set="js_home_query" step="create_data" lang_filter="Node.js" >}}
{{< /clients-example >}}

## Add the index

Expand All @@ -84,32 +58,21 @@ basic connection but see
[Connect to the server]({{< relref "/develop/clients/nodejs/connect" >}})
to learn more about the available connection options.

```js
const client = await createClient();
await client.connect();
```
{{< clients-example set="js_home_query" step="connect" lang_filter="Node.js" >}}
{{< /clients-example >}}

Create an index. 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/" >}}).

```js
await client.ft.create('idx:users', {
'$.name': {
type: SchemaFieldTypes.TEXT,
AS: 'name'
},
'$.city': {
type: SchemaFieldTypes.TEXT,
AS: 'city'
},
'$.age': {
type: SchemaFieldTypes.NUMERIC,
AS: 'age'
}
}, {
ON: 'JSON',
PREFIX: 'user:'
});
```
First, drop any existing index to avoid a collision. (The callback is required
to avoid an error if the index doesn't already exist.)

{{< clients-example set="js_home_query" step="cleanup_json" lang_filter="Node.js" >}}
{{< /clients-example >}}

Then create the index:

{{< clients-example set="js_home_query" step="create_index" lang_filter="Node.js" >}}
{{< /clients-example >}}

## Add the data

Expand All @@ -121,13 +84,8 @@ the commands in a `Promise.all()` call is an easy way to create a
[pipeline]({{< relref "/develop/clients/nodejs/transpipe" >}}),
which is more efficient than sending the commands individually.

```js
const [user1Reply, user2Reply, user3Reply] = await Promise.all([
client.json.set('user:1', '$', user1),
client.json.set('user:2', '$', user2),
client.json.set('user:3', '$', user3)
]);
```
{{< clients-example set="js_home_query" step="add_data" lang_filter="Node.js" >}}
{{< /clients-example >}}

## Query the data

Expand All @@ -136,58 +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:

```js
let findPaulResult = await client.ft.search('idx:users', 'Paul @age:[30 40]');

console.log(findPaulResult.total); // >>> 1

findPaulResult.documents.forEach(doc => {
console.log(`ID: ${doc.id}, name: ${doc.value.name}, age: ${doc.value.age}`);
});
```
{{< clients-example set="js_home_query" step="query1" lang_filter="Node.js" >}}
{{< /clients-example >}}

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

```js
let citiesResult = await client.ft.search('idx:users', '*',{
RETURN: 'city'
});

console.log(citiesResult.total); // >>> 3

citiesResult.documents.forEach(cityDoc => {
console.log(cityDoc.value);
});
```
{{< clients-example set="js_home_query" step="query2" lang_filter="Node.js" >}}
{{< /clients-example >}}

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

```js
let aggResult = await client.ft.aggregate('idx:users', '*', {
STEPS: [{
type: AggregateSteps.GROUPBY,
properties: '@city',
REDUCE: [{
type: AggregateGroupByReducers.COUNT,
AS: 'count'
}]
}]
});

console.log(aggResult.total); // >>> 2

aggResult.results.forEach(result => {
console.log(`${result.city} - ${result.count}`);
});
```

Finally, close the connection to Redis.

```js
await client.quit();
```
{{< clients-example set="js_home_query" step="query3" lang_filter="Node.js" >}}
{{< /clients-example >}}

## Differences with hash documents

Expand All @@ -201,52 +121,30 @@ 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.

```js
await client.ft.create('hash-idx:users', {
'name': {
type: SchemaFieldTypes.TEXT
},
'city': {
type: SchemaFieldTypes.TEXT
},
'age': {
type: SchemaFieldTypes.NUMERIC
}
}, {
ON: 'HASH',
PREFIX: 'huser:'
});
```
First, drop any existing index to avoid a collision.

{{< clients-example set="js_home_query" step="cleanup_hash" lang_filter="Node.js" >}}
{{< /clients-example >}}

Then create the new index:

{{< clients-example set="js_home_query" step="create_hash_index" lang_filter="Node.js" >}}
{{< /clients-example >}}

You use [`hSet()`]({{< relref "/commands/hset" >}}) to add the hash
documents instead of [`json.set()`]({{< relref "/commands/json.set" >}}),
but the same flat `userX` objects work equally well with either
hash or JSON:

```js
const [huser1Reply, huser2Reply, huser3Reply] = await Promise.all([
client.hSet('huser:1', user1),
client.hSet('huser:2', user2),
client.hSet('huser:3', user3)
]);
```
{{< clients-example set="js_home_query" step="add_hash_data" lang_filter="Node.js" >}}
{{< /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
also the same:

```js
let findPaulHashResult = await client.ft.search(
'hash-idx:users', 'Paul @age:[30 40]'
);

console.log(findPaulHashResult.total); // >>> 1

findPaulHashResult.documents.forEach(doc => {
console.log(`ID: ${doc.id}, name: ${doc.value.name}, age: ${doc.value.age}`);
});
// >>> ID: huser:3, name: Paul Zamir, age: 35
```
{{< clients-example set="js_home_query" step="query1_hash" lang_filter="Node.js" >}}
{{< /clients-example >}}

## More information

Expand Down
Loading