Skip to content

Commit

Permalink
Support GeoCoordinate as value in JRediSearch
Browse files Browse the repository at this point in the history
  • Loading branch information
sazzad16 committed Aug 26, 2021
1 parent 2bb0e1c commit ac3bb5d
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 8 deletions.
26 changes: 20 additions & 6 deletions src/main/java/io/redisearch/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -553,15 +553,29 @@ private BinaryClient addDocument(Document doc, AddOptions options, Jedis conn) {
}

args.add(Keywords.FIELDS.getRaw());
String key = null;
for (Map.Entry<String, Object> ent : doc.getProperties()) {
key = ent.getKey();
for (Map.Entry<String, Object> entry : doc.getProperties()) {
String key = entry.getKey();
args.add(SafeEncoder.encode(key));
Object value = ent.getValue();
Object value = entry.getValue();
if (value == null) {
throw new NullPointerException( "Document attribute '"+ key +"' is null. (Remove it, or set a value)" );
throw new NullPointerException("Document attribute '" + key + "' is null. (Remove it, or set a value)");
}
args.add(value instanceof byte[] ? (byte[])value : SafeEncoder.encode(value.toString()));
byte[] binaryValue;
if (value instanceof redis.clients.jedis.GeoCoordinate) {
redis.clients.jedis.GeoCoordinate geo = (redis.clients.jedis.GeoCoordinate) value;
byte[] lon = Protocol.toByteArray(geo.getLongitude());
byte[] lat = Protocol.toByteArray(geo.getLatitude());
binaryValue = new byte[lon.length + lat.length + 1];
System.arraycopy(lon, 0, binaryValue, 0, lon.length);
binaryValue[lon.length] = ',';
System.arraycopy(lat, 0, binaryValue, lon.length + 1, lat.length);
args.add(binaryValue);
} else if (value instanceof byte[]) {
binaryValue = (byte[]) value;
} else {
binaryValue = SafeEncoder.encode(value.toString());
}
args.add(binaryValue);
}

return sendCommand(conn, commands.getAddCommand(), args.toArray(new byte[args.size()][]));
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/io/redisearch/client/util/ClientUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@ public static Map<String, String> toStringMap(Map<String, Object> input) {
for (Map.Entry<String, Object> entry : input.entrySet()) {
String key = entry.getKey();
Object obj = entry.getValue();
String str = obj.toString();
String str;
if (obj instanceof redis.clients.jedis.GeoCoordinate) {
redis.clients.jedis.GeoCoordinate geo = (redis.clients.jedis.GeoCoordinate) obj;
str = geo.getLongitude() + "," + geo.getLatitude();
} else if (obj instanceof String) {
str = (String) obj;
} else {
str = obj.toString();
}
output.put(key, str);
}
return output;
Expand Down
23 changes: 22 additions & 1 deletion src/test/java/io/redisearch/client/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,6 @@ public void testStopwords() throws Exception {
assertEquals(1, cl.search(new Query("to be or not to be")).totalResults);
}


@Test
public void testGeoFilter() throws Exception {
Client cl = getDefaultClient();
Expand Down Expand Up @@ -347,6 +346,28 @@ public void testGeoFilter() throws Exception {
assertEquals(2, res.totalResults);
}

@Test
public void geoFilterAndGeoCoordinateObject() throws Exception {
Schema schema = new Schema().addTextField("title", 1.0).addGeoField("loc");
assertTrue(search.createIndex(schema, Client.IndexOptions.defaultOptions()));

Map<String, Object> fields = new HashMap<>();
fields.put("title", "hello world");
fields.put("loc", new redis.clients.jedis.GeoCoordinate(-0.441, 51.458));
assertTrue(search.addDocument("doc1", fields));
fields.put("loc", "-0.1,51.2");
fields.put("loc", new redis.clients.jedis.GeoCoordinate(-0.1, 51.2));
assertTrue(search.addDocument("doc2", fields));

SearchResult res = search.search(new Query("hello world").addFilter(
new Query.GeoFilter("loc", -0.44, 51.45, 10, Query.GeoFilter.KILOMETERS)));
assertEquals(1, res.totalResults);

res = search.search(new Query("hello world").addFilter(
new Query.GeoFilter("loc", -0.44, 51.45, 100, Query.GeoFilter.KILOMETERS)));
assertEquals(2, res.totalResults);
}

// TODO: This test was broken in master branch
@Test
public void testPayloads() throws Exception {
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/io/redisearch/client/util/UtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.redisearch.client.util;

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

public class UtilTest {

@Test
public void geoCoordinate() {
Map<String, Object> objectMap = Collections.singletonMap("geo", new redis.clients.jedis.GeoCoordinate(22.5, 45.0));
Map<String, String> stringMap = Collections.singletonMap("geo", (22.5 + "," + 45.0));
Assert.assertEquals(stringMap, ClientUtil.toStringMap(objectMap));
}
}

0 comments on commit ac3bb5d

Please sign in to comment.