|
| 1 | +// EXAMPLE: geoindex |
| 2 | +// REMOVE_START |
| 3 | +package io.redis.examples; |
| 4 | + |
| 5 | +import org.junit.Assert; |
| 6 | +import org.junit.Test; |
| 7 | +// REMOVE_END |
| 8 | + |
| 9 | +// HIDE_START |
| 10 | +import org.json.JSONObject; |
| 11 | + |
| 12 | +import redis.clients.jedis.UnifiedJedis; |
| 13 | +import redis.clients.jedis.json.Path2; |
| 14 | +import redis.clients.jedis.search.Document; |
| 15 | +import redis.clients.jedis.search.FTCreateParams; |
| 16 | +import redis.clients.jedis.search.FTSearchParams; |
| 17 | +import redis.clients.jedis.search.IndexDataType; |
| 18 | +import redis.clients.jedis.search.schemafields.*; |
| 19 | +import redis.clients.jedis.search.schemafields.GeoShapeField.CoordinateSystem; |
| 20 | +import redis.clients.jedis.search.SearchResult; |
| 21 | +import redis.clients.jedis.exceptions.JedisDataException; |
| 22 | +// HIDE_END |
| 23 | + |
| 24 | +// HIDE_START |
| 25 | +public class GeoIndexExample { |
| 26 | + |
| 27 | + @Test |
| 28 | + public void run() { |
| 29 | + UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379"); |
| 30 | + //REMOVE_START |
| 31 | + // Clear any keys here before using them in tests. |
| 32 | + try { |
| 33 | + jedis.ftDropIndex("productidx"); |
| 34 | + } catch (JedisDataException j) {} |
| 35 | + |
| 36 | + try { |
| 37 | + jedis.ftDropIndex("geomidx"); |
| 38 | + } catch (JedisDataException j) {} |
| 39 | + |
| 40 | + jedis.del("product:46885", "product:46886", "shape:1", "shape:2", "shape:3", "shape:4"); |
| 41 | + //REMOVE_END |
| 42 | +// HIDE_END |
| 43 | + |
| 44 | + // STEP_START create_geo_idx |
| 45 | + SchemaField[] geoSchema = { |
| 46 | + GeoField.of("$.location").as("location") |
| 47 | + }; |
| 48 | + |
| 49 | + String geoIdxCreateResult = jedis.ftCreate("productidx", |
| 50 | + FTCreateParams.createParams() |
| 51 | + .on(IndexDataType.JSON) |
| 52 | + .addPrefix("product:"), |
| 53 | + geoSchema |
| 54 | + ); |
| 55 | + // STEP_END |
| 56 | + // REMOVE_START |
| 57 | + Assert.assertEquals("OK", geoIdxCreateResult); |
| 58 | + // REMOVE_END |
| 59 | + |
| 60 | + // STEP_START add_geo_json |
| 61 | + JSONObject prd46885 = new JSONObject() |
| 62 | + .put("description", "Navy Blue Slippers") |
| 63 | + .put("price", 45.99) |
| 64 | + .put("city", "Denver") |
| 65 | + .put("location", "-104.991531, 39.742043"); |
| 66 | + |
| 67 | + String jsonAddResult1 = jedis.jsonSet("product:46885", new Path2("$"), prd46885); |
| 68 | + System.out.println(jsonAddResult1); // >>> OK |
| 69 | + |
| 70 | + JSONObject prd46886 = new JSONObject() |
| 71 | + .put("description", "Bright Green Socks") |
| 72 | + .put("price", 25.50) |
| 73 | + .put("city", "Fort Collins") |
| 74 | + .put("location", "-105.0618814,40.5150098"); |
| 75 | + |
| 76 | + String jsonAddResult2 = jedis.jsonSet("product:46886", new Path2("$"), prd46886); |
| 77 | + System.out.println(jsonAddResult2); // >>> OK |
| 78 | + // STEP_END |
| 79 | + // REMOVE_START |
| 80 | + Assert.assertEquals("OK", jsonAddResult1); |
| 81 | + Assert.assertEquals("OK", jsonAddResult2); |
| 82 | + // REMOVE_END |
| 83 | + |
| 84 | + // STEP_START geo_query |
| 85 | + SearchResult geoResult = jedis.ftSearch("productidx", |
| 86 | + "@location:[-104.800644 38.846127 100 mi]" |
| 87 | + ); |
| 88 | + |
| 89 | + System.out.println(geoResult.getTotalResults()); // >>> 1 |
| 90 | + |
| 91 | + for (Document doc: geoResult.getDocuments()) { |
| 92 | + System.out.println(doc.getId()); |
| 93 | + } |
| 94 | + // >>> product:46885 |
| 95 | + // STEP_END |
| 96 | + // REMOVE_START |
| 97 | + Assert.assertEquals("OK", jsonAddResult1); |
| 98 | + Assert.assertEquals("OK", jsonAddResult2); |
| 99 | + Assert.assertEquals("product:46885", geoResult.getDocuments().get(0).getId()); |
| 100 | + // REMOVE_END |
| 101 | + |
| 102 | + // STEP_START create_gshape_idx |
| 103 | + SchemaField[] geomSchema = { |
| 104 | + TextField.of("$.name").as("name"), |
| 105 | + GeoShapeField.of("$.geom", CoordinateSystem.FLAT).as("geom") |
| 106 | + }; |
| 107 | + |
| 108 | + String geomIndexCreateResult = jedis.ftCreate("geomidx", |
| 109 | + FTCreateParams.createParams() |
| 110 | + .on(IndexDataType.JSON) |
| 111 | + .addPrefix("shape"), |
| 112 | + geomSchema |
| 113 | + ); |
| 114 | + System.out.println(geomIndexCreateResult); // >>> OK |
| 115 | + // STEP_END |
| 116 | + // REMOVE_START |
| 117 | + Assert.assertEquals("OK", geomIndexCreateResult); |
| 118 | + // REMOVE_END |
| 119 | + |
| 120 | + // STEP_START add_gshape_json |
| 121 | + JSONObject shape1 = new JSONObject() |
| 122 | + .put("name", "Green Square") |
| 123 | + .put("geom", "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))"); |
| 124 | + |
| 125 | + String gmJsonRes1 = jedis.jsonSet("shape:1", new Path2("$"), shape1); |
| 126 | + System.out.println(gmJsonRes1); // >>> OK |
| 127 | + |
| 128 | + JSONObject shape2 = new JSONObject() |
| 129 | + .put("name", "Red Rectangle") |
| 130 | + .put("geom", "POLYGON ((2 2.5, 2 3.5, 3.5 3.5, 3.5 2.5, 2 2.5))"); |
| 131 | + |
| 132 | + String gmJsonRes2 = jedis.jsonSet("shape:2", new Path2("$"), shape2); |
| 133 | + System.out.println(gmJsonRes2); // >>> OK |
| 134 | + |
| 135 | + JSONObject shape3 = new JSONObject() |
| 136 | + .put("name", "Blue Triangle") |
| 137 | + .put("geom", "POLYGON ((3.5 1, 3.75 2, 4 1, 3.5 1))"); |
| 138 | + |
| 139 | + String gmJsonRes3 = jedis.jsonSet("shape:3", new Path2("$"), shape3); |
| 140 | + System.out.println(gmJsonRes3); // >>> OK |
| 141 | + |
| 142 | + JSONObject shape4 = new JSONObject() |
| 143 | + .put("name", "Purple Point") |
| 144 | + .put("geom", "POINT (2 2)"); |
| 145 | + |
| 146 | + String gmJsonRes4 = jedis.jsonSet("shape:4", new Path2("$"), shape4); |
| 147 | + System.out.println(gmJsonRes4); // >>> OK |
| 148 | + // STEP_END |
| 149 | + // REMOVE_START |
| 150 | + Assert.assertEquals("OK", gmJsonRes1); |
| 151 | + Assert.assertEquals("OK", gmJsonRes2); |
| 152 | + Assert.assertEquals("OK", gmJsonRes3); |
| 153 | + Assert.assertEquals("OK", gmJsonRes4); |
| 154 | + // REMOVE_END |
| 155 | + |
| 156 | + // STEP_START gshape_query |
| 157 | + SearchResult geomResult = jedis.ftSearch("geomidx", |
| 158 | + "(-@name:(Green Square) @geom:[WITHIN $qshape])", |
| 159 | + FTSearchParams.searchParams() |
| 160 | + .addParam("qshape", "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))") |
| 161 | + .dialect(4) |
| 162 | + .limit(0, 1) |
| 163 | + ); |
| 164 | + System.out.println(geomResult.getTotalResults()); // >>> 1 |
| 165 | + |
| 166 | + for (Document doc: geomResult.getDocuments()) { |
| 167 | + System.out.println(doc.getId()); |
| 168 | + } |
| 169 | + // shape:4 |
| 170 | + // STEP_END |
| 171 | + // REMOVE_START |
| 172 | + Assert.assertEquals(1, geomResult.getTotalResults()); |
| 173 | + Assert.assertEquals("shape:4", geomResult.getDocuments().get(0).getId()); |
| 174 | + // REMOVE_END |
| 175 | + // HIDE_START |
| 176 | + |
| 177 | + jedis.close(); |
| 178 | + } |
| 179 | +} |
| 180 | +// HIDE_END |
0 commit comments