You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-06 02:15:48 +03:00
Updates examples. (#2219)
* Updates examples. * Added command link for hset.
This commit is contained in:
@@ -3,89 +3,86 @@
|
||||
|
||||
import { createClient, SchemaFieldTypes } from 'redis';
|
||||
|
||||
async function searchHashes() {
|
||||
const client = createClient();
|
||||
const client = createClient();
|
||||
|
||||
await client.connect();
|
||||
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);
|
||||
}
|
||||
// Create an index...
|
||||
try {
|
||||
// Documentation: https://redis.io/commands/ft.create/
|
||||
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... sort by age, descending.
|
||||
// Documentation: https://oss.redis.com/redisearch/Commands/#ftsearch
|
||||
// Query syntax: https://oss.redis.com/redisearch/Query_Syntax/
|
||||
const results = await client.ft.search(
|
||||
'idx:animals',
|
||||
'@species:{dog}',
|
||||
{
|
||||
SORTBY: {
|
||||
BY: 'age',
|
||||
DIRECTION: 'DESC' // or 'ASC' (default if DIRECTION is not present)
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// results:
|
||||
// {
|
||||
// total: 2,
|
||||
// documents: [
|
||||
// {
|
||||
// id: 'noderedis:animals:3',
|
||||
// value: {
|
||||
// age: '9',
|
||||
// name: 'Rover',
|
||||
// species: 'dog'
|
||||
// }
|
||||
// },
|
||||
// {
|
||||
// id: 'noderedis:animals:4',
|
||||
// value: {
|
||||
// age: '7',
|
||||
// name: 'Fido',
|
||||
// species: 'dog'
|
||||
// }
|
||||
// }
|
||||
// ]
|
||||
// }
|
||||
|
||||
console.log(`Results found: ${results.total}.`);
|
||||
|
||||
for (const doc of results.documents) {
|
||||
// noderedis:animals:3: Rover, 9 years old.
|
||||
// noderedis:animals:4: Fido, 7 years old.
|
||||
console.log(`${doc.id}: ${doc.value.name}, ${doc.value.age} years old.`);
|
||||
}
|
||||
|
||||
await client.quit();
|
||||
}
|
||||
|
||||
searchHashes();
|
||||
// Add some sample data...
|
||||
// https://redis.io/commands/hset/
|
||||
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... sort by age, descending.
|
||||
// Documentation: https://redis.io/commands/ft.search/
|
||||
// Query syntax: https://redis.io/docs/stack/search/reference/query_syntax/
|
||||
const results = await client.ft.search(
|
||||
'idx:animals',
|
||||
'@species:{dog}',
|
||||
{
|
||||
SORTBY: {
|
||||
BY: 'age',
|
||||
DIRECTION: 'DESC' // or 'ASC (default if DIRECTION is not present)
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// results:
|
||||
// {
|
||||
// total: 2,
|
||||
// documents: [
|
||||
// {
|
||||
// id: 'noderedis:animals:3',
|
||||
// value: {
|
||||
// name: 'Rover',
|
||||
// species: 'dog',
|
||||
// age: '9'
|
||||
// }
|
||||
// },
|
||||
// {
|
||||
// id: 'noderedis:animals:4',
|
||||
// value: {
|
||||
// name: 'Fido',
|
||||
// species: 'dog',
|
||||
// age: '7'
|
||||
// }
|
||||
// }
|
||||
// ]
|
||||
// }
|
||||
|
||||
console.log(`Results found: ${results.total}.`);
|
||||
|
||||
for (const doc of results.documents) {
|
||||
// noderedis:animals:3: Rover, 9 years old.
|
||||
// noderedis:animals:4: Fido, 7 years old.
|
||||
console.log(`${doc.id}: ${doc.value.name}, ${doc.value.age} years old.`);
|
||||
}
|
||||
|
||||
await client.quit();
|
||||
|
Reference in New Issue
Block a user