Skip to content

Commit e404d79

Browse files
DOC-5108 added JavaScript hash search examples
1 parent 3eecd96 commit e404d79

File tree

1 file changed

+93
-14
lines changed

1 file changed

+93
-14
lines changed

content/develop/clients/nodejs/queryjson.md

Lines changed: 93 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,27 @@ categories:
99
- oss
1010
- kubernetes
1111
- 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
1515
weight: 2
1616
---
1717

1818
This example shows how to create a
1919
[search index]({{< relref "/develop/interact/search-and-query/indexing" >}})
20-
for [JSON]({{< relref "/develop/data-types/json" >}}) data and
21-
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.
2224

23-
Make sure that you have Redis Stack and `node-redis` installed.
25+
## Initialize
2426

25-
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+
[`node-redis`]({{< relref "/develop/clients/nodejs" >}}) client library if you
30+
haven't already done so.
31+
32+
Add the following dependencies:
2633

2734
```js
2835
import {
@@ -33,14 +40,10 @@ import {
3340
} from 'redis';
3441
```
3542

36-
Connect to the database:
37-
38-
```js
39-
const client = await createClient();
40-
await client.connect();
41-
```
43+
## Create data
4244

43-
Create some test data to add to the database:
45+
Create some test data to add to your database. The example data shown
46+
below is compatible with both JSON and hash objects.
4447

4548
```js
4649
const user1 = {
@@ -65,6 +68,18 @@ const user3 = {
6568
};
6669
```
6770

71+
## Add the index
72+
73+
Connect to your Redis database. The code below shows the most
74+
basic connection but see
75+
[Connect to the server]({{< relref "/develop/clients/nodejs/connect" >}})
76+
to learn more about the available connection options.
77+
78+
```js
79+
const client = await createClient();
80+
await client.connect();
81+
```
82+
6883
Create an index. In this example, only JSON documents with the key prefix `user:` are indexed. For more information, see [Query syntax]({{< relref "/develop/interact/search-and-query/query/" >}}).
6984

7085
```js
@@ -87,6 +102,8 @@ await client.ft.create('idx:users', {
87102
});
88103
```
89104

105+
## Add the data
106+
90107
Add the three sets of user data to the database as
91108
[JSON]({{< relref "/develop/data-types/json" >}}) objects.
92109
If you use keys with the `user:` prefix then Redis will index the
@@ -103,6 +120,8 @@ const [user1Reply, user2Reply, user3Reply] = await Promise.all([
103120
]);
104121
```
105122

123+
## Query the data
124+
106125
You can now use the index to search the JSON objects. The
107126
[query]({{< relref "/develop/interact/search-and-query/query" >}})
108127
below searches for objects that have the text "Paul" in any field
@@ -161,6 +180,66 @@ Finally, close the connection to Redis.
161180
await client.quit();
162181
```
163182

183+
## Differences with hash documents
184+
185+
Indexing for hash documents is very similar to JSON indexing but you
186+
need to specify some slightly different options.
187+
188+
When you create the schema for a hash index, you don't need to
189+
add aliases for the fields, since you use the basic names to access
190+
the fields anyway. Also, you must use `HASH` for the `ON` option
191+
when you create the index. The code below shows these changes with
192+
a new index called `hash-idx:users`, which is otherwise the same as
193+
the `idx:users` index used for JSON documents in the previous examples.
194+
195+
```js
196+
await client.ft.create('hash-idx:users', {
197+
'name': {
198+
type: SchemaFieldTypes.TEXT
199+
},
200+
'city': {
201+
type: SchemaFieldTypes.TEXT
202+
},
203+
'age': {
204+
type: SchemaFieldTypes.NUMERIC
205+
}
206+
}, {
207+
ON: 'HASH',
208+
PREFIX: 'huser:'
209+
});
210+
```
211+
212+
You use [`hSet()`]({{< relref "/commands/hset" >}}) to add the hash
213+
documents instead of [`json.set()`]({{< relref "/commands/json.set" >}}),
214+
but the same flat `userX` objects work equally well with either
215+
hash or JSON:
216+
217+
```js
218+
const [huser1Reply, huser2Reply, huser3Reply] = await Promise.all([
219+
client.hSet('huser:1', user1),
220+
client.hSet('huser:2', user2),
221+
client.hSet('huser:3', user3)
222+
]);
223+
```
224+
225+
The query commands work the same here for hash as they do for JSON (but
226+
the name of the hash index is different). The format of the result is
227+
also the same:
228+
229+
```js
230+
let findPaulHashResult = await client.ft.search(
231+
'hash-idx:users', 'Paul @age:[30 40]'
232+
);
233+
234+
console.log(findPaulHashResult.total); // >>> 1
235+
236+
findPaulHashResult.documents.forEach(doc => {
237+
console.log(`ID: ${doc.id}, name: ${doc.value.name}, age: ${doc.value.age}`);
238+
});
239+
// >>> ID: huser:3, name: Paul Zamir, age: 35
240+
```
241+
242+
## More information
164243

165244
See the [Redis Query Engine]({{< relref "/develop/interact/search-and-query" >}}) docs
166245
for a full description of all query features with examples.

0 commit comments

Comments
 (0)