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

Updated search example to show sorting. (#2148)

* Updated search example to show sorting.

* Fixed example response.
This commit is contained in:
Simon Prickett
2022-05-26 14:57:08 +01:00
committed by GitHub
parent 82f43d9a53
commit f269319f42

View File

@@ -40,29 +40,38 @@ async function searchHashes() {
client.hSet('noderedis:animals:4', {name: 'Fido', species: 'dog', age: 7})
]);
// Perform a search query, find all the dogs...
// Perform a search query, find all the dogs... sort by age, descending.
// 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}');
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:4',
// value: {
// name: 'Fido',
// species: 'dog',
// age: '7'
// }
// },
// {
// id: 'noderedis:animals:3',
// value: {
// age: '9',
// name: 'Rover',
// species: 'dog',
// age: '9'
// species: 'dog'
// }
// },
// {
// id: 'noderedis:animals:4',
// value: {
// age: '7',
// name: 'Fido',
// species: 'dog'
// }
// }
// ]
@@ -71,9 +80,9 @@ async function searchHashes() {
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}`);
// 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();