@@ -40,42 +40,16 @@ haven't already done so.
4040
4141Add the following dependencies:
4242
43- ``` js
44- import {
45- createClient ,
46- SCHEMA_FIELD_TYPE ,
47- FT_AGGREGATE_GROUP_BY_REDUCERS ,
48- FT_AGGREGATE_STEPS ,
49- } from ' redis' ;
50- ```
43+ {{< clients-example set="js_home_query" step="import" lang_filter="Node.js" >}}
44+ {{< /clients-example >}}
5145
5246## Create data
5347
5448Create some test data to add to your database. The example data shown
5549below is compatible with both JSON and hash objects.
5650
57- ``` js
58- const user1 = {
59- name: ' Paul John' ,
60- 61- age: 42 ,
62- city: ' London'
63- };
64-
65- const user2 = {
66- name: ' Eden Zamir' ,
67- 68- age: 29 ,
69- city: ' Tel Aviv'
70- };
71-
72- const user3 = {
73- name: ' Paul Zamir' ,
74- 75- age: 35 ,
76- city: ' Tel Aviv'
77- };
78- ```
51+ {{< clients-example set="js_home_query" step="create_data" lang_filter="Node.js" >}}
52+ {{< /clients-example >}}
7953
8054## Add the index
8155
@@ -84,32 +58,21 @@ basic connection but see
8458[ Connect to the server] ({{< relref "/develop/clients/nodejs/connect" >}})
8559to learn more about the available connection options.
8660
87- ``` js
88- const client = await createClient ();
89- await client .connect ();
90- ```
61+ {{< clients-example set="js_home_query" step="connect" lang_filter="Node.js" >}}
62+ {{< /clients-example >}}
9163
9264Create 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/" >}}).
9365
94- ``` js
95- await client .ft .create (' idx:users' , {
96- ' $.name' : {
97- type: SchemaFieldTypes .TEXT ,
98- AS : ' name'
99- },
100- ' $.city' : {
101- type: SchemaFieldTypes .TEXT ,
102- AS : ' city'
103- },
104- ' $.age' : {
105- type: SchemaFieldTypes .NUMERIC ,
106- AS : ' age'
107- }
108- }, {
109- ON : ' JSON' ,
110- PREFIX : ' user:'
111- });
112- ```
66+ First, drop any existing index to avoid a collision. (The callback is required
67+ to avoid an error if the index doesn't already exist.)
68+
69+ {{< clients-example set="js_home_query" step="cleanup_json" lang_filter="Node.js" >}}
70+ {{< /clients-example >}}
71+
72+ Then create the index:
73+
74+ {{< clients-example set="js_home_query" step="create_index" lang_filter="Node.js" >}}
75+ {{< /clients-example >}}
11376
11477## Add the data
11578
@@ -121,13 +84,8 @@ the commands in a `Promise.all()` call is an easy way to create a
12184[ pipeline] ({{< relref "/develop/clients/nodejs/transpipe" >}}),
12285which is more efficient than sending the commands individually.
12386
124- ``` js
125- const [user1Reply , user2Reply , user3Reply ] = await Promise .all ([
126- client .json .set (' user:1' , ' $' , user1),
127- client .json .set (' user:2' , ' $' , user2),
128- client .json .set (' user:3' , ' $' , user3)
129- ]);
130- ```
87+ {{< clients-example set="js_home_query" step="add_data" lang_filter="Node.js" >}}
88+ {{< /clients-example >}}
13189
13290## Query the data
13391
@@ -136,58 +94,20 @@ You can now use the index to search the JSON objects. The
13694below searches for objects that have the text "Paul" in any field
13795and have an ` age ` value in the range 30 to 40:
13896
139- ``` js
140- let findPaulResult = await client .ft .search (' idx:users' , ' Paul @age:[30 40]' );
141-
142- console .log (findPaulResult .total ); // >>> 1
143-
144- findPaulResult .documents .forEach (doc => {
145- console .log (` ID: ${ doc .id } , name: ${ doc .value .name } , age: ${ doc .value .age } ` );
146- });
147- ```
97+ {{< clients-example set="js_home_query" step="query1" lang_filter="Node.js" >}}
98+ {{< /clients-example >}}
14899
149100Specify query options to return only the ` city ` field:
150101
151- ``` js
152- let citiesResult = await client .ft .search (' idx:users' , ' *' ,{
153- RETURN : ' city'
154- });
155-
156- console .log (citiesResult .total ); // >>> 3
157-
158- citiesResult .documents .forEach (cityDoc => {
159- console .log (cityDoc .value );
160- });
161- ```
102+ {{< clients-example set="js_home_query" step="query2" lang_filter="Node.js" >}}
103+ {{< /clients-example >}}
162104
163105Use an
164106[ aggregation query] ({{< relref "/develop/ai/search-and-query/query/aggregation" >}})
165107to count all users in each city.
166108
167- ``` js
168- let aggResult = await client .ft .aggregate (' idx:users' , ' *' , {
169- STEPS : [{
170- type: AggregateSteps .GROUPBY ,
171- properties: ' @city' ,
172- REDUCE : [{
173- type: AggregateGroupByReducers .COUNT ,
174- AS : ' count'
175- }]
176- }]
177- });
178-
179- console .log (aggResult .total ); // >>> 2
180-
181- aggResult .results .forEach (result => {
182- console .log (` ${ result .city } - ${ result .count } ` );
183- });
184- ```
185-
186- Finally, close the connection to Redis.
187-
188- ``` js
189- await client .quit ();
190- ```
109+ {{< clients-example set="js_home_query" step="query3" lang_filter="Node.js" >}}
110+ {{< /clients-example >}}
191111
192112## Differences with hash documents
193113
@@ -201,52 +121,30 @@ when you create the index. The code below shows these changes with
201121a new index called ` hash-idx:users ` , which is otherwise the same as
202122the ` idx:users ` index used for JSON documents in the previous examples.
203123
204- ``` js
205- await client .ft .create (' hash-idx:users' , {
206- ' name' : {
207- type: SchemaFieldTypes .TEXT
208- },
209- ' city' : {
210- type: SchemaFieldTypes .TEXT
211- },
212- ' age' : {
213- type: SchemaFieldTypes .NUMERIC
214- }
215- }, {
216- ON : ' HASH' ,
217- PREFIX : ' huser:'
218- });
219- ```
124+ First, drop any existing index to avoid a collision.
125+
126+ {{< clients-example set="js_home_query" step="cleanup_hash" lang_filter="Node.js" >}}
127+ {{< /clients-example >}}
128+
129+ Then create the new index:
130+
131+ {{< clients-example set="js_home_query" step="create_hash_index" lang_filter="Node.js" >}}
132+ {{< /clients-example >}}
220133
221134You use [ ` hSet() ` ] ({{< relref "/commands/hset" >}}) to add the hash
222135documents instead of [ ` json.set() ` ] ({{< relref "/commands/json.set" >}}),
223136but the same flat ` userX ` objects work equally well with either
224137hash or JSON:
225138
226- ``` js
227- const [huser1Reply , huser2Reply , huser3Reply ] = await Promise .all ([
228- client .hSet (' huser:1' , user1),
229- client .hSet (' huser:2' , user2),
230- client .hSet (' huser:3' , user3)
231- ]);
232- ```
139+ {{< clients-example set="js_home_query" step="add_hash_data" lang_filter="Node.js" >}}
140+ {{< /clients-example >}}
233141
234142The query commands work the same here for hash as they do for JSON (but
235143the name of the hash index is different). The format of the result is
236144also the same:
237145
238- ``` js
239- let findPaulHashResult = await client .ft .search (
240- ' hash-idx:users' , ' Paul @age:[30 40]'
241- );
242-
243- console .log (findPaulHashResult .total ); // >>> 1
244-
245- findPaulHashResult .documents .forEach (doc => {
246- console .log (` ID: ${ doc .id } , name: ${ doc .value .name } , age: ${ doc .value .age } ` );
247- });
248- // >>> ID: huser:3, name: Paul Zamir, age: 35
249- ```
146+ {{< clients-example set="js_home_query" step="query1_hash" lang_filter="Node.js" >}}
147+ {{< /clients-example >}}
250148
251149## More information
252150
0 commit comments