You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-04 15:02:09 +03:00
Adds example of how to escape punctuation when searching. (#2027)
* Adds example of how to escape punctuation when searching. * Optimized regex.
This commit is contained in:
@@ -21,6 +21,10 @@ async function searchJSON() {
|
|||||||
'$.coins': {
|
'$.coins': {
|
||||||
type: SchemaFieldTypes.NUMERIC,
|
type: SchemaFieldTypes.NUMERIC,
|
||||||
AS: 'coins'
|
AS: 'coins'
|
||||||
|
},
|
||||||
|
'$.email': {
|
||||||
|
type: SchemaFieldTypes.TAG,
|
||||||
|
AS: 'email'
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
ON: 'JSON',
|
ON: 'JSON',
|
||||||
@@ -41,12 +45,14 @@ async function searchJSON() {
|
|||||||
client.json.set('noderedis:users:1', '$', {
|
client.json.set('noderedis:users:1', '$', {
|
||||||
name: 'Alice',
|
name: 'Alice',
|
||||||
age: 32,
|
age: 32,
|
||||||
coins: 100
|
coins: 100,
|
||||||
|
email: 'alice@nonexist.com'
|
||||||
}),
|
}),
|
||||||
client.json.set('noderedis:users:2', '$', {
|
client.json.set('noderedis:users:2', '$', {
|
||||||
name: 'Bob',
|
name: 'Bob',
|
||||||
age: 23,
|
age: 23,
|
||||||
coins: 15
|
coins: 15,
|
||||||
|
email: 'bob@somewhere.gov'
|
||||||
})
|
})
|
||||||
]);
|
]);
|
||||||
|
|
||||||
@@ -54,37 +60,85 @@ async function searchJSON() {
|
|||||||
console.log('Users under 30 years old:');
|
console.log('Users under 30 years old:');
|
||||||
console.log(
|
console.log(
|
||||||
// https://oss.redis.com/redisearch/Commands/#ftsearch
|
// https://oss.redis.com/redisearch/Commands/#ftsearch
|
||||||
await client.ft.search('idx:users', '@age:[0 30]')
|
JSON.stringify(
|
||||||
|
await client.ft.search('idx:users', '@age:[0 30]'),
|
||||||
|
null,
|
||||||
|
2
|
||||||
|
)
|
||||||
);
|
);
|
||||||
// {
|
// {
|
||||||
// total: 1,
|
// "total": 1,
|
||||||
// documents: [ { id: 'noderedis:users:2', value: [Object] } ]
|
// "documents": [
|
||||||
|
// {
|
||||||
|
// "id": "noderedis:users:2",
|
||||||
|
// "value": {
|
||||||
|
// "name": "Bob",
|
||||||
|
// "age": 23,
|
||||||
|
// "coins": 15,
|
||||||
|
// "email": "bob@somewhere.gov"
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// ]
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Find a user by email - note we need to escape . and @ characters
|
||||||
|
// in the email address. This applies for other punctuation too.
|
||||||
|
// https://oss.redis.com/redisearch/Tags/#including_punctuation_in_tags
|
||||||
|
console.log('Users with email "bob@somewhere.gov":');
|
||||||
|
const emailAddress = 'bob@somewhere.gov'.replace(/[.@]/g, '\\$&');
|
||||||
|
console.log(
|
||||||
|
JSON.stringify(
|
||||||
|
await client.ft.search('idx:users', `@email:{${emailAddress}}`),
|
||||||
|
null,
|
||||||
|
2
|
||||||
|
)
|
||||||
|
);
|
||||||
|
// {
|
||||||
|
// "total": 1,
|
||||||
|
// "documents": [
|
||||||
|
// {
|
||||||
|
// "id": "noderedis:users:2",
|
||||||
|
// "value": {
|
||||||
|
// "name": "Bob",
|
||||||
|
// "age": 23,
|
||||||
|
// "coins": 15,
|
||||||
|
// "email": "bob@somewhere.gov"
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// ]
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// Some aggregrations, what's the average age and total number of coins...
|
// Some aggregrations, what's the average age and total number of coins...
|
||||||
// https://oss.redis.com/redisearch/Commands/#ftaggregate
|
// https://oss.redis.com/redisearch/Commands/#ftaggregate
|
||||||
|
console.log('Aggregation Demo:');
|
||||||
console.log(
|
console.log(
|
||||||
await client.ft.aggregate('idx:users', '*', {
|
JSON.stringify(
|
||||||
STEPS: [{
|
await client.ft.aggregate('idx:users', '*', {
|
||||||
type: AggregateSteps.GROUPBY,
|
STEPS: [{
|
||||||
REDUCE: [{
|
type: AggregateSteps.GROUPBY,
|
||||||
type: AggregateGroupByReducers.AVG,
|
REDUCE: [{
|
||||||
property: 'age',
|
type: AggregateGroupByReducers.AVG,
|
||||||
AS: 'averageAge'
|
property: 'age',
|
||||||
}, {
|
AS: 'averageAge'
|
||||||
type: AggregateGroupByReducers.SUM,
|
}, {
|
||||||
property: 'coins',
|
type: AggregateGroupByReducers.SUM,
|
||||||
AS: 'totalCoins'
|
property: 'coins',
|
||||||
|
AS: 'totalCoins'
|
||||||
|
}]
|
||||||
}]
|
}]
|
||||||
}]
|
}),
|
||||||
})
|
null,
|
||||||
|
2
|
||||||
|
)
|
||||||
);
|
);
|
||||||
// {
|
// {
|
||||||
// total: 2,
|
// "total": 1,
|
||||||
// results: [{
|
// "results": [
|
||||||
// averageAge: '27.5',
|
// {
|
||||||
// totalCoins: '115'
|
// "averageAge": "27.5",
|
||||||
// }]
|
// "totalCoins": "115"
|
||||||
|
// }
|
||||||
|
// ]
|
||||||
// }
|
// }
|
||||||
|
|
||||||
await client.quit();
|
await client.quit();
|
||||||
|
Reference in New Issue
Block a user