|
| 1 | +//EXAMPLE: set_and_get |
| 2 | +//HIDE_START |
| 3 | +package io.redis.examples; |
| 4 | + |
| 5 | +import java.math.BigDecimal; |
| 6 | +import java.util.*; |
| 7 | + |
| 8 | +import redis.clients.jedis.*; |
| 9 | +import redis.clients.jedis.exceptions.*; |
| 10 | +import redis.clients.jedis.search.*; |
| 11 | +import redis.clients.jedis.search.aggr.*; |
| 12 | +import redis.clients.jedis.search.schemafields.*; |
| 13 | +//REMOVE_START |
| 14 | +import org.junit.Test; |
| 15 | + |
| 16 | +import static org.junit.Assert.assertEquals; |
| 17 | +//REMOVE_END |
| 18 | + |
| 19 | +class Bicycle { |
| 20 | + public String brand; |
| 21 | + public String model; |
| 22 | + public BigDecimal price; |
| 23 | + public String description; |
| 24 | + public String condition; |
| 25 | + |
| 26 | + public Bicycle(String brand, String model, BigDecimal price, String description, String condition) { |
| 27 | + this.brand = brand; |
| 28 | + this.model = model; |
| 29 | + this.price = price; |
| 30 | + this.description = description; |
| 31 | + this.condition = condition; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +public class SearchQuickstartExample { |
| 36 | + |
| 37 | + @Test |
| 38 | + public void run() { |
| 39 | + //HIDE_END |
| 40 | + |
| 41 | + // STEP_START connect |
| 42 | + UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379"); |
| 43 | + // STEP_END |
| 44 | + // REMOVE_START |
| 45 | + try { |
| 46 | + jedis.ftDropIndex("idx:bicycle"); |
| 47 | + } catch (JedisDataException e) { |
| 48 | + // ignore |
| 49 | + } |
| 50 | + // REMOVE_END |
| 51 | + |
| 52 | + // STEP_START data_sample |
| 53 | + Bicycle bike1 = new Bicycle( |
| 54 | + "Diaz Ltd", |
| 55 | + "Dealer Sl", |
| 56 | + new BigDecimal(7315.58), |
| 57 | + "The Diaz Ltd Dealer Sl is a reliable choice" + |
| 58 | + " for urban cycling. The Diaz Ltd Dealer Sl " + |
| 59 | + "is a comfortable choice for urban cycling.", |
| 60 | + "used" |
| 61 | + ); |
| 62 | + // STEP_END |
| 63 | + |
| 64 | + Bicycle[] bicycles = { |
| 65 | + bike1, |
| 66 | + new Bicycle( |
| 67 | + "Bridges Group", |
| 68 | + "Project Pro", |
| 69 | + new BigDecimal(3610.82), |
| 70 | + "This mountain bike is perfect for mountain biking. The Bridges" + |
| 71 | + " Group Project Pro is a responsive choice for mountain biking.", |
| 72 | + "used" |
| 73 | + ), |
| 74 | + new Bicycle( |
| 75 | + "Vega, Cole and Miller", |
| 76 | + "Group Advanced", |
| 77 | + new BigDecimal(8961.42), |
| 78 | + "The Vega, Cole and Miller Group Advanced provides a excellent" + |
| 79 | + " ride. With its fast carbon frame and 24 gears, this bicycle is" + |
| 80 | + " perfect for any terrain.", |
| 81 | + "used" |
| 82 | + ), |
| 83 | + new Bicycle( |
| 84 | + "Powell-Montgomery", |
| 85 | + "Angle Race", |
| 86 | + new BigDecimal(4050.27), |
| 87 | + "The Powell-Montgomery Angle Race is a smooth choice for road" + |
| 88 | + " cycling. The Powell-Montgomery Angle Race provides a durable" + |
| 89 | + " ride.", |
| 90 | + "used" |
| 91 | + ), |
| 92 | + new Bicycle( |
| 93 | + "Gill-Lewis", |
| 94 | + "Action Evo", |
| 95 | + new BigDecimal(283.68), |
| 96 | + "The Gill-Lewis Action Evo provides a smooth ride. The Gill-Lewis" + |
| 97 | + " Action Evo provides a excellent ride.", |
| 98 | + "used" |
| 99 | + ), |
| 100 | + new Bicycle( |
| 101 | + "Rodriguez-Guerrero", |
| 102 | + "Drama Comp", |
| 103 | + new BigDecimal(4462.55), |
| 104 | + "This kids bike is perfect for young riders. With its excellent" + |
| 105 | + " aluminum frame and 12 gears, this bicycle is perfect for any" + |
| 106 | + " terrain.", |
| 107 | + "new" |
| 108 | + ), |
| 109 | + new Bicycle( |
| 110 | + "Moore PLC", |
| 111 | + "Award Race", |
| 112 | + new BigDecimal(3790.76), |
| 113 | + |
| 114 | + "This olive folding bike features a carbon frame and 27.5 inch" + |
| 115 | + " wheels. This folding bike is perfect for compact storage and" + |
| 116 | + " transportation.", |
| 117 | + "new" |
| 118 | + ), |
| 119 | + new Bicycle( |
| 120 | + "Hall, Haley and Hayes", |
| 121 | + "Weekend Plus", |
| 122 | + new BigDecimal(2008.4), |
| 123 | + "The Hall, Haley and Hayes Weekend Plus provides a comfortable" + |
| 124 | + " ride. This blue kids bike features a steel frame and 29.0 inch" + |
| 125 | + " wheels.", |
| 126 | + "new" |
| 127 | + ), |
| 128 | + new Bicycle( |
| 129 | + "Peck-Carson", |
| 130 | + "Sun Hybrid", |
| 131 | + new BigDecimal(9874.95), |
| 132 | + "With its comfortable aluminum frame and 25 gears, this bicycle is" + |
| 133 | + " perfect for any terrain. The Peck-Carson Sun Hybrid provides a" + |
| 134 | + " comfortable ride.", |
| 135 | + "new" |
| 136 | + ), |
| 137 | + new Bicycle( |
| 138 | + "Fowler Ltd", |
| 139 | + "Weekend Trail", |
| 140 | + new BigDecimal(3833.71), |
| 141 | + "The Fowler Ltd Letter Trail is a comfortable choice for" + |
| 142 | + " transporting cargo. This cargo bike is perfect for transporting" + |
| 143 | + " cargo.", |
| 144 | + "refurbished" |
| 145 | + ) |
| 146 | + }; |
| 147 | + |
| 148 | + // STEP_START define_index |
| 149 | + SchemaField[] schema = { |
| 150 | + TextField.of("$.brand").as("brand"), |
| 151 | + TextField.of("$.model").as("model"), |
| 152 | + TextField.of("$.description").as("description"), |
| 153 | + NumericField.of("$.price").as("price"), |
| 154 | + TagField.of("$.condition").as("condition") |
| 155 | + }; |
| 156 | + // STEP_END |
| 157 | + |
| 158 | + // STEP_START create_index |
| 159 | + jedis.ftCreate("idx:bicycle", |
| 160 | + FTCreateParams.createParams() |
| 161 | + .on(IndexDataType.JSON) |
| 162 | + .addPrefix("bicycle:"), |
| 163 | + schema |
| 164 | + ); |
| 165 | + // STEP_END |
| 166 | + // STEP_START add_documents |
| 167 | + for (int i = 0; i < bicycles.length; i++) { |
| 168 | + jedis.jsonSetWithEscape(String.format("bicycle:%d", i), bicycles[i]); |
| 169 | + } |
| 170 | + // STEP_END |
| 171 | + |
| 172 | + // STEP_START query_single_term_and_num_range |
| 173 | + Query query = new Query("folding @price:[1000 4000]"); |
| 174 | + List<Document> result = jedis.ftSearch("idx:bicycle", query).getDocuments(); |
| 175 | + System.out.println(result); |
| 176 | + // Prints: [id:bicycle:6, score: 1.0, payload:null, properties:[ |
| 177 | + // $={"brand":"Moore PLC","model":"Award Race","price":3790.76, |
| 178 | + // "description":"This olive folding bike features a carbon frame and 27.5 inch wheels. |
| 179 | + // This folding bike is perfect for compact storage and transportation.","condition":"new"}] |
| 180 | + // ] |
| 181 | + // STEP_END |
| 182 | + // REMOVE_START |
| 183 | + assertEquals("Validate folding bike id", "bicycle:6", result.get(0).getId()); |
| 184 | + // REMOVE_END |
| 185 | + |
| 186 | + // STEP_START query_single_term_limit_fields |
| 187 | + Query cargo_query = new Query("cargo").returnFields("price"); |
| 188 | + List<Document> cargo_result = jedis.ftSearch( |
| 189 | + "idx:bicycle", cargo_query).getDocuments(); |
| 190 | + System.out.println(cargo_result); |
| 191 | + // Prints: [id:bicycle:9, score: 1.0, payload:null, properties:[price=3833.71]] |
| 192 | + // STEP_END |
| 193 | + // REMOVE_START |
| 194 | + assertEquals("Validate cargo bike id", "bicycle:9", cargo_result.get(0).getId()); |
| 195 | + // REMOVE_END |
| 196 | + |
| 197 | + // STEP_START simple_aggregation |
| 198 | + AggregationBuilder ab = new AggregationBuilder("*") |
| 199 | + .groupBy("@condition", Reducers.count().as("count")); |
| 200 | + AggregationResult ar = jedis.ftAggregate("idx:bicycle", ab); |
| 201 | + |
| 202 | + for (int i = 0; i < ar.getTotalResults(); i++) { |
| 203 | + System.out.println( |
| 204 | + ar.getRow(i).getString("condition") |
| 205 | + + " - " |
| 206 | + + ar.getRow(i).getString("count")); |
| 207 | + } |
| 208 | + // Prints: |
| 209 | + // refurbished - 1 |
| 210 | + // used - 5 |
| 211 | + // new - 4 |
| 212 | + // STEP_END |
| 213 | + // REMOVE_START |
| 214 | + assertEquals("Validate aggregation results", 3, ar.getTotalResults()); |
| 215 | + // REMOVE_END |
| 216 | + } |
| 217 | +} |
| 218 | +//HIDE_END |
0 commit comments