diff --git a/src/main/java/io/redisearch/client/util/ClientUtil.java b/src/main/java/io/redisearch/client/util/ClientUtil.java new file mode 100644 index 0000000..440dbff --- /dev/null +++ b/src/main/java/io/redisearch/client/util/ClientUtil.java @@ -0,0 +1,22 @@ +package io.redisearch.client.util; + +import java.util.HashMap; +import java.util.Map; + +public class ClientUtil { + + public static Map toStringMap(Map input) { + Map output = new HashMap<>(input.size()); + for (Map.Entry entry : input.entrySet()) { + String key = entry.getKey(); + Object obj = entry.getValue(); + String str = obj.toString(); + output.put(key, str); + } + return output; + } + + private ClientUtil() { + throw new InstantiationError("Must not instantiate this class"); + } +} diff --git a/src/test/java/io/redisearch/client/ClientTest.java b/src/test/java/io/redisearch/client/ClientTest.java index 8ea7034..0034c05 100644 --- a/src/test/java/io/redisearch/client/ClientTest.java +++ b/src/test/java/io/redisearch/client/ClientTest.java @@ -17,6 +17,7 @@ import org.junit.BeforeClass; import org.junit.Test; +import static io.redisearch.client.util.ClientUtil.toStringMap; import static org.junit.Assert.*; public class ClientTest extends TestBase { @@ -32,6 +33,14 @@ public static void tearDown() { TestBase.tearDown(); } + private static Map toMap(Object... values) { + Map map = new HashMap<>(); + for (int i = 0; i < values.length; i += 2) { + map.put((String) values[i], values[i + 1]); + } + return map; + } + private static Map toMap(String... values) { Map map = new HashMap<>(); for (int i = 0; i < values.length; i += 2) { @@ -79,6 +88,31 @@ public void creatDefinion() throws Exception { assertEquals(0, res3.totalResults); } + @Test + public void withObjectMap() throws Exception { + Schema sc = new Schema().addTextField("first", 1.0).addTextField("last", 1.0).addNumericField("age"); + assertTrue(search.createIndex(sc, Client.IndexOptions.defaultOptions())); + + try (Jedis jedis = search.connection()) { + jedis.hset("student:1111", toStringMap(toMap("first", "Joe", "last", "Dod", "age", 18))); + jedis.hset("student:3333", toStringMap(toMap("first", "El", "last", "Mark", "age", 17))); + jedis.hset("pupil:4444", toStringMap(toMap("first", "Pat", "last", "Shu", "age", 21))); + jedis.hset("student:5555", toStringMap(toMap("first", "Joen", "last", "Ko", "age", 20))); + } + + SearchResult noFilters = search.search(new Query()); + assertEquals(4, noFilters.totalResults); + + SearchResult res1 = search.search(new Query("@first:Jo*")); + assertEquals(2, res1.totalResults); + + SearchResult res2 = search.search(new Query("@first:Pat")); + assertEquals(1, res2.totalResults); + + SearchResult res3 = search.search(new Query("@last:Rod")); + assertEquals(0, res3.totalResults); + } + @Test public void createWithFieldNames() throws Exception { Schema sc = new Schema().addField(new TextField(FieldName.of("first").as("given")))