@@ -21,6 +21,10 @@ async function searchJSON() {
21
21
'$.coins' : {
22
22
type : SchemaFieldTypes . NUMERIC ,
23
23
AS : 'coins'
24
+ } ,
25
+ '$.email' : {
26
+ type : SchemaFieldTypes . TAG ,
27
+ AS : 'email'
24
28
}
25
29
} , {
26
30
ON : 'JSON' ,
@@ -41,50 +45,100 @@ async function searchJSON() {
41
45
client . json . set ( 'noderedis:users:1' , '$' , {
42
46
name : 'Alice' ,
43
47
age : 32 ,
44
- coins : 100
48
+ coins : 100 ,
49
+
45
50
} ) ,
46
51
client . json . set ( 'noderedis:users:2' , '$' , {
47
52
name : 'Bob' ,
48
53
age : 23 ,
49
- coins : 15
54
+ coins : 15 ,
55
+
50
56
} )
51
57
] ) ;
52
58
53
59
// Search all users under 30
54
60
console . log ( 'Users under 30 years old:' ) ;
55
61
console . log (
56
62
// 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
+ )
58
68
) ;
59
69
// {
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
+
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
+
106
+ // }
107
+ // }
108
+ // ]
62
109
// }
63
110
64
111
// Some aggregrations, what's the average age and total number of coins...
65
112
// https://oss.redis.com/redisearch/Commands/#ftaggregate
113
+ console . log ( 'Aggregation Demo:' ) ;
66
114
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
+ } ]
78
128
} ]
79
- } ]
80
- } )
129
+ } ) ,
130
+ null ,
131
+ 2
132
+ )
81
133
) ;
82
134
// {
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
+ // ]
88
142
// }
89
143
90
144
await client . quit ( ) ;
0 commit comments