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