Skip to content

Commit 2d5e2ba

Browse files
docs: DOC-5841 added index/query doc page examples (#3109)
1 parent 5763805 commit 2d5e2ba

File tree

1 file changed

+205
-0
lines changed

1 file changed

+205
-0
lines changed

doctests/home-query.js

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
// EXAMPLE: js_home_query
2+
// BINDER_ID nodejs-js_home_query
3+
// REMOVE_START
4+
import assert from "node:assert";
5+
// REMOVE_END
6+
// STEP_START import
7+
import {
8+
createClient,
9+
SCHEMA_FIELD_TYPE,
10+
FT_AGGREGATE_GROUP_BY_REDUCERS,
11+
FT_AGGREGATE_STEPS,
12+
} from 'redis';
13+
// STEP_END
14+
15+
// STEP_START create_data
16+
const user1 = {
17+
name: 'Paul John',
18+
email: 'paul.john@example.com',
19+
age: 42,
20+
city: 'London'
21+
};
22+
23+
const user2 = {
24+
name: 'Eden Zamir',
25+
email: 'eden.zamir@example.com',
26+
age: 29,
27+
city: 'Tel Aviv'
28+
};
29+
30+
const user3 = {
31+
name: 'Paul Zamir',
32+
email: 'paul.zamir@example.com',
33+
age: 35,
34+
city: 'Tel Aviv'
35+
};
36+
// STEP_END
37+
38+
// STEP_START connect
39+
const client = await createClient();
40+
await client.connect();
41+
// STEP_END
42+
43+
// STEP_START cleanup_json
44+
await client.ft.dropIndex('idx:users', { DD: true }).then(() => {}, () => {});
45+
// STEP_END
46+
47+
// STEP_START create_index
48+
await client.ft.create('idx:users', {
49+
'$.name': {
50+
type: SCHEMA_FIELD_TYPE.TEXT,
51+
AS: 'name'
52+
},
53+
'$.city': {
54+
type: SCHEMA_FIELD_TYPE.TEXT,
55+
AS: 'city'
56+
},
57+
'$.age': {
58+
type: SCHEMA_FIELD_TYPE.NUMERIC,
59+
AS: 'age'
60+
}
61+
}, {
62+
ON: 'JSON',
63+
PREFIX: 'user:'
64+
});
65+
// STEP_END
66+
67+
// STEP_START add_data
68+
const [user1Reply, user2Reply, user3Reply] = await Promise.all([
69+
client.json.set('user:1', '$', user1),
70+
client.json.set('user:2', '$', user2),
71+
client.json.set('user:3', '$', user3)
72+
]);
73+
// STEP_END
74+
// REMOVE_START
75+
assert.equal(user1Reply, 'OK');
76+
assert.equal(user2Reply, 'OK');
77+
assert.equal(user3Reply, 'OK');
78+
// REMOVE_END
79+
80+
// STEP_START query1
81+
let findPaulResult = await client.ft.search('idx:users', 'Paul @age:[30 40]');
82+
83+
console.log(findPaulResult.total); // >>> 1
84+
85+
findPaulResult.documents.forEach(doc => {
86+
console.log(`ID: ${doc.id}, name: ${doc.value.name}, age: ${doc.value.age}`);
87+
});
88+
// >>> ID: user:3, name: Paul Zamir, age: 35
89+
// STEP_END
90+
// REMOVE_START
91+
assert.equal(findPaulResult.total, 1);
92+
assert.equal(findPaulResult.documents[0].id, 'user:3');
93+
// REMOVE_END
94+
95+
// STEP_START query2
96+
let citiesResult = await client.ft.search('idx:users', '*',{
97+
RETURN: 'city'
98+
});
99+
100+
console.log(citiesResult.total); // >>> 3
101+
102+
citiesResult.documents.forEach(cityDoc => {
103+
console.log(cityDoc.value);
104+
});
105+
// >>> { city: 'London' }
106+
// >>> { city: 'Tel Aviv' }
107+
// >>> { city: 'Tel Aviv' }
108+
// STEP_END
109+
// REMOVE_START
110+
assert.equal(citiesResult.total, 3);
111+
citiesResult.documents.sort((a, b) => a.value.city.localeCompare(b.value.city));
112+
assert.deepEqual(citiesResult.documents.map(doc => doc.value.city), [
113+
'London',
114+
'Tel Aviv',
115+
'Tel Aviv'
116+
]);
117+
// REMOVE_END
118+
119+
// STEP_START query3
120+
let aggResult = await client.ft.aggregate('idx:users', '*', {
121+
STEPS: [{
122+
type: FT_AGGREGATE_STEPS.GROUPBY,
123+
properties: '@city',
124+
REDUCE: [{
125+
type: FT_AGGREGATE_GROUP_BY_REDUCERS.COUNT,
126+
AS: 'count'
127+
}]
128+
}]
129+
});
130+
131+
console.log(aggResult.total); // >>> 2
132+
133+
aggResult.results.forEach(result => {
134+
console.log(`${result.city} - ${result.count}`);
135+
});
136+
// >>> London - 1
137+
// >>> Tel Aviv - 2
138+
// STEP_END
139+
// REMOVE_START
140+
assert.equal(aggResult.total, 2);
141+
aggResult.results.sort((a, b) => a.city.localeCompare(b.city));
142+
assert.deepEqual(aggResult.results.map(result => result.city), [
143+
'London',
144+
'Tel Aviv'
145+
]);
146+
assert.deepEqual(aggResult.results.map(result => result.count), [
147+
1,
148+
2
149+
]);
150+
// REMOVE_END
151+
152+
// STEP_START cleanup_hash
153+
await client.ft.dropIndex('hash-idx:users', { DD: true }).then(() => {}, () => {});
154+
// STEP_END
155+
156+
// STEP_START create_hash_index
157+
await client.ft.create('hash-idx:users', {
158+
'name': {
159+
type: SCHEMA_FIELD_TYPE.TEXT
160+
},
161+
'city': {
162+
type: SCHEMA_FIELD_TYPE.TEXT
163+
},
164+
'age': {
165+
type: SCHEMA_FIELD_TYPE.NUMERIC
166+
}
167+
}, {
168+
ON: 'HASH',
169+
PREFIX: 'huser:'
170+
});
171+
// STEP_END
172+
173+
// STEP_START add_hash_data
174+
const [huser1Reply, huser2Reply, huser3Reply] = await Promise.all([
175+
client.hSet('huser:1', user1),
176+
client.hSet('huser:2', user2),
177+
client.hSet('huser:3', user3)
178+
]);
179+
// STEP_END
180+
// REMOVE_START
181+
assert.equal(huser1Reply, 4);
182+
assert.equal(huser2Reply, 4);
183+
assert.equal(huser3Reply, 4);
184+
// REMOVE_END
185+
186+
// STEP_START query1_hash
187+
let findPaulHashResult = await client.ft.search(
188+
'hash-idx:users', 'Paul @age:[30 40]'
189+
);
190+
191+
console.log(findPaulHashResult.total); // >>> 1
192+
193+
findPaulHashResult.documents.forEach(doc => {
194+
console.log(`ID: ${doc.id}, name: ${doc.value.name}, age: ${doc.value.age}`);
195+
});
196+
// >>> ID: huser:3, name: Paul Zamir, age: 35
197+
// STEP_END
198+
// REMOVE_START
199+
assert.equal(findPaulHashResult.total, 1);
200+
assert.equal(findPaulHashResult.documents[0].id, 'huser:3');
201+
// REMOVE_END
202+
203+
// STEP_START close
204+
await client.quit();
205+
// STEP_END

0 commit comments

Comments
 (0)