Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 22 additions & 0 deletions src/main/java/io/redisearch/client/util/ClientUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.redisearch.client.util;

import java.util.HashMap;
import java.util.Map;

public class ClientUtil {

public static Map<String, String> toStringMap(Map<String, Object> input) {
Map<String, String> output = new HashMap<>(input.size());
for (Map.Entry<String, Object> 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");
}
}
34 changes: 34 additions & 0 deletions src/test/java/io/redisearch/client/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -32,6 +33,14 @@ public static void tearDown() {
TestBase.tearDown();
}

private static Map<String, Object> toMap(Object... values) {
Map<String, Object> 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<String, String> toMap(String... values) {
Map<String, String> map = new HashMap<>();
for (int i = 0; i < values.length; i += 2) {
Expand Down Expand Up @@ -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 search() throws Exception {
Client cl = getDefaultClient();
Expand Down