Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test: INTERSECTS and DIJOINT conditions support in GeoSearch #3862

Merged
merged 2 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ start: stunnel cleanup compile-module
echo "$$REDIS_UDS" | redis-server -
echo "$$REDIS_UNAVAILABLE_CONF" | redis-server -
redis-cli -a cluster --cluster create 127.0.0.1:7479 127.0.0.1:7480 127.0.0.1:7481 --cluster-yes
docker run -p 6479:6379 --name jedis-stack -d redis/redis-stack-server:edge
docker run -p 6479:6379 --name jedis-stack -d redis/redis-stack-server:7.4.0-rc1

cleanup:
- rm -vf /tmp/redis_cluster_node*.conf 2>/dev/null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package redis.clients.jedis.examples;

import java.util.Collections;
import org.junit.Assert;

import org.locationtech.jts.geom.Coordinate;
Expand All @@ -14,16 +13,21 @@
import redis.clients.jedis.JedisPooled;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.search.FTSearchParams;
import redis.clients.jedis.search.RediSearchUtil;
import redis.clients.jedis.search.SearchResult;
import redis.clients.jedis.search.schemafields.GeoShapeField;

/**
* As of RediSearch 2.8.4, advanced GEO querying with GEOSHAPE fields is supported.
* <p>
* Notes:
* <ul>
* <li>As of RediSearch 2.8.4, only POLYGON and POINT objects are supported.</li>
* <li>As of RediSearch 2.8.4, only WITHIN and CONTAINS conditions are supported.</li>
* <li>As of RedisStack 7.4.0, support for INTERSECTS and DISJOINT conditions are added.</li>
* </ul>
*
* Any object/library producing a
* <a href="https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry"> well-known
* text (WKT)</a> in {@code toString()} method can be used.
* Any object/library producing a <a href="https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry">
* well-known text (WKT)</a> in {@code toString()} method can be used.
*
* This example uses the <a href="https://github.com/locationtech/jts">JTS</a> library.
* <pre>
Expand Down Expand Up @@ -65,7 +69,8 @@ public static void main(String[] args) {
);

// client.hset("small", RediSearchUtil.toStringMap(Collections.singletonMap("geometry", small))); // setting data
client.hset("small", "geometry", small.toString()); // simplified setting data
// client.hset("small", "geometry", small.toString()); // simplified setting data
client.hsetObject("small", "geometry", small); // more simplified setting data

final Polygon large = factory.createPolygon(
new Coordinate[]{new Coordinate(34.9001, 29.7001),
Expand All @@ -74,7 +79,8 @@ public static void main(String[] args) {
);

// client.hset("large", RediSearchUtil.toStringMap(Collections.singletonMap("geometry", large))); // setting data
client.hset("large", "geometry", large.toString()); // simplified setting data
// client.hset("large", "geometry", large.toString()); // simplified setting data
client.hsetObject("large", "geometry", large); // more simplified setting data

// searching
final Polygon within = factory.createPolygon(
Expand All @@ -84,11 +90,10 @@ public static void main(String[] args) {
);

SearchResult res = client.ftSearch("geometry-index",
"@geometry:[within $poly]", // querying 'within' condition.
// RediSearch also supports 'contains' condition.
"@geometry:[within $poly]", // query string
FTSearchParams.searchParams()
.addParam("poly", within)
.dialect(3) // DIALECT '3' is required for this query
.dialect(3) // DIALECT '3' is required for this query
);
Assert.assertEquals(1, res.getTotalResults());
Assert.assertEquals(1, res.getDocuments().size());
Expand All @@ -98,10 +103,8 @@ public static void main(String[] args) {
final WKTReader reader = new WKTReader();
Geometry object = reader.read(res.getDocuments().get(0).getString("geometry"));
Assert.assertEquals(small, object);
} catch (ParseException ex) {
} catch (ParseException ex) { // WKTReader#read throws ParseException
ex.printStackTrace(System.err);
}
}

// Note: As of RediSearch 2.8.4, only POLYGON and POINT objects are supported.
}
Original file line number Diff line number Diff line change
Expand Up @@ -360,42 +360,42 @@ public void geoShapeFilterSpherical() throws ParseException {
final Polygon small = factory.createPolygon(new Coordinate[]{new Coordinate(34.9001, 29.7001),
new Coordinate(34.9001, 29.7100), new Coordinate(34.9100, 29.7100),
new Coordinate(34.9100, 29.7001), new Coordinate(34.9001, 29.7001)});
client.hset("small", "geom", small.toString());
client.hsetObject("small", "geom", small);

final Polygon large = factory.createPolygon(new Coordinate[]{new Coordinate(34.9001, 29.7001),
new Coordinate(34.9001, 29.7200), new Coordinate(34.9200, 29.7200),
new Coordinate(34.9200, 29.7001), new Coordinate(34.9001, 29.7001)});
client.hset("large", "geom", large.toString());
client.hsetObject("large", "geom", large);

// within condition
final Polygon within = factory.createPolygon(new Coordinate[]{new Coordinate(34.9000, 29.7000),
new Coordinate(34.9000, 29.7150), new Coordinate(34.9150, 29.7150),
new Coordinate(34.9150, 29.7000), new Coordinate(34.9000, 29.7000)});

SearchResult res = client.ftSearch(index, "@geom:[within $poly]",
SearchResult result = client.ftSearch(index, "@geom:[within $poly]",
FTSearchParams.searchParams().addParam("poly", within).dialect(3));
assertEquals(1, res.getTotalResults());
assertEquals(1, res.getDocuments().size());
assertEquals(small, reader.read(res.getDocuments().get(0).getString("geom")));
assertEquals(1, result.getTotalResults());
assertEquals(1, result.getDocuments().size());
assertEquals(small, reader.read(result.getDocuments().get(0).getString("geom")));

// contains condition
final Polygon contains = factory.createPolygon(new Coordinate[]{new Coordinate(34.9002, 29.7002),
new Coordinate(34.9002, 29.7050), new Coordinate(34.9050, 29.7050),
new Coordinate(34.9050, 29.7002), new Coordinate(34.9002, 29.7002)});

res = client.ftSearch(index, "@geom:[contains $poly]",
result = client.ftSearch(index, "@geom:[contains $poly]",
FTSearchParams.searchParams().addParam("poly", contains).dialect(3));
assertEquals(2, res.getTotalResults());
assertEquals(2, res.getDocuments().size());
assertEquals(2, result.getTotalResults());
assertEquals(2, result.getDocuments().size());

// point type
final Point point = factory.createPoint(new Coordinate(34.9010, 29.7010));
client.hset("point", "geom", point.toString());

res = client.ftSearch(index, "@geom:[within $poly]",
result = client.ftSearch(index, "@geom:[within $poly]",
FTSearchParams.searchParams().addParam("poly", within).dialect(3));
assertEquals(2, res.getTotalResults());
assertEquals(2, res.getDocuments().size());
assertEquals(2, result.getTotalResults());
assertEquals(2, result.getDocuments().size());
}

@Test
Expand All @@ -406,41 +406,57 @@ public void geoShapeFilterFlat() throws ParseException {
assertOK(client.ftCreate(index, GeoShapeField.of("geom", GeoShapeField.CoordinateSystem.FLAT)));

// polygon type
final Polygon small = factory.createPolygon(new Coordinate[]{new Coordinate(1, 1),
new Coordinate(1, 100), new Coordinate(100, 100), new Coordinate(100, 1), new Coordinate(1, 1)});
client.hset("small", "geom", small.toString());
final Polygon small = factory.createPolygon(new Coordinate[]{new Coordinate(20, 20),
new Coordinate(20, 100), new Coordinate(100, 100), new Coordinate(100, 20), new Coordinate(20, 20)});
client.hsetObject("small", "geom", small);

final Polygon large = factory.createPolygon(new Coordinate[]{new Coordinate(1, 1),
new Coordinate(1, 200), new Coordinate(200, 200), new Coordinate(200, 1), new Coordinate(1, 1)});
client.hset("large", "geom", large.toString());
final Polygon large = factory.createPolygon(new Coordinate[]{new Coordinate(10, 10),
new Coordinate(10, 200), new Coordinate(200, 200), new Coordinate(200, 10), new Coordinate(10, 10)});
client.hsetObject("large", "geom", large);

// within condition
final Polygon within = factory.createPolygon(new Coordinate[]{new Coordinate(0, 0),
new Coordinate(0, 150), new Coordinate(150, 150), new Coordinate(150, 0), new Coordinate(0, 0)});

SearchResult res = client.ftSearch(index, "@geom:[within $poly]",
SearchResult result = client.ftSearch(index, "@geom:[within $poly]",
FTSearchParams.searchParams().addParam("poly", within).dialect(3));
assertEquals(1, res.getTotalResults());
assertEquals(1, res.getDocuments().size());
assertEquals(small, reader.read(res.getDocuments().get(0).getString("geom")));
assertEquals(1, result.getTotalResults());
assertEquals(1, result.getDocuments().size());
assertEquals(small, reader.read(result.getDocuments().get(0).getString("geom")));

// contains condition
final Polygon contains = factory.createPolygon(new Coordinate[]{new Coordinate(2, 2),
new Coordinate(2, 50), new Coordinate(50, 50), new Coordinate(50, 2), new Coordinate(2, 2)});
final Polygon contains = factory.createPolygon(new Coordinate[]{new Coordinate(25, 25),
new Coordinate(25, 50), new Coordinate(50, 50), new Coordinate(50, 25), new Coordinate(25, 25)});

res = client.ftSearch(index, "@geom:[contains $poly]",
result = client.ftSearch(index, "@geom:[contains $poly]",
FTSearchParams.searchParams().addParam("poly", contains).dialect(3));
assertEquals(2, res.getTotalResults());
assertEquals(2, res.getDocuments().size());
assertEquals(2, result.getTotalResults());
assertEquals(2, result.getDocuments().size());

// intersects and disjoint
final Polygon disjointersect = factory.createPolygon(new Coordinate[]{new Coordinate(150, 150),
new Coordinate(150, 250), new Coordinate(250, 250), new Coordinate(250, 150), new Coordinate(150, 150)});

result = client.ftSearch(index, "@geom:[intersects $poly]",
FTSearchParams.searchParams().addParam("poly", disjointersect).dialect(3));
assertEquals(1, result.getTotalResults());
assertEquals(1, result.getDocuments().size());
assertEquals(large, reader.read(result.getDocuments().get(0).getString("geom")));

result = client.ftSearch(index, "@geom:[disjoint $poly]",
FTSearchParams.searchParams().addParam("poly", disjointersect).dialect(3));
assertEquals(1, result.getTotalResults());
assertEquals(1, result.getDocuments().size());
assertEquals(small, reader.read(result.getDocuments().get(0).getString("geom")));

// point type
final Point point = factory.createPoint(new Coordinate(10, 10));
client.hset("point", "geom", point.toString());
final Point point = factory.createPoint(new Coordinate(30, 30));
client.hsetObject("point", "geom", point);

res = client.ftSearch(index, "@geom:[within $poly]",
result = client.ftSearch(index, "@geom:[within $poly]",
FTSearchParams.searchParams().addParam("poly", within).dialect(3));
assertEquals(2, res.getTotalResults());
assertEquals(2, res.getDocuments().size());
assertEquals(2, result.getTotalResults());
assertEquals(2, result.getDocuments().size());
}

@Test
Expand Down
Loading