|
| 1 | +// This example demonstrates the use of the Top K |
| 2 | +// in the RedisBloom module (https://redisbloom.io/) |
| 3 | + |
| 4 | +import { createClient } from 'redis'; |
| 5 | + |
| 6 | +async function topK() { |
| 7 | + const client = createClient(); |
| 8 | + |
| 9 | + await client.connect(); |
| 10 | + |
| 11 | + // Delete any pre-existing Top K. |
| 12 | + await client.del('mytopk'); |
| 13 | + |
| 14 | + // Reserve a Top K to track the 10 most common items. |
| 15 | + // https://oss.redis.com/redisbloom/TopK_Commands/#topkreserve |
| 16 | + try { |
| 17 | + await client.topK.reserve('mytopk', 10); |
| 18 | + console.log('Reserved Top K.'); |
| 19 | + } catch (e) { |
| 20 | + if (e.message.endsWith('key already exists')) { |
| 21 | + console.log('Top K already reserved.'); |
| 22 | + } else { |
| 23 | + console.log('Error, maybe RedisBloom is not installed?:'); |
| 24 | + console.log(e); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + const teamMembers = [ |
| 29 | + 'leibale', |
| 30 | + 'simon', |
| 31 | + 'guy', |
| 32 | + 'suze', |
| 33 | + 'brian', |
| 34 | + 'steve', |
| 35 | + 'kyleb', |
| 36 | + 'kyleo', |
| 37 | + 'josefin', |
| 38 | + 'alex', |
| 39 | + 'nava', |
| 40 | + 'lance', |
| 41 | + 'rachel', |
| 42 | + 'kaitlyn' |
| 43 | + ]; |
| 44 | + |
| 45 | + // Add random counts for random team members with TOPK.INCRBY |
| 46 | + for (let n = 0; n < 1000; n++) { |
| 47 | + const teamMember = teamMembers[Math.floor(Math.random() * teamMembers.length)]; |
| 48 | + const points = Math.floor(Math.random() * 1000) + 1; |
| 49 | + await client.topK.incrBy('mytopk', { |
| 50 | + item: teamMember, |
| 51 | + incrementBy: points |
| 52 | + }); |
| 53 | + console.log(`Added ${points} points for ${teamMember}.`); |
| 54 | + } |
| 55 | + |
| 56 | + // List out the top 10 with TOPK.LIST |
| 57 | + const top10 = await client.topK.list('mytopk'); |
| 58 | + console.log('The top 10:'); |
| 59 | + // top10 looks like this: |
| 60 | + // [ |
| 61 | + // 'guy', 'nava', |
| 62 | + // 'kaitlyn', 'brian', |
| 63 | + // 'simon', 'suze', |
| 64 | + // 'lance', 'alex', |
| 65 | + // 'steve', 'kyleo' |
| 66 | + // ] |
| 67 | + console.log(top10); |
| 68 | + |
| 69 | + // Check if a few team members are in the top 10 with TOPK.QUERY: |
| 70 | + const [ steve, suze, leibale, frederick ] = await client.topK.query('mytopk', [ |
| 71 | + 'steve', |
| 72 | + 'suze', |
| 73 | + 'leibale', |
| 74 | + 'frederick' |
| 75 | + ]); |
| 76 | + |
| 77 | + console.log(`steve ${steve === 1 ? 'is': 'is not'} in the top 10.`); |
| 78 | + console.log(`suze ${suze === 1 ? 'is': 'is not'} in the top 10.`); |
| 79 | + console.log(`leibale ${leibale === 1 ? 'is': 'is not'} in the top 10.`); |
| 80 | + console.log(`frederick ${frederick === 1 ? 'is': 'is not'} in the top 10.`); |
| 81 | + |
| 82 | + // Get count estimate for some team members: |
| 83 | + const [ simonCount, lanceCount ] = await client.topK.count('mytopk', [ |
| 84 | + 'simon', |
| 85 | + 'lance' |
| 86 | + ]); |
| 87 | + |
| 88 | + console.log(`Count estimate for simon: ${simonCount}.`); |
| 89 | + console.log(`Count estimate for lance: ${lanceCount}.`); |
| 90 | + |
| 91 | + await client.quit(); |
| 92 | +} |
| 93 | + |
| 94 | +topK(); |
0 commit comments