|
9 | 9 | import org.junit.BeforeClass;
|
10 | 10 | import org.junit.Test;
|
11 | 11 |
|
| 12 | +import org.locationtech.jts.geom.Coordinate; |
| 13 | +import org.locationtech.jts.geom.GeometryFactory; |
| 14 | +import org.locationtech.jts.geom.Point; |
| 15 | +import org.locationtech.jts.geom.Polygon; |
| 16 | +import org.locationtech.jts.io.ParseException; |
| 17 | +import org.locationtech.jts.io.WKTReader; |
| 18 | + |
12 | 19 | import redis.clients.jedis.GeoCoordinate;
|
13 | 20 | import redis.clients.jedis.RedisProtocol;
|
14 | 21 | import redis.clients.jedis.args.GeoUnit;
|
@@ -333,6 +340,100 @@ public void geoFilterAndGeoCoordinateObject() {
|
333 | 340 | assertEquals(2, res.getTotalResults());
|
334 | 341 | }
|
335 | 342 |
|
| 343 | + @Test |
| 344 | + public void geoShapeFilterSpherical() throws ParseException { |
| 345 | + final WKTReader reader = new WKTReader(); |
| 346 | + final GeometryFactory factory = new GeometryFactory(); |
| 347 | + |
| 348 | + assertOK(client.ftCreate(index, GeoShapeField.of("geom", GeoShapeField.CoordinateSystem.SPHERICAL))); |
| 349 | + |
| 350 | + // polygon type |
| 351 | + final Polygon small = factory.createPolygon(new Coordinate[]{new Coordinate(34.9001, 29.7001), |
| 352 | + new Coordinate(34.9001, 29.7100), new Coordinate(34.9100, 29.7100), |
| 353 | + new Coordinate(34.9100, 29.7001), new Coordinate(34.9001, 29.7001)}); |
| 354 | + client.hset("small", RediSearchUtil.toStringMap(Collections.singletonMap("geom", small))); |
| 355 | + |
| 356 | + final Polygon large = factory.createPolygon(new Coordinate[]{new Coordinate(34.9001, 29.7001), |
| 357 | + new Coordinate(34.9001, 29.7200), new Coordinate(34.9200, 29.7200), |
| 358 | + new Coordinate(34.9200, 29.7001), new Coordinate(34.9001, 29.7001)}); |
| 359 | + client.hset("large", RediSearchUtil.toStringMap(Collections.singletonMap("geom", large))); |
| 360 | + |
| 361 | + // within condition |
| 362 | + final Polygon within = factory.createPolygon(new Coordinate[]{new Coordinate(34.9000, 29.7000), |
| 363 | + new Coordinate(34.9000, 29.7150), new Coordinate(34.9150, 29.7150), |
| 364 | + new Coordinate(34.9150, 29.7000), new Coordinate(34.9000, 29.7000)}); |
| 365 | + |
| 366 | + SearchResult res = client.ftSearch(index, "@geom:[within $poly]", |
| 367 | + FTSearchParams.searchParams().addParam("poly", within).dialect(3)); |
| 368 | + assertEquals(1, res.getTotalResults()); |
| 369 | + assertEquals(1, res.getDocuments().size()); |
| 370 | + assertEquals(small, reader.read(res.getDocuments().get(0).getString("geom"))); |
| 371 | + |
| 372 | + // contains condition |
| 373 | + final Polygon contains = factory.createPolygon(new Coordinate[]{new Coordinate(34.9002, 29.7002), |
| 374 | + new Coordinate(34.9002, 29.7050), new Coordinate(34.9050, 29.7050), |
| 375 | + new Coordinate(34.9050, 29.7002), new Coordinate(34.9002, 29.7002)}); |
| 376 | + |
| 377 | + res = client.ftSearch(index, "@geom:[contains $poly]", |
| 378 | + FTSearchParams.searchParams().addParam("poly", contains).dialect(3)); |
| 379 | + assertEquals(2, res.getTotalResults()); |
| 380 | + assertEquals(2, res.getDocuments().size()); |
| 381 | + |
| 382 | + // point type |
| 383 | + final Point point = factory.createPoint(new Coordinate(34.9010, 29.7010)); |
| 384 | + client.hset("point", RediSearchUtil.toStringMap(Collections.singletonMap("geom", point))); |
| 385 | + |
| 386 | + res = client.ftSearch(index, "@geom:[within $poly]", |
| 387 | + FTSearchParams.searchParams().addParam("poly", within).dialect(3)); |
| 388 | + assertEquals(2, res.getTotalResults()); |
| 389 | + assertEquals(2, res.getDocuments().size()); |
| 390 | + } |
| 391 | + |
| 392 | + @Test |
| 393 | + public void geoShapeFilterFlat() throws ParseException { |
| 394 | + final WKTReader reader = new WKTReader(); |
| 395 | + final GeometryFactory factory = new GeometryFactory(); |
| 396 | + |
| 397 | + assertOK(client.ftCreate(index, GeoShapeField.of("geom", GeoShapeField.CoordinateSystem.FLAT))); |
| 398 | + |
| 399 | + // polygon type |
| 400 | + final Polygon small = factory.createPolygon(new Coordinate[]{new Coordinate(1, 1), |
| 401 | + new Coordinate(1, 100), new Coordinate(100, 100), new Coordinate(100, 1), new Coordinate(1, 1)}); |
| 402 | + client.hset("small", RediSearchUtil.toStringMap(Collections.singletonMap("geom", small))); |
| 403 | + |
| 404 | + final Polygon large = factory.createPolygon(new Coordinate[]{new Coordinate(1, 1), |
| 405 | + new Coordinate(1, 200), new Coordinate(200, 200), new Coordinate(200, 1), new Coordinate(1, 1)}); |
| 406 | + client.hset("large", RediSearchUtil.toStringMap(Collections.singletonMap("geom", large))); |
| 407 | + |
| 408 | + // within condition |
| 409 | + final Polygon within = factory.createPolygon(new Coordinate[]{new Coordinate(0, 0), |
| 410 | + new Coordinate(0, 150), new Coordinate(150, 150), new Coordinate(150, 0), new Coordinate(0, 0)}); |
| 411 | + |
| 412 | + SearchResult res = client.ftSearch(index, "@geom:[within $poly]", |
| 413 | + FTSearchParams.searchParams().addParam("poly", within).dialect(3)); |
| 414 | + assertEquals(1, res.getTotalResults()); |
| 415 | + assertEquals(1, res.getDocuments().size()); |
| 416 | + assertEquals(small, reader.read(res.getDocuments().get(0).getString("geom"))); |
| 417 | + |
| 418 | + // contains condition |
| 419 | + final Polygon contains = factory.createPolygon(new Coordinate[]{new Coordinate(2, 2), |
| 420 | + new Coordinate(2, 50), new Coordinate(50, 50), new Coordinate(50, 2), new Coordinate(2, 2)}); |
| 421 | + |
| 422 | + res = client.ftSearch(index, "@geom:[contains $poly]", |
| 423 | + FTSearchParams.searchParams().addParam("poly", contains).dialect(3)); |
| 424 | + assertEquals(2, res.getTotalResults()); |
| 425 | + assertEquals(2, res.getDocuments().size()); |
| 426 | + |
| 427 | + // point type |
| 428 | + final Point point = factory.createPoint(new Coordinate(10, 10)); |
| 429 | + client.hset("point", RediSearchUtil.toStringMap(Collections.singletonMap("geom", point))); |
| 430 | + |
| 431 | + res = client.ftSearch(index, "@geom:[within $poly]", |
| 432 | + FTSearchParams.searchParams().addParam("poly", within).dialect(3)); |
| 433 | + assertEquals(2, res.getTotalResults()); |
| 434 | + assertEquals(2, res.getDocuments().size()); |
| 435 | + } |
| 436 | + |
336 | 437 | @Test
|
337 | 438 | public void testQueryFlags() {
|
338 | 439 | assertOK(client.ftCreate(index, TextField.of("title")));
|
|
0 commit comments