You've already forked node-redis
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:
@@ -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();
|
||||||
|
Reference in New Issue
Block a user