Skip to content

Commit

Permalink
Use simple version of HSET
Browse files Browse the repository at this point in the history
for easier readability
  • Loading branch information
sazzad16 committed Oct 17, 2023
1 parent 4cd5a6a commit c5e5890
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package redis.clients.jedis.examples;

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

import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
Expand All @@ -11,13 +14,10 @@
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;

import static java.util.Collections.singletonMap;
import static org.junit.Assert.assertEquals;
import static redis.clients.jedis.search.RediSearchUtil.toStringMap;

/**
* As of RediSearch 2.8.4, advanced GEO querying with GEOSHAPE fields is supported.
*
Expand Down Expand Up @@ -64,15 +64,17 @@ public static void main(String[] args) {
new Coordinate(34.9100, 29.7001), new Coordinate(34.9001, 29.7001)}
);

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

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", toStringMap(singletonMap("geometry", large))); // setting data
// client.hset("large", RediSearchUtil.toStringMap(Collections.singletonMap("geometry", large))); // setting data
client.hset("large", "geometry", large.toString()); // simplified setting data

// searching
final Polygon within = factory.createPolygon(
Expand All @@ -88,14 +90,14 @@ public static void main(String[] args) {
.addParam("poly", within)
.dialect(3) // DIALECT '3' is required for this query
);
assertEquals(1, res.getTotalResults());
assertEquals(1, res.getDocuments().size());
Assert.assertEquals(1, res.getTotalResults());
Assert.assertEquals(1, res.getDocuments().size());

// We can parse geometry objects with WKTReader
try {
final WKTReader reader = new WKTReader();
Geometry object = reader.read(res.getDocuments().get(0).getString("geometry"));
assertEquals(small, object);
Assert.assertEquals(small, object);
} catch (ParseException ex) {
ex.printStackTrace(System.err);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,12 +351,12 @@ 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", RediSearchUtil.toStringMap(Collections.singletonMap("geom", small)));
client.hset("small", "geom", small.toString());

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", RediSearchUtil.toStringMap(Collections.singletonMap("geom", large)));
client.hset("large", "geom", large.toString());

// within condition
final Polygon within = factory.createPolygon(new Coordinate[]{new Coordinate(34.9000, 29.7000),
Expand All @@ -381,7 +381,7 @@ public void geoShapeFilterSpherical() throws ParseException {

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

res = client.ftSearch(index, "@geom:[within $poly]",
FTSearchParams.searchParams().addParam("poly", within).dialect(3));
Expand All @@ -399,11 +399,11 @@ public void geoShapeFilterFlat() throws ParseException {
// 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", RediSearchUtil.toStringMap(Collections.singletonMap("geom", small)));
client.hset("small", "geom", small.toString());

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", RediSearchUtil.toStringMap(Collections.singletonMap("geom", large)));
client.hset("large", "geom", large.toString());

// within condition
final Polygon within = factory.createPolygon(new Coordinate[]{new Coordinate(0, 0),
Expand All @@ -426,7 +426,7 @@ public void geoShapeFilterFlat() throws ParseException {

// point type
final Point point = factory.createPoint(new Coordinate(10, 10));
client.hset("point", RediSearchUtil.toStringMap(Collections.singletonMap("geom", point)));
client.hset("point", "geom", point.toString());

res = client.ftSearch(index, "@geom:[within $poly]",
FTSearchParams.searchParams().addParam("poly", within).dialect(3));
Expand Down Expand Up @@ -1130,9 +1130,9 @@ public void maxPrefixExpansionSearchProfile() {
client.ftConfigSet(configParam, "2");

assertOK(client.ftCreate(index, TextField.of("t")));
client.hset("1", Collections.singletonMap("t", "foo1"));
client.hset("2", Collections.singletonMap("t", "foo2"));
client.hset("3", Collections.singletonMap("t", "foo3"));
client.hset("1", "t", "foo1");
client.hset("2", "t", "foo2");
client.hset("3", "t", "foo3");

Map.Entry<SearchResult, Map<String, Object>> reply = client.ftProfileSearch(index,
FTProfileParams.profileParams(), "foo*", FTSearchParams.searchParams().limit(0, 0));
Expand All @@ -1152,8 +1152,8 @@ public void maxPrefixExpansionSearchProfile() {
@Test
public void noContentSearchProfile() {
assertOK(client.ftCreate(index, TextField.of("t")));
client.hset("1", Collections.singletonMap("t", "foo"));
client.hset("2", Collections.singletonMap("t", "bar"));
client.hset("1", "t", "foo");
client.hset("2", "t", "bar");

Map.Entry<SearchResult, Map<String, Object>> profile = client.ftProfileSearch(index,
FTProfileParams.profileParams(), "foo -@t:baz", FTSearchParams.searchParams().noContent());
Expand All @@ -1179,8 +1179,8 @@ public void noContentSearchProfile() {
@Test
public void deepReplySearchProfile() {
assertOK(client.ftCreate(index, TextField.of("t")));
client.hset("1", Collections.singletonMap("t", "hello"));
client.hset("2", Collections.singletonMap("t", "world"));
client.hset("1", "t", "hello");
client.hset("2", "t", "world");

Map.Entry<SearchResult, Map<String, Object>> profile
= client.ftProfileSearch(index, FTProfileParams.profileParams(),
Expand Down Expand Up @@ -1217,10 +1217,10 @@ public void deepReplySearchProfile() {
@Test
public void limitedSearchProfile() {
assertOK(client.ftCreate(index, TextField.of("t")));
client.hset("1", Collections.singletonMap("t", "hello"));
client.hset("2", Collections.singletonMap("t", "hell"));
client.hset("3", Collections.singletonMap("t", "help"));
client.hset("4", Collections.singletonMap("t", "helowa"));
client.hset("1", "t", "hello");
client.hset("2", "t", "hell");
client.hset("3", "t", "help");
client.hset("4", "t", "helowa");

Map.Entry<SearchResult, Map<String, Object>> profile = client.ftProfileSearch(index,
FTProfileParams.profileParams().limited(), "%hell% hel*", FTSearchParams.searchParams().noContent());
Expand Down

0 comments on commit c5e5890

Please sign in to comment.