|
| 1 | +// EXAMPLE: query_ft |
| 2 | +// HIDE_START |
| 3 | +import assert from 'assert'; |
| 4 | +import fs from 'fs'; |
| 5 | +import { createClient, SchemaFieldTypes, AggregateGroupByReducers, AggregateSteps} from 'redis'; |
| 6 | + |
| 7 | +const client = createClient(); |
| 8 | + |
| 9 | +await client.connect(); |
| 10 | + |
| 11 | +// create index |
| 12 | +await client.ft.create('idx:bicycle', { |
| 13 | + '$.model': { |
| 14 | + type: SchemaFieldTypes.TEXT, |
| 15 | + AS: 'model' |
| 16 | + }, |
| 17 | + '$.brand': { |
| 18 | + type: SchemaFieldTypes.TEXT, |
| 19 | + AS: 'brand' |
| 20 | + }, |
| 21 | + '$.description': { |
| 22 | + type: SchemaFieldTypes.TEXT, |
| 23 | + AS: 'description' |
| 24 | + } |
| 25 | +}, { |
| 26 | + ON: 'JSON', |
| 27 | + PREFIX: 'bicycle:' |
| 28 | +}) |
| 29 | + |
| 30 | +// load data |
| 31 | +const bicycles = JSON.parse(fs.readFileSync('data/query_em.json', 'utf8')); |
| 32 | + |
| 33 | +await Promise.all( |
| 34 | + bicycles.map((bicycle, bid) => { |
| 35 | + return client.json.set(`bicycle:${bid}`, '$', bicycle); |
| 36 | + }) |
| 37 | +); |
| 38 | +// HIDE_END |
| 39 | + |
| 40 | +// STEP_START ft1 |
| 41 | +let res = await client.ft.search('idx:bicycle', '@description: kids'); |
| 42 | +console.log(res.total); // >>> 2 |
| 43 | +// REMOVE_START |
| 44 | +assert.strictEqual(res.total, 2); |
| 45 | +// REMOVE_END |
| 46 | +// STEP_END |
| 47 | + |
| 48 | +// STEP_START ft2 |
| 49 | +res = await client.ft.search('idx:bicycle', '@model: ka*'); |
| 50 | +console.log(res.total); // >>> 1 |
| 51 | +// REMOVE_START |
| 52 | +assert.strictEqual(res.total, 1); |
| 53 | +// REMOVE_END |
| 54 | +// STEP_END |
| 55 | + |
| 56 | +// STEP_START ft3 |
| 57 | +res = await client.ft.search('idx:bicycle', '@brand: *bikes'); |
| 58 | +console.log(res.total); // >>> 2 |
| 59 | +// REMOVE_START |
| 60 | +assert.strictEqual(res.total, 2); |
| 61 | +// REMOVE_END |
| 62 | +// STEP_END |
| 63 | + |
| 64 | +// STEP_START ft4 |
| 65 | +res = await client.ft.search('idx:bicycle', '%optamized%'); |
| 66 | +console.log(res); // >>> { total: 1, documents: [ { id: 'bicycle:3', value: [Object: null prototype] } ]} |
| 67 | +// REMOVE_START |
| 68 | +assert.strictEqual(res.total, 1); |
| 69 | +// REMOVE_END |
| 70 | +// STEP_END |
| 71 | + |
| 72 | +// STEP_START ft5 |
| 73 | +res = await client.ft.search('idx:bicycle', '%%optamised%%'); |
| 74 | +console.log(res); // >>> { total: 1, documents: [ { id: 'bicycle:3', value: [Object: null prototype] } ]} |
| 75 | +// REMOVE_START |
| 76 | +assert.strictEqual(res.total, 1); |
| 77 | +// REMOVE_END |
| 78 | +// STEP_END |
| 79 | + |
| 80 | +// REMOVE_START |
| 81 | +// destroy index and data |
| 82 | +await client.ft.dropIndex('idx:bicycle', { DD: true }); |
| 83 | +await client.disconnect(); |
| 84 | +// REMOVE_END |
0 commit comments