1
+ // EXAMPLE: search_quickstart
2
+ using NRedisStack . RedisStackCommands ;
3
+ using NRedisStack . Search ;
4
+ using NRedisStack . Search . Aggregation ;
5
+ using NRedisStack . Search . Literals . Enums ;
6
+ using StackExchange . Redis ;
7
+
8
+ // REMOVE_START
9
+ namespace NRedisStack . Doc ;
10
+ // REMOVE_END
11
+
12
+ public class SearchQuickstartExample
13
+ {
14
+ [ Fact ]
15
+ public void run ( )
16
+ {
17
+ // STEP_START connect
18
+ var redis = ConnectionMultiplexer . Connect ( "localhost:6379" ) ;
19
+ var db = redis . GetDatabase ( ) ;
20
+ var ft = db . FT ( ) ;
21
+ var json = db . JSON ( ) ;
22
+ // STEP_END
23
+
24
+ // REMOVE_START
25
+ try
26
+ {
27
+ ft . DropIndex ( "idx:bicycle" ) ;
28
+ }
29
+ catch
30
+ {
31
+ }
32
+ // REMOVE_END
33
+
34
+ // STEP_START data_sample
35
+ var bike1 = new
36
+ {
37
+ Brand = "Diaz Ltd" ,
38
+ Model = "Dealer Sl" ,
39
+ Price = 7315.58M ,
40
+ Description = "The Diaz Ltd Dealer Sl is a reliable choice" +
41
+ " for urban cycling. The Diaz Ltd Dealer Sl " +
42
+ "is a comfortable choice for urban cycling." ,
43
+ Condition = "used"
44
+ } ;
45
+ // STEP_END
46
+
47
+ var bicycles = new [ ]
48
+ {
49
+ bike1 ,
50
+ new
51
+ {
52
+ Brand = "Bridges Group" ,
53
+ Model = "Project Pro" ,
54
+ Price = 3610.82M ,
55
+ Description =
56
+ "This mountain bike is perfect for mountain biking. " +
57
+ "The Bridges Group Project Pro is a responsive choice" +
58
+ " for mountain biking." ,
59
+ Condition = "used"
60
+ } ,
61
+ new
62
+ {
63
+ Brand = "Vega, Cole and Miller" ,
64
+ Model = "Group Advanced" ,
65
+ Price = 8961.42M ,
66
+ Description =
67
+ "The Vega, Cole and Miller Group Advanced provides an " +
68
+ "excellent ride. With its fast carbon frame and 24 gears," +
69
+ " this bicycle is perfect for any terrain." ,
70
+ Condition = "used"
71
+ } ,
72
+ new
73
+ {
74
+ Brand = "Powell-Montgomery" ,
75
+ Model = "Angle Race" ,
76
+ Price = 4050.27M ,
77
+ Description =
78
+ "The Powell-Montgomery Angle Race is a smooth choice for" +
79
+ " road cycling. The Powell-Montgomery Angle Race" +
80
+ " provides a durable ride." ,
81
+ Condition = "used"
82
+ } ,
83
+ new
84
+ {
85
+ Brand = "Gill-Lewis" ,
86
+ Model = "Action Evo" ,
87
+ Price = 283.68M ,
88
+ Description =
89
+ "The Gill-Lewis Action Evo provides a smooth ride. " +
90
+ "The Gill-Lewis Action Evo provides an excellent ride." ,
91
+ Condition = "used"
92
+ } ,
93
+ new
94
+ {
95
+ Brand = "Rodriguez-Guerrero" ,
96
+ Model = "Drama Comp" ,
97
+ Price = 4462.55M ,
98
+ Description =
99
+ "This kids bike is perfect for young riders. With its " +
100
+ "excellent aluminum frame and 12 gears, this bicycle " +
101
+ "is perfect for any terrain." ,
102
+ Condition = "new"
103
+ } ,
104
+ new
105
+ {
106
+ Brand = "Moore PLC" ,
107
+ Model = "Award Race" ,
108
+ Price = 3790.76M ,
109
+ Description =
110
+ "This olive folding bike features a carbon frame and" +
111
+ " 27.5 inch wheels. This folding bike is perfect for" +
112
+ " compact storage and transportation." ,
113
+ Condition = "new"
114
+ } ,
115
+ new
116
+ {
117
+ Brand = "Hall, Haley and Hayes" ,
118
+ Model = "Weekend Plus" ,
119
+ Price = 2008.4M ,
120
+ Description =
121
+ "The Hall, Haley and Hayes Weekend Plus provides a" +
122
+ " comfortable ride. This blue kids bike features a" +
123
+ " steel frame and 29.0 inch wheels." ,
124
+ Condition = "new"
125
+ } ,
126
+ new
127
+ {
128
+ Brand = "Peck-Carson" ,
129
+ Model = "Sun Hybrid" ,
130
+ Price = 9874.95M ,
131
+ Description =
132
+ "With its comfortable aluminum frame and 25 gears," +
133
+ " this bicycle is perfect for any terrain. The " +
134
+ "Peck-Carson Sun Hybrid provides a comfortable ride." ,
135
+ Condition = "new"
136
+ } ,
137
+ new
138
+ {
139
+ Brand = "Fowler Ltd" ,
140
+ Model = "Weekend Trail" ,
141
+ Price = 3833.71M ,
142
+ Description =
143
+ "The Fowler Ltd Letter Trail is a comfortable choice" +
144
+ " for transporting cargo. This cargo bike is " +
145
+ "perfect for transporting cargo." ,
146
+ Condition = "refurbished"
147
+ }
148
+ } ;
149
+
150
+ // STEP_START define_index
151
+ var schema = new Schema ( )
152
+ . AddTextField ( new FieldName ( "$.Brand" , "Brand" ) )
153
+ . AddTextField ( new FieldName ( "$.Model" , "Model" ) )
154
+ . AddTextField ( new FieldName ( "$.Description" , "Description" ) )
155
+ . AddNumericField ( new FieldName ( "$.Price" , "Price" ) )
156
+ . AddTagField ( new FieldName ( "$.Condition" , "Condition" ) ) ;
157
+ // STEP_END
158
+
159
+ // STEP_START create_index
160
+ ft . Create (
161
+ "idx:bicycle" ,
162
+ new FTCreateParams ( ) . On ( IndexDataType . JSON ) . Prefix ( "bicycle:" ) ,
163
+ schema ) ;
164
+ // STEP_END
165
+
166
+ // STEP_START add_documents
167
+ for ( int i = 0 ; i < bicycles . Length ; i ++ )
168
+ {
169
+ json . Set ( $ "bicycle:{ i } ", "$" , bicycles [ i ] ) ;
170
+ }
171
+ // STEP_END
172
+
173
+ // STEP_START query_single_term_and_num_range
174
+ var query = new Query ( "folding @Price:[1000 4000]" ) ;
175
+ var res = ft . Search ( "idx:bicycle" , query ) . Documents ;
176
+ Console . WriteLine ( string . Join ( "\n " , res . Select ( x => x [ "json" ] ) ) ) ;
177
+ // Prints: {"Brand":"Moore PLC","Model":"Award Race","Price":3790.76,
178
+ // "Description":"This olive folding bike features a carbon frame
179
+ // and 27.5 inch wheels. This folding bike is perfect for compact
180
+ // storage and transportation.","Condition":"new"}
181
+ // STEP_END
182
+ // REMOVE_START
183
+ Assert . Single ( res ) ;
184
+ Assert . Equal ( "bicycle:6" , res [ 0 ] . Id ) ;
185
+ // REMOVE_END
186
+
187
+ // STEP_START query_single_term_limit_fields
188
+ var cargoQuery = new Query ( "cargo" ) . ReturnFields ( "Price" ) ;
189
+ var cargoRes = ft . Search ( "idx:bicycle" , cargoQuery ) . Documents ;
190
+ Console . WriteLine ( cargoRes . First ( ) [ "Price" ] ) ;
191
+ // Prints: 3833.71
192
+ // STEP_END
193
+ // REMOVE_START
194
+ Assert . Single ( cargoRes ) ;
195
+ Assert . Equal ( "bicycle:9" , cargoRes [ 0 ] . Id ) ;
196
+ // REMOVE_END
197
+
198
+ // STEP_START simple_aggregation
199
+ var request = new AggregationRequest ( "*" ) . GroupBy (
200
+ "@Condition" , Reducers . Count ( ) . As ( "Count" ) ) ;
201
+ var result = ft . Aggregate ( "idx:bicycle" , request ) ;
202
+
203
+ for ( var i = 0 ; i < result . TotalResults ; i ++ )
204
+ {
205
+ var row = result . GetRow ( i ) ;
206
+ Console . WriteLine ( $ "{ row [ "Condition" ] } - { row [ "Count" ] } ") ;
207
+ }
208
+
209
+ // Prints:
210
+ // refurbished - 1
211
+ // used - 5
212
+ // new - 4
213
+ // STEP_END
214
+ // REMOVE_START
215
+ Assert . Equal ( 3 , result . TotalResults ) ;
216
+ // REMOVE_END
217
+ }
218
+ }
0 commit comments