1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-07 13:22:56 +03:00

Adds RediSearch demo.

This commit is contained in:
Simon Prickett
2021-11-23 21:39:05 +00:00
parent 42e36dfbb1
commit 13714e69ba
2 changed files with 91 additions and 8 deletions

View File

@@ -3,12 +3,13 @@
This folder contains example scripts showing how to use Node Redis in different scenarios. This folder contains example scripts showing how to use Node Redis in different scenarios.
| File Name | Description | | File Name | Description |
|-----------------------------|------------------------------------------------------------------------------------| |-----------------------------|----------------------------------------------------------------------------------------------------------------|
| `blocking-list-pop.js` | Block until an element is pushed to a list | | `blocking-list-pop.js` | Block until an element is pushed to a list |
| `command-with-modifiers.js` | Define a script that allows to run a command with several modifiers | | `command-with-modifiers.js` | Define a script that allows to run a command with several modifiers |
| `connect-as-acl-user.js` | Connect to Redis 6 using an ACL user | | `connect-as-acl-user.js` | Connect to Redis 6 using an ACL user |
| `lua-multi-incr.js` | Define a custom lua script that allows you to perform INCRBY on multiple keys | | `lua-multi-incr.js` | Define a custom lua script that allows you to perform INCRBY on multiple keys |
| `search+json.js` | Use [Redis Search](https://redisearch.io/) and [Redis JSON](https://redisjson.io/) | | `search-hashes.js` | Uses [RediSearch](https://redisearch.io) to index and search data in hashes |
| `search+json.js` | Uses [RediSearch](https://redisearch.io/) and [RedisJSON](https://redisjson.io/) to index and search JSON data |
| `set-scan.js` | An example script that shows how to use the SSCAN iterator functionality | | `set-scan.js` | An example script that shows how to use the SSCAN iterator functionality |
## Contributing ## Contributing

82
examples/search-hashes.js Normal file
View File

@@ -0,0 +1,82 @@
// This example demonstrates how to use RediSearch to index and query data
// stored in Redis hashes.
import { createClient, SchemaFieldTypes } from 'redis';
async function searchHashes() {
const client = createClient();
await client.connect();
// Create an index...
try {
// Documentation: https://oss.redis.com/redisearch/Commands/#ftcreate
await client.ft.create('idx:animals', {
name: {
type: SchemaFieldTypes.TEXT,
sortable: true
},
species: SchemaFieldTypes.TAG,
age: SchemaFieldTypes.NUMERIC
}, {
ON: 'HASH',
PREFIX: 'noderedis:animals'
});
} catch (e) {
if (e.message === 'Index already exists') {
console.log('Index exists already, skipped creation.');
} else {
// Something went wrong, perhaps RediSearch isn't installed...
console.error(e);
process.exit(1);
}
}
// Add some sample data...
await Promise.all([
client.hSet('noderedis:animals:1', {name: 'Fluffy', species: 'cat', age: 3}),
client.hSet('noderedis:animals:2', {name: 'Ginger', species: 'cat', age: 4}),
client.hSet('noderedis:animals:3', {name: 'Rover', species: 'dog', age: 9}),
client.hSet('noderedis:animals:4', {name: 'Fido', species: 'dog', age: 7})
]);
// Perform a search query, find all the dogs...
// Documentation: https://oss.redis.com/redisearch/Commands/#ftsearch
// Query synatax: https://oss.redis.com/redisearch/Query_Syntax/
const results = await client.ft.search('idx:animals', '@species:{dog}');
// results:
// {
// total: 2,
// documents: [
// {
// id: 'noderedis:animals:4',
// value: {
// name: 'Fido',
// species: 'dog',
// age: '7'
// }
// },
// {
// id: 'noderedis:animals:3',
// value: {
// name: 'Rover',
// species: 'dog',
// age: '9'
// }
// }
// ]
// }
console.log(`Results found: ${results.total}.`);
for (const doc of results.documents) {
// noderedis:animals:4: Fido
// noderedis:animals:3: Rover
console.log(`${doc.id}: ${doc.value.name}`);
}
await client.quit();
}
searchHashes();