Skip to content

Commit 8256698

Browse files
committed
Add search-quickstart
1 parent a533a68 commit 8256698

1 file changed

Lines changed: 234 additions & 0 deletions

File tree

doctests/search-quickstart.js

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
// EXAMPLE: search_quickstart
2+
// REMOVE_START
3+
import assert from "assert";
4+
// REMOVE_END
5+
// HIDE_START
6+
import {AggregateGroupByReducers, AggregateSteps, createClient, SchemaFieldTypes} from 'redis';
7+
8+
const client = createClient();
9+
client.on('error', err => console.log('Redis Client Error', err));
10+
11+
await client.connect();
12+
13+
// HIDE_END
14+
// STEP_START data_sample
15+
let bicycle1 = {
16+
"brand": "Diaz Ltd",
17+
"model": "Dealer Sl",
18+
"price": 7315.58,
19+
"description": "The Diaz Ltd Dealer Sl is a reliable choice" +
20+
" for urban cycling. The Diaz Ltd Dealer Sl " +
21+
"is a comfortable choice for urban cycling.",
22+
"condition": "used",
23+
}
24+
// STEP_END
25+
let bicycles = [
26+
bicycle1,
27+
{
28+
"brand": "Bridges Group",
29+
"model": "Project Pro",
30+
"price": 3610.82,
31+
"description": "This mountain bike is perfect for mountain biking. The Bridges Group Project Pro is a responsive choice for mountain biking.",
32+
"condition": "used",
33+
},
34+
{
35+
"brand": "Vega, Cole and Miller",
36+
"model": "Group Advanced",
37+
"price": 8961.42,
38+
"description": "The Vega, Cole and Miller Group Advanced provides a excellent ride. With its fast carbon frame and 24 gears, this bicycle is perfect for any terrain.",
39+
"condition": "used",
40+
},
41+
{
42+
"brand": "Powell-Montgomery",
43+
"model": "Angle Race",
44+
"price": 4050.27,
45+
"description": "The Powell-Montgomery Angle Race is a smooth choice for road cycling. The Powell-Montgomery Angle Race provides a durable ride.",
46+
"condition": "used",
47+
},
48+
{
49+
"brand": "Gill-Lewis",
50+
"model": "Action Evo",
51+
"price": 283.68,
52+
"description": "The Gill-Lewis Action Evo provides a smooth ride. The Gill-Lewis Action Evo provides a excellent ride.",
53+
"condition": "used",
54+
},
55+
{
56+
"brand": "Rodriguez-Guerrero",
57+
"model": "Drama Comp",
58+
"price": 4462.55,
59+
"description": "This kids bike is perfect for young riders. With its excellent aluminum frame and 12 gears, this bicycle is perfect for any terrain.",
60+
"condition": "new",
61+
},
62+
{
63+
"brand": "Moore PLC",
64+
"model": "Award Race",
65+
"price": 3790.76,
66+
"description": "This olive folding bike features a carbon frame and 27.5 inch wheels. This folding bike is perfect for compact storage and transportation.",
67+
"condition": "new",
68+
},
69+
{
70+
"brand": "Hall, Haley and Hayes",
71+
"model": "Weekend Plus",
72+
"price": 2008.4,
73+
"description": "The Hall, Haley and Hayes Weekend Plus provides a comfortable ride. This blue kids bike features a steel frame and 29.0 inch wheels.",
74+
"condition": "new",
75+
},
76+
{
77+
"brand": "Peck-Carson",
78+
"model": "Sun Hybrid",
79+
"price": 9874.95,
80+
"description": "With its comfortable aluminum frame and 25 gears, this bicycle is perfect for any terrain. The Peck-Carson Sun Hybrid provides a comfortable ride.",
81+
"condition": "new",
82+
},
83+
{
84+
"brand": "Fowler Ltd",
85+
"model": "Weekend Trail",
86+
"price": 3833.71,
87+
"description": "The Fowler Ltd Letter Trail is a comfortable choice for transporting cargo. This cargo bike is perfect for transporting cargo.",
88+
"condition": "refurbished",
89+
},
90+
]
91+
// STEP_START define_index
92+
try {
93+
await client.ft.create('idx:bicycle', {
94+
'$.brand': {
95+
type: SchemaFieldTypes.TEXT,
96+
sortable: true,
97+
AS: 'brand'
98+
},
99+
'$.model': {
100+
type: SchemaFieldTypes.TEXT,
101+
AS: 'model'
102+
},
103+
'$.description': {
104+
type: SchemaFieldTypes.TEXT,
105+
AS: 'description'
106+
},
107+
'$.price': {
108+
type: SchemaFieldTypes.NUMERIC,
109+
AS: 'price'
110+
},
111+
'$.condition': {
112+
type: SchemaFieldTypes.TAG,
113+
AS: 'condition'
114+
},
115+
}, {
116+
ON: 'JSON',
117+
PREFIX: 'bicycle:'
118+
});
119+
} catch (e) {
120+
if (e.message === 'Index already exists') {
121+
console.log('Index exists already, skipped creation.');
122+
} else {
123+
// Something went wrong, perhaps RediSearch isn't installed...
124+
console.error(e);
125+
process.exit(1);
126+
}
127+
}
128+
// STEP_END
129+
130+
// STEP_START add_documents
131+
for (let i = 0; i < bicycles.length; i++) {
132+
await client.json.set(`bicycle:${i}`, '$', bicycles[i]);
133+
}
134+
// STEP_END
135+
136+
// STEP_START query_single_term_and_num_range
137+
let result = await client.ft.search(
138+
'idx:bicycle',
139+
'folding @price:[1000 4000]'
140+
);
141+
142+
console.log(JSON.stringify(result, null, 2));
143+
/*
144+
{
145+
"total": 1,
146+
"documents": [
147+
{
148+
"id": "bicycle:6",
149+
"value": {
150+
"brand": "Moore PLC",
151+
"model": "Award Race",
152+
"price": 3790.76,
153+
"description": "This olive folding bike features a carbon frame and 27.5 inch wheels. This folding bike is perfect for compact storage and transportation.",
154+
"condition": "new"
155+
}
156+
}
157+
]
158+
}
159+
*/
160+
// STEP_END
161+
// REMOVE_START
162+
assert.equal(result.documents[0].id, "bicycle:6");
163+
// REMOVE_END
164+
165+
// STEP_START query_single_term_limit_fields
166+
result = await client.ft.search(
167+
'idx:bicycle',
168+
'cargo',
169+
{
170+
RETURN: ['$.price']
171+
}
172+
);
173+
174+
console.log(JSON.stringify(result, null, 2));
175+
/*
176+
{
177+
"total": 1,
178+
"documents": [
179+
{
180+
"id": "bicycle:9",
181+
"value": {
182+
"$.price": "3833.71"
183+
}
184+
}
185+
]
186+
}
187+
*/
188+
// STEP_END
189+
// REMOVE_START
190+
assert.equal(result.documents[0].id, "bicycle:9");
191+
// REMOVE_END
192+
193+
// STEP_START simple_aggregation
194+
result = await client.ft.aggregate('idx:bicycle', '*', {
195+
STEPS: [
196+
{
197+
type: AggregateSteps.GROUPBY,
198+
properties: ['@condition'],
199+
REDUCE: [
200+
{
201+
type: AggregateGroupByReducers.COUNT,
202+
AS: 'count'
203+
}
204+
]
205+
}
206+
]
207+
})
208+
209+
console.log(JSON.stringify(result, null, 2));
210+
/*
211+
{
212+
"total": 3,
213+
"results": [
214+
{
215+
"condition": "refurbished",
216+
"count": "1"
217+
},
218+
{
219+
"condition": "used",
220+
"count": "5"
221+
},
222+
{
223+
"condition": "new",
224+
"count": "4"
225+
}
226+
]
227+
}
228+
*/
229+
// STEP_END
230+
// REMOVE_START
231+
assert.equal(result.total, 3);
232+
// REMOVE_END
233+
234+
await client.quit();

0 commit comments

Comments
 (0)