Skip to content

Commit f269319

Browse files
author
Simon Prickett
authored
Updated search example to show sorting. (#2148)
* Updated search example to show sorting. * Fixed example response.
1 parent 82f43d9 commit f269319

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

examples/search-hashes.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,38 @@ async function searchHashes() {
4040
client.hSet('noderedis:animals:4', {name: 'Fido', species: 'dog', age: 7})
4141
]);
4242

43-
// Perform a search query, find all the dogs...
43+
// Perform a search query, find all the dogs... sort by age, descending.
4444
// Documentation: https://oss.redis.com/redisearch/Commands/#ftsearch
4545
// Query synatax: https://oss.redis.com/redisearch/Query_Syntax/
46-
const results = await client.ft.search('idx:animals', '@species:{dog}');
46+
const results = await client.ft.search(
47+
'idx:animals',
48+
'@species:{dog}',
49+
{
50+
SORTBY: {
51+
BY: 'age',
52+
DIRECTION: 'DESC' // or 'ASC' (default if DIRECTION is not present)
53+
}
54+
}
55+
);
4756

4857
// results:
4958
// {
5059
// total: 2,
5160
// documents: [
52-
// {
53-
// id: 'noderedis:animals:4',
61+
// {
62+
// id: 'noderedis:animals:3',
5463
// value: {
55-
// name: 'Fido',
56-
// species: 'dog',
57-
// age: '7'
64+
// age: '9',
65+
// name: 'Rover',
66+
// species: 'dog'
5867
// }
5968
// },
6069
// {
61-
// id: 'noderedis:animals:3',
70+
// id: 'noderedis:animals:4',
6271
// value: {
63-
// name: 'Rover',
64-
// species: 'dog',
65-
// age: '9'
72+
// age: '7',
73+
// name: 'Fido',
74+
// species: 'dog'
6675
// }
6776
// }
6877
// ]
@@ -71,9 +80,9 @@ async function searchHashes() {
7180
console.log(`Results found: ${results.total}.`);
7281

7382
for (const doc of results.documents) {
74-
// noderedis:animals:4: Fido
75-
// noderedis:animals:3: Rover
76-
console.log(`${doc.id}: ${doc.value.name}`);
83+
// noderedis:animals:3: Rover, 9 years old.
84+
// noderedis:animals:4: Fido, 7 years old.
85+
console.log(`${doc.id}: ${doc.value.name}, ${doc.value.age} years old.`);
7786
}
7887

7988
await client.quit();

0 commit comments

Comments
 (0)