Skip to content

Commit cb6d685

Browse files
DOC-4243 added range query examples (#338)
* DOC-4243 added range query examples * DOC-4243 implemented PR feedback * DOC-4243 reinstated filter example using infinity
1 parent e756568 commit cb6d685

File tree

1 file changed

+273
-0
lines changed

1 file changed

+273
-0
lines changed

tests/Doc/QueryRangeExample.cs

Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
// EXAMPLE: query_range
2+
// HIDE_START
3+
4+
using NRedisStack.RedisStackCommands;
5+
using NRedisStack.Search;
6+
using NRedisStack.Search.Literals.Enums;
7+
using NRedisStack.Tests;
8+
using StackExchange.Redis;
9+
10+
// HIDE_END
11+
12+
// REMOVE_START
13+
namespace Doc;
14+
[Collection("DocsTests")]
15+
// REMOVE_END
16+
17+
// HIDE_START
18+
public class QueryRangeExample
19+
{
20+
21+
[SkipIfRedis(Is.OSSCluster)]
22+
public void run()
23+
{
24+
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
25+
var db = muxer.GetDatabase();
26+
//REMOVE_START
27+
// Clear any keys here before using them in tests.
28+
try { db.FT().DropIndex("idx:bicycle", dd: true); } catch { }
29+
//REMOVE_END
30+
31+
Schema bikeSchema = new Schema()
32+
.AddTextField(new FieldName("$.description", "description"))
33+
.AddNumericField(new FieldName("$.price", "price"))
34+
.AddTagField(new FieldName("$.condition", "condition"));
35+
36+
FTCreateParams bikeParams = new FTCreateParams()
37+
.AddPrefix("bicycle:")
38+
.On(IndexDataType.JSON);
39+
40+
db.FT().Create("idx:bicycle", bikeParams, bikeSchema);
41+
42+
var bicycles = new object[]
43+
{
44+
new
45+
{
46+
brand = "Velorim",
47+
model = "Jigger",
48+
price = 270,
49+
description = "Small and powerful, the Jigger is the best ride " +
50+
"for the smallest of tikes! This is the tiniest " +
51+
"kids’ pedal bike on the market available without" +
52+
" a coaster brake, the Jigger is the vehicle of " +
53+
"choice for the rare tenacious little rider " +
54+
"raring to go.",
55+
condition = "used"
56+
},
57+
new
58+
{
59+
brand = "Bicyk",
60+
model = "Hillcraft",
61+
price = 1200,
62+
description = "Kids want to ride with as little weight as possible." +
63+
" Especially on an incline! They may be at the age " +
64+
"when a 27.5 inch wheel bike is just too clumsy coming " +
65+
"off a 24 inch bike. The Hillcraft 26 is just the solution" +
66+
" they need!",
67+
condition = "used",
68+
},
69+
new
70+
{
71+
brand = "Nord",
72+
model = "Chook air 5",
73+
price = 815,
74+
description = "The Chook Air 5 gives kids aged six years and older " +
75+
"a durable and uberlight mountain bike for their first" +
76+
" experience on tracks and easy cruising through forests" +
77+
" and fields. The lower top tube makes it easy to mount" +
78+
" and dismount in any situation, giving your kids greater" +
79+
" safety on the trails.",
80+
condition = "used",
81+
},
82+
new
83+
{
84+
brand = "Eva",
85+
model = "Eva 291",
86+
price = 3400,
87+
description = "The sister company to Nord, Eva launched in 2005 as the" +
88+
" first and only women-dedicated bicycle brand. Designed" +
89+
" by women for women, allEva bikes are optimized for the" +
90+
" feminine physique using analytics from a body metrics" +
91+
" database. If you like 29ers, try the Eva 291. It’s a " +
92+
"brand new bike for 2022.. This full-suspension, " +
93+
"cross-country ride has been designed for velocity. The" +
94+
" 291 has 100mm of front and rear travel, a superlight " +
95+
"aluminum frame and fast-rolling 29-inch wheels. Yippee!",
96+
condition = "used",
97+
},
98+
new
99+
{
100+
brand = "Noka Bikes",
101+
model = "Kahuna",
102+
price = 3200,
103+
description = "Whether you want to try your hand at XC racing or are " +
104+
"looking for a lively trail bike that's just as inspiring" +
105+
" on the climbs as it is over rougher ground, the Wilder" +
106+
" is one heck of a bike built specifically for short women." +
107+
" Both the frames and components have been tweaked to " +
108+
"include a women’s saddle, different bars and unique " +
109+
"colourway.",
110+
condition = "used",
111+
},
112+
new
113+
{
114+
brand = "Breakout",
115+
model = "XBN 2.1 Alloy",
116+
price = 810,
117+
description = "The XBN 2.1 Alloy is our entry-level road bike – but that’s" +
118+
" not to say that it’s a basic machine. With an internal " +
119+
"weld aluminium frame, a full carbon fork, and the slick-shifting" +
120+
" Claris gears from Shimano’s, this is a bike which doesn’t" +
121+
" break the bank and delivers craved performance.",
122+
condition = "new",
123+
},
124+
new
125+
{
126+
brand = "ScramBikes",
127+
model = "WattBike",
128+
price = 2300,
129+
description = "The WattBike is the best e-bike for people who still feel young" +
130+
" at heart. It has a Bafang 1000W mid-drive system and a 48V" +
131+
" 17.5AH Samsung Lithium-Ion battery, allowing you to ride for" +
132+
" more than 60 miles on one charge. It’s great for tackling hilly" +
133+
" terrain or if you just fancy a more leisurely ride. With three" +
134+
" working modes, you can choose between E-bike, assisted bicycle," +
135+
" and normal bike modes.",
136+
condition = "new",
137+
},
138+
new
139+
{
140+
brand = "Peaknetic",
141+
model = "Secto",
142+
price = 430,
143+
description = "If you struggle with stiff fingers or a kinked neck or back after" +
144+
" a few minutes on the road, this lightweight, aluminum bike" +
145+
" alleviates those issues and allows you to enjoy the ride. From" +
146+
" the ergonomic grips to the lumbar-supporting seat position, the" +
147+
" Roll Low-Entry offers incredible comfort. The rear-inclined seat" +
148+
" tube facilitates stability by allowing you to put a foot on the" +
149+
" ground to balance at a stop, and the low step-over frame makes it" +
150+
" accessible for all ability and mobility levels. The saddle is" +
151+
" very soft, with a wide back to support your hip joints and a" +
152+
" cutout in the center to redistribute that pressure. Rim brakes" +
153+
" deliver satisfactory braking control, and the wide tires provide" +
154+
" a smooth, stable ride on paved roads and gravel. Rack and fender" +
155+
" mounts facilitate setting up the Roll Low-Entry as your preferred" +
156+
" commuter, and the BMX-like handlebar offers space for mounting a" +
157+
" flashlight, bell, or phone holder.",
158+
condition = "new",
159+
},
160+
new
161+
{
162+
brand = "nHill",
163+
model = "Summit",
164+
price = 1200,
165+
description = "This budget mountain bike from nHill performs well both on bike" +
166+
" paths and on the trail. The fork with 100mm of travel absorbs" +
167+
" rough terrain. Fat Kenda Booster tires give you grip in corners" +
168+
" and on wet trails. The Shimano Tourney drivetrain offered enough" +
169+
" gears for finding a comfortable pace to ride uphill, and the" +
170+
" Tektro hydraulic disc brakes break smoothly. Whether you want an" +
171+
" affordable bike that you can take to work, but also take trail in" +
172+
" mountains on the weekends or you’re just after a stable," +
173+
" comfortable ride for the bike path, the Summit gives a good value" +
174+
" for money.",
175+
condition = "new",
176+
},
177+
new
178+
{
179+
model = "ThrillCycle",
180+
brand = "BikeShind",
181+
price = 815,
182+
description = "An artsy, retro-inspired bicycle that’s as functional as it is" +
183+
" pretty: The ThrillCycle steel frame offers a smooth ride. A" +
184+
" 9-speed drivetrain has enough gears for coasting in the city, but" +
185+
" we wouldn’t suggest taking it to the mountains. Fenders protect" +
186+
" you from mud, and a rear basket lets you transport groceries," +
187+
" flowers and books. The ThrillCycle comes with a limited lifetime" +
188+
" warranty, so this little guy will last you long past graduation.",
189+
condition = "refurbished",
190+
},
191+
};
192+
193+
for (var i = 0; i < bicycles.Length; i++)
194+
{
195+
db.JSON().Set($"bicycle:{i}", "$", bicycles[i]);
196+
}
197+
// HIDE_END
198+
199+
200+
// STEP_START range1
201+
SearchResult res1 = db.FT().Search(
202+
"idx:bicycle",
203+
new Query("@price:[500 1000]")
204+
);
205+
Console.WriteLine(res1.TotalResults); // >>> 3
206+
// STEP_END
207+
208+
// Tests for 'range1' step.
209+
// REMOVE_START
210+
Assert.Equal(3, res1.TotalResults);
211+
// REMOVE_END
212+
213+
214+
// STEP_START range2
215+
SearchResult res2 = db.FT().Search(
216+
"idx:bicycle",
217+
new Query().AddFilter(
218+
new Query.NumericFilter("price", 500, 1000)
219+
)
220+
);
221+
Console.WriteLine(res2.TotalResults); // >>> 3
222+
// STEP_END
223+
224+
// Tests for 'range2' step.
225+
// REMOVE_START
226+
Assert.Equal(3, res2.TotalResults);
227+
// REMOVE_END
228+
229+
230+
// STEP_START range3
231+
SearchResult res3 = db.FT().Search(
232+
"idx:bicycle",
233+
new Query("*").AddFilter(new Query.NumericFilter(
234+
"price", 1000, true, Double.PositiveInfinity, false
235+
)
236+
)
237+
);
238+
Console.WriteLine(res3.TotalResults); // >>> 5
239+
// STEP_END
240+
241+
// Tests for 'range3' step.
242+
// REMOVE_START
243+
Assert.Equal(5, res3.TotalResults);
244+
// REMOVE_END
245+
246+
247+
// STEP_START range4
248+
SearchResult res4 = db.FT().Search(
249+
"idx:bicycle",
250+
new Query("@price:[-inf 2000]")
251+
.SetSortBy("price")
252+
.Limit(0, 5)
253+
);
254+
Console.WriteLine(res4.TotalResults); // >>> 7
255+
Console.WriteLine($"Prices: {string.Join(", ", res4.Documents.Select(d => d["price"]))}");
256+
// >>> Prices: 270, 430, 810, 815, 815
257+
// STEP_END
258+
259+
// Tests for 'range4' step.
260+
// REMOVE_START
261+
Assert.Equal(7, res4.TotalResults);
262+
Assert.Equal(
263+
"Prices: 270, 430, 810, 815, 815",
264+
$"Prices: {string.Join(", ", res4.Documents.Select(d => d["price"]))}"
265+
);
266+
// REMOVE_END
267+
268+
269+
// HIDE_START
270+
}
271+
}
272+
// HIDE_END
273+

0 commit comments

Comments
 (0)