Skip to content

Commit

Permalink
Add Hashes and String Example (#3477)
Browse files Browse the repository at this point in the history
* Add Hashes Example

* Fixes examples for hash tutorial

* Refactor Hash Tutorial to Java 8 (from 17)

* Change indentation to 4 spaces

* Add Strings Example

* Reformat samples to 2-space indentation

---------

Co-authored-by: Elena Kolevska <[email protected]>
  • Loading branch information
bsbodden and elena-kolevska authored Jul 20, 2023
1 parent ec0e1a3 commit 983b677
Show file tree
Hide file tree
Showing 3 changed files with 326 additions and 88 deletions.
101 changes: 101 additions & 0 deletions src/test/java/io/redis/examples/HashExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//EXAMPLE: hash_tutorial
package io.redis.examples;

import redis.clients.jedis.UnifiedJedis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
//REMOVE_START
import org.junit.Test;
import static org.junit.Assert.assertEquals;
//REMOVE_END

public class HashExample {
@Test
public void run() {
try (UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379")) {
// REMOVE_START
jedis.del("bike:1", "bike:1:stats");
// REMOVE_END

// STEP_START set_get_all
Map<String, String> bike1 = new HashMap<>();
bike1.put("model", "Deimos");
bike1.put("brand", "Ergonom");
bike1.put("type", "Enduro bikes");
bike1.put("price", "4972");

Long res1 = jedis.hset("bike:1", bike1);
System.out.println(res1); // 4

String res2 = jedis.hget("bike:1", "model");
System.out.println(res2); // Deimos

String res3 = jedis.hget("bike:1", "price");
System.out.println(res3); // 4972

Map<String, String> res4 = jedis.hgetAll("bike:1");
System.out.println(res4); // {type=Enduro bikes, brand=Ergonom, price=4972, model=Deimos}
// STEP_END

// REMOVE_START
assertEquals(4, res1.longValue());
assertEquals("Deimos", res2);
assertEquals("4972", res3);
assertEquals("Deimos", res4.get("model"));
assertEquals("Ergonom", res4.get("brand"));
assertEquals("Enduro bikes", res4.get("type"));
assertEquals("4972", res4.get("price"));
// REMOVE_END

// STEP_START hmget
List<String> res5 = jedis.hmget("bike:1", "model", "price");
System.out.println(res5); // [Deimos, 4972]
// STEP_END

// REMOVE_START
assert res5.toString().equals("[Deimos, 4972]");
// REMOVE_END

// STEP_START hincrby
Long res6 = jedis.hincrBy("bike:1", "price", 100);
System.out.println(res6); // 5072
Long res7 = jedis.hincrBy("bike:1", "price", -100);
System.out.println(res7); // 4972
// STEP_END

// REMOVE_START
assertEquals(5072, res6.longValue());
assertEquals(4972, res7.longValue());
// REMOVE_END

// STEP_START incrby_get_mget
Long res8 = jedis.hincrBy("bike:1:stats", "rides", 1);
System.out.println(res8); // 1
Long res9 = jedis.hincrBy("bike:1:stats", "rides", 1);
System.out.println(res9); // 2
Long res10 = jedis.hincrBy("bike:1:stats", "rides", 1);
System.out.println(res10); // 3
Long res11 = jedis.hincrBy("bike:1:stats", "crashes", 1);
System.out.println(res11); // 1
Long res12 = jedis.hincrBy("bike:1:stats", "owners", 1);
System.out.println(res12); // 1
String res13 = jedis.hget("bike:1:stats", "rides");
System.out.println(res13); // 3
List<String> res14 = jedis.hmget("bike:1:stats", "crashes", "owners");
System.out.println(res14); // [1, 1]
// STEP_END

// REMOVE_START
assertEquals(1, res8.longValue());
assertEquals(2, res9.longValue());
assertEquals(3, res10.longValue());
assertEquals(1, res11.longValue());
assertEquals(1, res12.longValue());
assertEquals("3", res13);
assertEquals("[1, 1]", res14.toString());
// REMOVE_END
}
}
}
238 changes: 150 additions & 88 deletions src/test/java/io/redis/examples/SearchQuickstartExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,99 +49,161 @@ public void run() {
// REMOVE_END

// STEP_START create_index
SchemaField[] schema = { TextField.of("$.brand").as("brand"),
TextField.of("$.model").as("model"), TextField.of("$.description").as("description"),
NumericField.of("$.price").as("price"), TagField.of("$.condition").as("condition") };
SchemaField[] schema = {
TextField.of("$.brand").as("brand"),
TextField.of("$.model").as("model"),
TextField.of("$.description").as("description"),
NumericField.of("$.price").as("price"),
TagField.of("$.condition").as("condition")
};

jedis.ftCreate("idx:bicycle",
FTCreateParams.createParams().on(IndexDataType.JSON).addPrefix("bicycle:"), schema);
FTCreateParams.createParams()
.on(IndexDataType.JSON)
.addPrefix("bicycle:"),
schema
);
// STEP_END

Bicycle[] bicycles = {
new Bicycle("Velorim", "Jigger", new BigDecimal(270), "new",
"Small and powerful, the Jigger is the best ride "
+ "for the smallest of tikes! This is the tiniest "
+ "kids’ pedal bike on the market available without"
+ " a coaster brake, the Jigger is the vehicle of "
+ "choice for the rare tenacious little rider " + "raring to go."),
new Bicycle("Bicyk", "Hillcraft", new BigDecimal(1200), "used",
"Kids want to ride with as little weight as possible."
+ " Especially on an incline! They may be at the age "
+ "when a 27.5 inch wheel bike is just too clumsy coming "
+ "off a 24 inch bike. The Hillcraft 26 is just the solution" + " they need!"),
new Bicycle("Nord", "Chook air 5", new BigDecimal(815), "used",
"The Chook Air 5 gives kids aged six years and older "
+ "a durable and uberlight mountain bike for their first"
+ " experience on tracks and easy cruising through forests"
+ " and fields. The lower top tube makes it easy to mount"
+ " and dismount in any situation, giving your kids greater"
+ " safety on the trails."),
new Bicycle("Eva", "Eva 291", new BigDecimal(3400), "used",
"The sister company to Nord, Eva launched in 2005 as the"
+ " first and only women-dedicated bicycle brand. Designed"
+ " by women for women, allEva bikes are optimized for the"
+ " feminine physique using analytics from a body metrics"
+ " database. If you like 29ers, try the Eva 291. It's a "
+ "brand new bike for 2022.. This full-suspension, "
+ "cross-country ride has been designed for velocity. The"
+ " 291 has 100mm of front and rear travel, a superlight "
+ "aluminum frame and fast-rolling 29-inch wheels. Yippee!"),
new Bicycle("Noka Bikes", "Kahuna", new BigDecimal(3200), "used",
"Whether you want to try your hand at XC racing or are "
+ "looking for a lively trail bike that's just as inspiring"
+ " on the climbs as it is over rougher ground, the Wilder"
+ " is one heck of a bike built specifically for short women."
+ " Both the frames and components have been tweaked to "
+ "include a women’s saddle, different bars and unique " + "colourway."),
new Bicycle("Breakout", "XBN 2.1 Alloy", new BigDecimal(810), "new",
"The XBN 2.1 Alloy is our entry-level road bike – but that’s"
+ " not to say that it’s a basic machine. With an internal "
+ "weld aluminium frame, a full carbon fork, and the slick-shifting"
+ " Claris gears from Shimano’s, this is a bike which doesn’t"
+ " break the bank and delivers craved performance."),
new Bicycle("ScramBikes", "WattBike", new BigDecimal(2300), "new",
"The WattBike is the best e-bike for people who still feel young"
+ " at heart. It has a Bafang 1000W mid-drive system and a 48V"
+ " 17.5AH Samsung Lithium-Ion battery, allowing you to ride for"
+ " more than 60 miles on one charge. It’s great for tackling hilly"
+ " terrain or if you just fancy a more leisurely ride. With three"
+ " working modes, you can choose between E-bike, assisted bicycle,"
+ " and normal bike modes."),
new Bicycle("Peaknetic", "Secto", new BigDecimal(430), "new",
"If you struggle with stiff fingers or a kinked neck or back after"
+ " a few minutes on the road, this lightweight, aluminum bike"
+ " alleviates those issues and allows you to enjoy the ride. From"
+ " the ergonomic grips to the lumbar-supporting seat position, the"
+ " Roll Low-Entry offers incredible comfort. The rear-inclined seat"
+ " tube facilitates stability by allowing you to put a foot on the"
+ " ground to balance at a stop, and the low step-over frame makes it"
+ " accessible for all ability and mobility levels. The saddle is"
+ " very soft, with a wide back to support your hip joints and a"
+ " cutout in the center to redistribute that pressure. Rim brakes"
+ " deliver satisfactory braking control, and the wide tires provide"
+ " a smooth, stable ride on paved roads and gravel. Rack and fender"
+ " mounts facilitate setting up the Roll Low-Entry as your preferred"
+ " commuter, and the BMX-like handlebar offers space for mounting a"
+ " flashlight, bell, or phone holder."),
new Bicycle("nHill", "Summit", new BigDecimal(1200), "new",
"This budget mountain bike from nHill performs well both on bike"
+ " paths and on the trail. The fork with 100mm of travel absorbs"
+ " rough terrain. Fat Kenda Booster tires give you grip in corners"
+ " and on wet trails. The Shimano Tourney drivetrain offered enough"
+ " gears for finding a comfortable pace to ride uphill, and the"
+ " Tektro hydraulic disc brakes break smoothly. Whether you want an"
+ " affordable bike that you can take to work, but also take trail in"
+ " mountains on the weekends or you’re just after a stable,"
+ " comfortable ride for the bike path, the Summit gives a good value"
+ " for money."),
new Bicycle("ThrillCycle", "BikeShind", new BigDecimal(815), "refurbished",
"An artsy, retro-inspired bicycle that’s as functional as it is"
+ " pretty: The ThrillCycle steel frame offers a smooth ride. A"
+ " 9-speed drivetrain has enough gears for coasting in the city, but"
+ " we wouldn’t suggest taking it to the mountains. Fenders protect"
+ " you from mud, and a rear basket lets you transport groceries,"
+ " flowers and books. The ThrillCycle comes with a limited lifetime"
+ " warranty, so this little guy will last you long past graduation."), };
new Bicycle(
"Velorim",
"Jigger",
new BigDecimal(270),
"new",
"Small and powerful, the Jigger is the best ride " +
"for the smallest of tikes! This is the tiniest " +
"kids’ pedal bike on the market available without" +
" a coaster brake, the Jigger is the vehicle of " +
"choice for the rare tenacious little rider " +
"raring to go."
),
new Bicycle(
"Bicyk",
"Hillcraft",
new BigDecimal(1200),
"used",
"Kids want to ride with as little weight as possible." +
" Especially on an incline! They may be at the age " +
"when a 27.5 inch wheel bike is just too clumsy coming " +
"off a 24 inch bike. The Hillcraft 26 is just the solution" +
" they need!"
),
new Bicycle(
"Nord",
"Chook air 5",
new BigDecimal(815),
"used",
"The Chook Air 5 gives kids aged six years and older " +
"a durable and uberlight mountain bike for their first" +
" experience on tracks and easy cruising through forests" +
" and fields. The lower top tube makes it easy to mount" +
" and dismount in any situation, giving your kids greater" +
" safety on the trails."
),
new Bicycle(
"Eva",
"Eva 291",
new BigDecimal(3400),
"used",
"The sister company to Nord, Eva launched in 2005 as the" +
" first and only women-dedicated bicycle brand. Designed" +
" by women for women, allEva bikes are optimized for the" +
" feminine physique using analytics from a body metrics" +
" database. If you like 29ers, try the Eva 291. It's a " +
"brand new bike for 2022.. This full-suspension, " +
"cross-country ride has been designed for velocity. The" +
" 291 has 100mm of front and rear travel, a superlight " +
"aluminum frame and fast-rolling 29-inch wheels. Yippee!"
),
new Bicycle(
"Noka Bikes",
"Kahuna",
new BigDecimal(3200),
"used",
"Whether you want to try your hand at XC racing or are " +
"looking for a lively trail bike that's just as inspiring" +
" on the climbs as it is over rougher ground, the Wilder" +
" is one heck of a bike built specifically for short women." +
" Both the frames and components have been tweaked to " +
"include a women’s saddle, different bars and unique " +
"colourway."
),
new Bicycle(
"Breakout",
"XBN 2.1 Alloy",
new BigDecimal(810),
"new",
"The XBN 2.1 Alloy is our entry-level road bike – but that’s" +
" not to say that it’s a basic machine. With an internal " +
"weld aluminium frame, a full carbon fork, and the slick-shifting" +
" Claris gears from Shimano’s, this is a bike which doesn’t" +
" break the bank and delivers craved performance."
),
new Bicycle(
"ScramBikes",
"WattBike",
new BigDecimal(2300),
"new",
"The WattBike is the best e-bike for people who still feel young" +
" at heart. It has a Bafang 1000W mid-drive system and a 48V" +
" 17.5AH Samsung Lithium-Ion battery, allowing you to ride for" +
" more than 60 miles on one charge. It’s great for tackling hilly" +
" terrain or if you just fancy a more leisurely ride. With three" +
" working modes, you can choose between E-bike, assisted bicycle," +
" and normal bike modes."
),
new Bicycle(
"Peaknetic",
"Secto",
new BigDecimal(430),
"new",
"If you struggle with stiff fingers or a kinked neck or back after" +
" a few minutes on the road, this lightweight, aluminum bike" +
" alleviates those issues and allows you to enjoy the ride. From" +
" the ergonomic grips to the lumbar-supporting seat position, the" +
" Roll Low-Entry offers incredible comfort. The rear-inclined seat" +
" tube facilitates stability by allowing you to put a foot on the" +
" ground to balance at a stop, and the low step-over frame makes it" +
" accessible for all ability and mobility levels. The saddle is" +
" very soft, with a wide back to support your hip joints and a" +
" cutout in the center to redistribute that pressure. Rim brakes" +
" deliver satisfactory braking control, and the wide tires provide" +
" a smooth, stable ride on paved roads and gravel. Rack and fender" +
" mounts facilitate setting up the Roll Low-Entry as your preferred" +
" commuter, and the BMX-like handlebar offers space for mounting a" +
" flashlight, bell, or phone holder."
),
new Bicycle(
"nHill",
"Summit",
new BigDecimal(1200),
"new",
"This budget mountain bike from nHill performs well both on bike" +
" paths and on the trail. The fork with 100mm of travel absorbs" +
" rough terrain. Fat Kenda Booster tires give you grip in corners" +
" and on wet trails. The Shimano Tourney drivetrain offered enough" +
" gears for finding a comfortable pace to ride uphill, and the" +
" Tektro hydraulic disc brakes break smoothly. Whether you want an" +
" affordable bike that you can take to work, but also take trail in" +
" mountains on the weekends or you’re just after a stable," +
" comfortable ride for the bike path, the Summit gives a good value" +
" for money."
),
new Bicycle(
"ThrillCycle",
"BikeShind",
new BigDecimal(815),
"refurbished",
"An artsy, retro-inspired bicycle that’s as functional as it is" +
" pretty: The ThrillCycle steel frame offers a smooth ride. A" +
" 9-speed drivetrain has enough gears for coasting in the city, but" +
" we wouldn’t suggest taking it to the mountains. Fenders protect" +
" you from mud, and a rear basket lets you transport groceries," +
" flowers and books. The ThrillCycle comes with a limited lifetime" +
" warranty, so this little guy will last you long past graduation."
),
};

// STEP_START add_documents
for (int i = 0; i < bicycles.length; i++) {
Expand Down
Loading

0 comments on commit 983b677

Please sign in to comment.