Skip to content

Commit

Permalink
Updated search example to show sorting. (#2148)
Browse files Browse the repository at this point in the history
* Updated search example to show sorting.

* Fixed example response.
  • Loading branch information
Simon Prickett authored May 26, 2022
1 parent 82f43d9 commit f269319
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions examples/search-hashes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
// {
// id: 'noderedis:animals:3',
// value: {
// name: 'Fido',
// species: 'dog',
// age: '7'
// age: '9',
// name: 'Rover',
// species: 'dog'
// }
// },
// {
// id: 'noderedis:animals:3',
// id: 'noderedis:animals:4',
// value: {
// name: 'Rover',
// species: 'dog',
// age: '9'
// age: '7',
// name: 'Fido',
// species: 'dog'
// }
// }
// ]
Expand All @@ -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();
Expand Down

0 comments on commit f269319

Please sign in to comment.