Skip to content

Commit bae54f1

Browse files
author
Simon Prickett
authored
Adds example of how to escape punctuation when searching. (#2027)
* Adds example of how to escape punctuation when searching. * Optimized regex.
1 parent c57da8b commit bae54f1

File tree

1 file changed

+77
-23
lines changed

1 file changed

+77
-23
lines changed

examples/search-json.js

Lines changed: 77 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ async function searchJSON() {
2121
'$.coins': {
2222
type: SchemaFieldTypes.NUMERIC,
2323
AS: 'coins'
24+
},
25+
'$.email': {
26+
type: SchemaFieldTypes.TAG,
27+
AS: 'email'
2428
}
2529
}, {
2630
ON: 'JSON',
@@ -41,50 +45,100 @@ async function searchJSON() {
4145
client.json.set('noderedis:users:1', '$', {
4246
name: 'Alice',
4347
age: 32,
44-
coins: 100
48+
coins: 100,
49+
4550
}),
4651
client.json.set('noderedis:users:2', '$', {
4752
name: 'Bob',
4853
age: 23,
49-
coins: 15
54+
coins: 15,
55+
5056
})
5157
]);
5258

5359
// Search all users under 30
5460
console.log('Users under 30 years old:');
5561
console.log(
5662
// https://oss.redis.com/redisearch/Commands/#ftsearch
57-
await client.ft.search('idx:users', '@age:[0 30]')
63+
JSON.stringify(
64+
await client.ft.search('idx:users', '@age:[0 30]'),
65+
null,
66+
2
67+
)
5868
);
5969
// {
60-
// total: 1,
61-
// documents: [ { id: 'noderedis:users:2', value: [Object] } ]
70+
// "total": 1,
71+
// "documents": [
72+
// {
73+
// "id": "noderedis:users:2",
74+
// "value": {
75+
// "name": "Bob",
76+
// "age": 23,
77+
// "coins": 15,
78+
// "email": "[email protected]"
79+
// }
80+
// }
81+
// ]
82+
// }
83+
84+
// Find a user by email - note we need to escape . and @ characters
85+
// in the email address. This applies for other punctuation too.
86+
// https://oss.redis.com/redisearch/Tags/#including_punctuation_in_tags
87+
console.log('Users with email "[email protected]":');
88+
const emailAddress = '[email protected]'.replace(/[.@]/g, '\\$&');
89+
console.log(
90+
JSON.stringify(
91+
await client.ft.search('idx:users', `@email:{${emailAddress}}`),
92+
null,
93+
2
94+
)
95+
);
96+
// {
97+
// "total": 1,
98+
// "documents": [
99+
// {
100+
// "id": "noderedis:users:2",
101+
// "value": {
102+
// "name": "Bob",
103+
// "age": 23,
104+
// "coins": 15,
105+
// "email": "[email protected]"
106+
// }
107+
// }
108+
// ]
62109
// }
63110

64111
// Some aggregrations, what's the average age and total number of coins...
65112
// https://oss.redis.com/redisearch/Commands/#ftaggregate
113+
console.log('Aggregation Demo:');
66114
console.log(
67-
await client.ft.aggregate('idx:users', '*', {
68-
STEPS: [{
69-
type: AggregateSteps.GROUPBY,
70-
REDUCE: [{
71-
type: AggregateGroupByReducers.AVG,
72-
property: 'age',
73-
AS: 'averageAge'
74-
}, {
75-
type: AggregateGroupByReducers.SUM,
76-
property: 'coins',
77-
AS: 'totalCoins'
115+
JSON.stringify(
116+
await client.ft.aggregate('idx:users', '*', {
117+
STEPS: [{
118+
type: AggregateSteps.GROUPBY,
119+
REDUCE: [{
120+
type: AggregateGroupByReducers.AVG,
121+
property: 'age',
122+
AS: 'averageAge'
123+
}, {
124+
type: AggregateGroupByReducers.SUM,
125+
property: 'coins',
126+
AS: 'totalCoins'
127+
}]
78128
}]
79-
}]
80-
})
129+
}),
130+
null,
131+
2
132+
)
81133
);
82134
// {
83-
// total: 2,
84-
// results: [{
85-
// averageAge: '27.5',
86-
// totalCoins: '115'
87-
// }]
135+
// "total": 1,
136+
// "results": [
137+
// {
138+
// "averageAge": "27.5",
139+
// "totalCoins": "115"
140+
// }
141+
// ]
88142
// }
89143

90144
await client.quit();

0 commit comments

Comments
 (0)