1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-06 02:15:48 +03:00

Renamed JSON search example.

This commit is contained in:
Simon Prickett
2021-11-24 19:40:20 +00:00
parent 055af50890
commit 99b261b79c
2 changed files with 1 additions and 1 deletions

74
examples/search-json.js Normal file
View File

@@ -0,0 +1,74 @@
// This example demonstrates how to use RediSearch and RedisJSON together.
import { createClient, SchemaFieldTypes, AggregateGroupByReducers, AggregateSteps } from 'redis';
async function searchPlusJson() {
const client = createClient();
await client.connect();
// Create an index
await client.ft.create('users', {
'$.name': {
type: SchemaFieldTypes.TEXT,
SORTABLE: 'UNF'
},
'$.age': SchemaFieldTypes.NUMERIC,
'$.coins': SchemaFieldTypes.NUMERIC
}, {
ON: 'JSON'
});
// Add some users
await Promise.all([
client.json.set('users:1', '$', {
name: 'Alice',
age: 32,
coins: 100
}),
client.json.set('users:2', '$', {
name: 'Bob',
age: 23,
coins: 15
})
]);
// Search all users under 30
// TODO: why "$.age:[-inf, 30]" does not work?
console.log(
await client.ft.search('users', '*')
);
// {
// total: 1,
// documents: [...]
// }
// Some aggregrations...
console.log(
await client.ft.aggregate('users', '*', {
STEPS: [{
type: AggregateSteps.GROUPBY,
REDUCE: [{
type: AggregateGroupByReducers.AVG,
property: '$.age',
AS: 'averageAge'
}, {
type: AggregateGroupByReducers.SUM,
property: '$.coins',
AS: 'totalCoins'
}]
}]
})
);
// {
// total: 2,
// results: [{
// averageAge: '27.5',
// totalCoins: '115'
// }]
// }
await client.quit();
}
searchPlusJson();