|
| 1 | +// This example demonstrates how to use RediSearch to index and query data |
| 2 | +// stored in Redis hashes using vector similarity search. |
| 3 | +// |
| 4 | +// Inspired by RediSearch Python tests: |
| 5 | +// https://github.com/RediSearch/RediSearch/blob/06e36d48946ea08bd0d8b76394a4e82eeb919d78/tests/pytests/test_vecsim.py#L96 |
| 6 | + |
| 7 | +import { createClient, SchemaFieldTypes, VectorAlgorithms } from 'redis'; |
| 8 | + |
| 9 | +const client = createClient(); |
| 10 | + |
| 11 | +await client.connect(); |
| 12 | + |
| 13 | +// Create an index... |
| 14 | +try { |
| 15 | + // Documentation: https://redis.io/docs/stack/search/reference/vectors/ |
| 16 | + await client.ft.create('idx:knn-example', { |
| 17 | + v: { |
| 18 | + type: SchemaFieldTypes.VECTOR, |
| 19 | + ALGORITHM: VectorAlgorithms.HNSW, |
| 20 | + TYPE: 'FLOAT32', |
| 21 | + DIM: 2, |
| 22 | + DISTANCE_METRIC: 'COSINE' |
| 23 | + } |
| 24 | + }, { |
| 25 | + ON: 'HASH', |
| 26 | + PREFIX: 'noderedis:knn' |
| 27 | + }); |
| 28 | +} catch (e) { |
| 29 | + if (e.message === 'Index already exists') { |
| 30 | + console.log('Index exists already, skipped creation.'); |
| 31 | + } else { |
| 32 | + // Something went wrong, perhaps RediSearch isn't installed... |
| 33 | + console.error(e); |
| 34 | + process.exit(1); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +function float32Buffer(arr) { |
| 39 | + return Buffer.from(new Float32Array(arr).buffer); |
| 40 | +} |
| 41 | + |
| 42 | +// Add some sample data... |
| 43 | +// https://redis.io/commands/hset/ |
| 44 | +await Promise.all([ |
| 45 | + client.hSet('noderedis:knn:a', { v: float32Buffer([0.1, 0.1]) }), |
| 46 | + client.hSet('noderedis:knn:b', { v: float32Buffer([0.1, 0.2]) }), |
| 47 | + client.hSet('noderedis:knn:c', { v: float32Buffer([0.1, 0.3]) }), |
| 48 | + client.hSet('noderedis:knn:d', { v: float32Buffer([0.1, 0.4]) }), |
| 49 | +]); |
| 50 | +// Perform a K-Nearest Neighbors vector similarity search |
| 51 | +// Documentation: https://redis.io/docs/stack/search/reference/vectors/#pure-knn-queries |
| 52 | +const results = await client.ft.search('idx:knn-example', '*=>[KNN 4 @v $BLOB AS dist]', { |
| 53 | + PARAMS: { |
| 54 | + BLOB: float32Buffer([0.1, 0.1]) |
| 55 | + }, |
| 56 | + SORTBY: 'dist', |
| 57 | + DIALECT: 2, |
| 58 | + RETURN: ['dist'] |
| 59 | +}); |
| 60 | +console.log(JSON.stringify(results, null, 2)); |
| 61 | +// results: |
| 62 | +// { |
| 63 | +// "total": 4, |
| 64 | +// "documents": [ |
| 65 | +// { |
| 66 | +// "id": "noderedis:knn:a", |
| 67 | +// "value": { |
| 68 | +// "dist": "5.96046447754e-08" |
| 69 | +// } |
| 70 | +// }, |
| 71 | +// { |
| 72 | +// "id": "noderedis:knn:b", |
| 73 | +// "value": { |
| 74 | +// "dist": "0.0513167381287" |
| 75 | +// } |
| 76 | +// }, |
| 77 | +// { |
| 78 | +// "id": "noderedis:knn:c", |
| 79 | +// "value": { |
| 80 | +// "dist": "0.10557281971" |
| 81 | +// } |
| 82 | +// }, |
| 83 | +// { |
| 84 | +// "id": "noderedis:knn:d", |
| 85 | +// "value": { |
| 86 | +// "dist": "0.142507016659" |
| 87 | +// } |
| 88 | +// } |
| 89 | +// ] |
| 90 | +// } |
| 91 | +await client.quit(); |
0 commit comments