-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathquery-agg.js
139 lines (128 loc) · 3.78 KB
/
query-agg.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// EXAMPLE: query_agg
// HIDE_START
import assert from 'node:assert';
import fs from 'node:fs';
import { createClient } from 'redis';
import { SchemaFieldTypes, AggregateSteps, AggregateGroupByReducers } from '@redis/search';
const client = createClient();
await client.connect().catch(console.error);
// create index
await client.ft.create('idx:bicycle', {
'$.condition': {
type: SchemaFieldTypes.TAG,
AS: 'condition'
},
'$.price': {
type: SchemaFieldTypes.NUMERIC,
AS: 'price'
}
}, {
ON: 'JSON',
PREFIX: 'bicycle:'
})
// load data
const bicycles = JSON.parse(fs.readFileSync('data/query_em.json', 'utf8'));
await Promise.all(
bicycles.map((bicycle, bid) => {
return client.json.set(`bicycle:${bid}`, '$', bicycle);
})
);
// HIDE_END
// STEP_START agg1
const res1 = await client.ft.aggregate('idx:bicycle', '@condition:{new}', {
LOAD: ['__key', 'price'],
APPLY: {
expression: '@price - (@price * 0.1)',
AS: 'discounted'
}
});
console.log(res1.results.length); // >>> 5
console.log(res1.results); // >>>
//[
// [Object: null prototype] { __key: 'bicycle:0', price: '270' },
// [Object: null prototype] { __key: 'bicycle:5', price: '810' },
// [Object: null prototype] { __key: 'bicycle:6', price: '2300' },
// [Object: null prototype] { __key: 'bicycle:7', price: '430' },
// [Object: null prototype] { __key: 'bicycle:8', price: '1200' }
//]
// REMOVE_START
assert.strictEqual(res1.results.length, 5);
// REMOVE_END
// STEP_END
// STEP_START agg2
const res2 = await client.ft.aggregate('idx:bicycle', '*', {
LOAD: ['@price'],
STEPS: [{
type: AggregateSteps.APPLY,
expression: '@price<1000',
AS: 'price_category'
},{
type: AggregateSteps.GROUPBY,
properties: '@condition',
REDUCE:[{
type: AggregateGroupByReducers.SUM,
property: '@price_category',
AS: 'num_affordable'
}]
}]
});
console.log(res2.results.length); // >>> 3
console.log(res2.results); // >>>
//[[Object: null prototype] { condition: 'refurbished', num_affordable: '1' },
// [Object: null prototype] { condition: 'used', num_affordable: '1' },
// [Object: null prototype] { condition: 'new', num_affordable: '3' }
//]
// REMOVE_START
assert.strictEqual(res2.results.length, 3);
// REMOVE_END
// STEP_END
// STEP_START agg3
const res3 = await client.ft.aggregate('idx:bicycle', '*', {
STEPS: [{
type: AggregateSteps.APPLY,
expression: "'bicycle'",
AS: 'type'
}, {
type: AggregateSteps.GROUPBY,
properties: '@type',
REDUCE: [{
type: AggregateGroupByReducers.COUNT,
property: null,
AS: 'num_total'
}]
}]
});
console.log(res3.results.length); // >>> 1
console.log(res3.results); // >>>
//[ [Object: null prototype] { type: 'bicycle', num_total: '10' } ]
// REMOVE_START
assert.strictEqual(res3.results.length, 1);
// REMOVE_END
// STEP_END
// STEP_START agg4
const res4 = await client.ft.aggregate('idx:bicycle', '*', {
LOAD: ['__key'],
STEPS: [{
type: AggregateSteps.GROUPBY,
properties: '@condition',
REDUCE: [{
type: AggregateGroupByReducers.TOLIST,
property: '__key',
AS: 'bicycles'
}]
}]
});
console.log(res4.results.length); // >>> 3
console.log(res4.results); // >>>
//[[Object: null prototype] {condition: 'refurbished', bicycles: [ 'bicycle:9' ]},
// [Object: null prototype] {condition: 'used', bicycles: [ 'bicycle:1', 'bicycle:2', 'bicycle:3', 'bicycle:4' ]},
// [Object: null prototype] {condition: 'new', bicycles: [ 'bicycle:5', 'bicycle:6', 'bicycle:7', 'bicycle:0', 'bicycle:8' ]}]
// REMOVE_START
assert.strictEqual(res4.results.length, 3);
// REMOVE_END
// STEP_END
// REMOVE_START
// destroy index and data
await client.ft.dropIndex('idx:bicycle', { DD: true });
await client.disconnect();
// REMOVE_END