1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-09 00:22:08 +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}) 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 // Documentation: https://oss.redis.com/redisearch/Commands/#ftsearch
// Query synatax: https://oss.redis.com/redisearch/Query_Syntax/ // 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: // results:
// { // {
// total: 2, // total: 2,
// documents: [ // documents: [
// { // {
// id: 'noderedis:animals:4', // id: 'noderedis:animals:3',
// value: { // value: {
// name: 'Fido', // age: '9',
// species: 'dog', // name: 'Rover',
// age: '7' // species: 'dog'
// } // }
// }, // },
// { // {
// id: 'noderedis:animals:3', // id: 'noderedis:animals:4',
// value: { // value: {
// name: 'Rover', // age: '7',
// species: 'dog', // name: 'Fido',
// age: '9' // species: 'dog'
// } // }
// } // }
// ] // ]
@@ -71,9 +80,9 @@ async function searchHashes() {
console.log(`Results found: ${results.total}.`); console.log(`Results found: ${results.total}.`);
for (const doc of results.documents) { for (const doc of results.documents) {
// noderedis:animals:4: Fido // noderedis:animals:3: Rover, 9 years old.
// noderedis:animals:3: Rover // noderedis:animals:4: Fido, 7 years old.
console.log(`${doc.id}: ${doc.value.name}`); console.log(`${doc.id}: ${doc.value.name}, ${doc.value.age} years old.`);
} }
await client.quit(); await client.quit();