Skip to content

Commit cc8c8ce

Browse files
DOC-4262 geo query examples (#3966)
Co-authored-by: M Sazzadul Hoque <[email protected]>
1 parent 43dd286 commit cc8c8ce

File tree

1 file changed

+299
-0
lines changed

1 file changed

+299
-0
lines changed
Lines changed: 299 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,299 @@
1+
// EXAMPLE: query_geo
2+
// REMOVE_START
3+
package io.redis.examples;
4+
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
// REMOVE_END
8+
// HIDE_START
9+
import java.util.List;
10+
import redis.clients.jedis.UnifiedJedis;
11+
import redis.clients.jedis.search.*;
12+
import redis.clients.jedis.search.schemafields.*;
13+
import redis.clients.jedis.search.schemafields.GeoShapeField.CoordinateSystem;
14+
import redis.clients.jedis.exceptions.JedisDataException;
15+
import redis.clients.jedis.json.Path2;
16+
// HIDE_END
17+
18+
// HIDE_START
19+
public class QueryGeoExample {
20+
@Test
21+
public void run() {
22+
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
23+
24+
//REMOVE_START
25+
// Clear any keys here before using them in tests.
26+
try { jedis.ftDropIndex("idx:bicycle"); } catch (JedisDataException j) {}
27+
//REMOVE_END
28+
// HIDE_END
29+
30+
SchemaField[] schema = {
31+
TextField.of("$.brand").as("brand"),
32+
TextField.of("$.model").as("model"),
33+
TextField.of("$.description").as("description"),
34+
NumericField.of("$.price").as("price"),
35+
TagField.of("$.condition").as("condition"),
36+
GeoField.of("$.store_location").as("store_location"),
37+
GeoShapeField.of("$.pickup_zone", CoordinateSystem.FLAT).as("pickup_zone")
38+
};
39+
40+
jedis.ftCreate("idx:bicycle",
41+
FTCreateParams.createParams()
42+
.on(IndexDataType.JSON)
43+
.addPrefix("bicycle:"),
44+
schema
45+
);
46+
47+
String[] bicycleJsons = new String[] {
48+
" {"
49+
+ " \"pickup_zone\": \"POLYGON((-74.0610 40.7578, -73.9510 40.7578, -73.9510 40.6678, "
50+
+ "-74.0610 40.6678, -74.0610 40.7578))\","
51+
+ " \"store_location\": \"-74.0060,40.7128\","
52+
+ " \"brand\": \"Velorim\","
53+
+ " \"model\": \"Jigger\","
54+
+ " \"price\": 270,"
55+
+ " \"description\": \"Small and powerful, the Jigger is the best ride for the smallest of tikes! "
56+
+ "This is the tiniest kids’ pedal bike on the market available without a coaster brake, the Jigger "
57+
+ "is the vehicle of choice for the rare tenacious little rider raring to go.\","
58+
+ " \"condition\": \"new\""
59+
+ " }",
60+
61+
" {"
62+
+ " \"pickup_zone\": \"POLYGON((-118.2887 34.0972, -118.1987 34.0972, -118.1987 33.9872, "
63+
+ "-118.2887 33.9872, -118.2887 34.0972))\","
64+
+ " \"store_location\": \"-118.2437,34.0522\","
65+
+ " \"brand\": \"Bicyk\","
66+
+ " \"model\": \"Hillcraft\","
67+
+ " \"price\": 1200,"
68+
+ " \"description\": \"Kids want to ride with as little weight as possible. Especially "
69+
+ "on an incline! They may be at the age when a 27.5'' wheel bike is just too clumsy coming "
70+
+ "off a 24'' bike. The Hillcraft 26 is just the solution they need!\","
71+
+ " \"condition\": \"used\""
72+
+ " }",
73+
74+
" {"
75+
+ " \"pickup_zone\": \"POLYGON((-87.6848 41.9331, -87.5748 41.9331, -87.5748 41.8231, "
76+
+ "-87.6848 41.8231, -87.6848 41.9331))\","
77+
+ " \"store_location\": \"-87.6298,41.8781\","
78+
+ " \"brand\": \"Nord\","
79+
+ " \"model\": \"Chook air 5\","
80+
+ " \"price\": 815,"
81+
+ " \"description\": \"The Chook Air 5 gives kids aged six years and older a durable "
82+
+ "and uberlight mountain bike for their first experience on tracks and easy cruising through "
83+
+ "forests and fields. The lower top tube makes it easy to mount and dismount in any "
84+
+ "situation, giving your kids greater safety on the trails.\","
85+
+ " \"condition\": \"used\""
86+
+ " }",
87+
88+
" {"
89+
+ " \"pickup_zone\": \"POLYGON((-80.2433 25.8067, -80.1333 25.8067, -80.1333 25.6967, "
90+
+ "-80.2433 25.6967, -80.2433 25.8067))\","
91+
+ " \"store_location\": \"-80.1918,25.7617\","
92+
+ " \"brand\": \"Eva\","
93+
+ " \"model\": \"Eva 291\","
94+
+ " \"price\": 3400,"
95+
+ " \"description\": \"The sister company to Nord, Eva launched in 2005 as the first "
96+
+ "and only women-dedicated bicycle brand. Designed by women for women, allEva bikes "
97+
+ "are optimized for the feminine physique using analytics from a body metrics database. "
98+
+ "If you like 29ers, try the Eva 291. It’s a brand new bike for 2022.. This "
99+
+ "full-suspension, cross-country ride has been designed for velocity. The 291 has "
100+
+ "100mm of front and rear travel, a superlight aluminum frame and fast-rolling "
101+
+ "29-inch wheels. Yippee!\","
102+
+ " \"condition\": \"used\""
103+
+ " }",
104+
105+
" {"
106+
+ " \"pickup_zone\": \"POLYGON((-122.4644 37.8199, -122.3544 37.8199, -122.3544 37.7099, "
107+
+ "-122.4644 37.7099, -122.4644 37.8199))\","
108+
+ " \"store_location\": \"-122.4194,37.7749\","
109+
+ " \"brand\": \"Noka Bikes\","
110+
+ " \"model\": \"Kahuna\","
111+
+ " \"price\": 3200,"
112+
+ " \"description\": \"Whether you want to try your hand at XC racing or are looking "
113+
+ "for a lively trail bike that's just as inspiring on the climbs as it is over rougher "
114+
+ "ground, the Wilder is one heck of a bike built specifically for short women. Both the "
115+
+ "frames and components have been tweaked to include a women’s saddle, different bars "
116+
+ "and unique colourway.\","
117+
+ " \"condition\": \"used\""
118+
+ " }",
119+
120+
" {"
121+
+ " \"pickup_zone\": \"POLYGON((-0.1778 51.5524, 0.0822 51.5524, 0.0822 51.4024, "
122+
+ "-0.1778 51.4024, -0.1778 51.5524))\","
123+
+ " \"store_location\": \"-0.1278,51.5074\","
124+
+ " \"brand\": \"Breakout\","
125+
+ " \"model\": \"XBN 2.1 Alloy\","
126+
+ " \"price\": 810,"
127+
+ " \"description\": \"The XBN 2.1 Alloy is our entry-level road bike – but that’s "
128+
+ "not to say that it’s a basic machine. With an internal weld aluminium frame, a full "
129+
+ "carbon fork, and the slick-shifting Claris gears from Shimano’s, this is a bike which "
130+
+ "doesn’t break the bank and delivers craved performance.\","
131+
+ " \"condition\": \"new\""
132+
+ " }",
133+
134+
" {"
135+
+ " \"pickup_zone\": \"POLYGON((2.1767 48.9016, 2.5267 48.9016, 2.5267 48.5516, "
136+
+ "2.1767 48.5516, 2.1767 48.9016))\","
137+
+ " \"store_location\": \"2.3522,48.8566\","
138+
+ " \"brand\": \"ScramBikes\","
139+
+ " \"model\": \"WattBike\","
140+
+ " \"price\": 2300,"
141+
+ " \"description\": \"The WattBike is the best e-bike for people who still "
142+
+ "feel young at heart. It has a Bafang 1000W mid-drive system and a 48V 17.5AH "
143+
+ "Samsung Lithium-Ion battery, allowing you to ride for more than 60 miles on one "
144+
+ "charge. It’s great for tackling hilly terrain or if you just fancy a more "
145+
+ "leisurely ride. With three working modes, you can choose between E-bike, "
146+
+ "assisted bicycle, and normal bike modes.\","
147+
+ " \"condition\": \"new\""
148+
+ " }",
149+
150+
" {"
151+
+ " \"pickup_zone\": \"POLYGON((13.3260 52.5700, 13.6550 52.5700, 13.6550 52.2700, "
152+
+ "13.3260 52.2700, 13.3260 52.5700))\","
153+
+ " \"store_location\": \"13.4050,52.5200\","
154+
+ " \"brand\": \"Peaknetic\","
155+
+ " \"model\": \"Secto\","
156+
+ " \"price\": 430,"
157+
+ " \"description\": \"If you struggle with stiff fingers or a kinked neck or "
158+
+ "back after a few minutes on the road, this lightweight, aluminum bike alleviates "
159+
+ "those issues and allows you to enjoy the ride. From the ergonomic grips to the "
160+
+ "lumbar-supporting seat position, the Roll Low-Entry offers incredible comfort. "
161+
+ "The rear-inclined seat tube facilitates stability by allowing you to put a foot "
162+
+ "on the ground to balance at a stop, and the low step-over frame makes it "
163+
+ "accessible for all ability and mobility levels. The saddle is very soft, with "
164+
+ "a wide back to support your hip joints and a cutout in the center to redistribute "
165+
+ "that pressure. Rim brakes deliver satisfactory braking control, and the wide tires "
166+
+ "provide a smooth, stable ride on paved roads and gravel. Rack and fender mounts "
167+
+ "facilitate setting up the Roll Low-Entry as your preferred commuter, and the "
168+
+ "BMX-like handlebar offers space for mounting a flashlight, bell, or phone holder.\","
169+
+ " \"condition\": \"new\""
170+
+ " }",
171+
172+
" {"
173+
+ " \"pickup_zone\": \"POLYGON((1.9450 41.4301, 2.4018 41.4301, 2.4018 41.1987, "
174+
+ "1.9450 41.1987, 1.9450 41.4301))\","
175+
+ " \"store_location\": \"2.1734, 41.3851\","
176+
+ " \"brand\": \"nHill\","
177+
+ " \"model\": \"Summit\","
178+
+ " \"price\": 1200,"
179+
+ " \"description\": \"This budget mountain bike from nHill performs well both "
180+
+ "on bike paths and on the trail. The fork with 100mm of travel absorbs rough "
181+
+ "terrain. Fat Kenda Booster tires give you grip in corners and on wet trails. "
182+
+ "The Shimano Tourney drivetrain offered enough gears for finding a comfortable "
183+
+ "pace to ride uphill, and the Tektro hydraulic disc brakes break smoothly. "
184+
+ "Whether you want an affordable bike that you can take to work, but also take "
185+
+ "trail in mountains on the weekends or you’re just after a stable, comfortable "
186+
+ "ride for the bike path, the Summit gives a good value for money.\","
187+
+ " \"condition\": \"new\""
188+
+ " }",
189+
190+
" {"
191+
+ " \"pickup_zone\": \"POLYGON((12.4464 42.1028, 12.5464 42.1028, "
192+
+ "12.5464 41.7028, 12.4464 41.7028, 12.4464 42.1028))\","
193+
+ " \"store_location\": \"12.4964,41.9028\","
194+
+ " \"model\": \"ThrillCycle\","
195+
+ " \"brand\": \"BikeShind\","
196+
+ " \"price\": 815,"
197+
+ " \"description\": \"An artsy, retro-inspired bicycle that’s as "
198+
+ "functional as it is pretty: The ThrillCycle steel frame offers a smooth ride. "
199+
+ "A 9-speed drivetrain has enough gears for coasting in the city, but we wouldn’t "
200+
+ "suggest taking it to the mountains. Fenders protect you from mud, and a rear "
201+
+ "basket lets you transport groceries, flowers and books. The ThrillCycle comes "
202+
+ "with a limited lifetime warranty, so this little guy will last you long "
203+
+ "past graduation.\","
204+
+ " \"condition\": \"refurbished\""
205+
+ " }"
206+
};
207+
208+
for (int i = 0; i < bicycleJsons.length; i++) {
209+
jedis.jsonSet("bicycle:" + i, Path2.ROOT_PATH, bicycleJsons[i]);
210+
}
211+
212+
// STEP_START geo1
213+
SearchResult res1 = jedis.ftSearch("idx:bicycle",
214+
"@store_location:[$lon $lat $radius $units]",
215+
FTSearchParams.searchParams()
216+
.addParam("lon", -0.1778)
217+
.addParam("lat", 51.5524)
218+
.addParam("radius", 20)
219+
.addParam("units", "mi")
220+
.dialect(2)
221+
);
222+
System.out.println(res1.getTotalResults()); // >>> 1
223+
224+
List<Document> docs1 = res1.getDocuments();
225+
226+
for (int i = 0; i < docs1.size(); i++) {
227+
System.out.println(docs1.get(i).getId());
228+
}
229+
// >>> bicycle:5
230+
// STEP_END
231+
232+
// Tests for 'geo1' step.
233+
// REMOVE_START
234+
Assert.assertEquals(1, res1.getTotalResults());
235+
Assert.assertEquals("bicycle:5", docs1.get(0).getId());
236+
// REMOVE_END
237+
238+
239+
// STEP_START geo2
240+
SearchResult res2 = jedis.ftSearch("idx:bicycle",
241+
"@pickup_zone:[CONTAINS $bike]",
242+
FTSearchParams.searchParams()
243+
.addParam("bike", "POINT(-0.1278 51.5074)")
244+
.dialect(3)
245+
);
246+
System.out.println(res2.getTotalResults()); // >>> 1
247+
248+
List<Document> docs2 = res2.getDocuments();
249+
250+
for (int i = 0; i < docs2.size(); i++) {
251+
System.out.println(docs2.get(i).getId());
252+
}
253+
// >>> bicycle:5
254+
// STEP_END
255+
256+
// Tests for 'geo2' step.
257+
// REMOVE_START
258+
Assert.assertEquals(1, res2.getTotalResults());
259+
Assert.assertEquals("bicycle:5", docs2.get(0).getId());
260+
// REMOVE_END
261+
262+
263+
// STEP_START geo3
264+
SearchResult res3 = jedis.ftSearch("idx:bicycle",
265+
"@pickup_zone:[WITHIN $europe]",
266+
FTSearchParams.searchParams()
267+
.addParam("europe", "POLYGON((-25 35, 40 35, 40 70, -25 70, -25 35))")
268+
.dialect(3)
269+
);
270+
System.out.println(res3.getTotalResults()); // >>> 5
271+
272+
List<Document> docs3 = res3.getDocuments();
273+
274+
for (int i = 0; i < docs3.size(); i++) {
275+
System.out.println(docs3.get(i).getId());
276+
}
277+
// >>> bicycle:5
278+
// >>> bicycle:6
279+
// >>> bicycle:7
280+
// >>> bicycle:8
281+
// >>> bicycle:9
282+
// STEP_END
283+
284+
// Tests for 'geo3' step.
285+
// REMOVE_START
286+
Assert.assertEquals(5, res3.getTotalResults());
287+
Assert.assertEquals("bicycle:5", docs3.get(0).getId());
288+
Assert.assertEquals("bicycle:6", docs3.get(1).getId());
289+
Assert.assertEquals("bicycle:7", docs3.get(2).getId());
290+
Assert.assertEquals("bicycle:8", docs3.get(3).getId());
291+
Assert.assertEquals("bicycle:9", docs3.get(4).getId());
292+
// REMOVE_END
293+
294+
// HIDE_START
295+
jedis.close();
296+
}
297+
}
298+
// HIDE_END
299+

0 commit comments

Comments
 (0)