Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
!.circleci/**
.idea/
target
bin
20 changes: 18 additions & 2 deletions src/main/java/io/redisearch/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class Client implements io.redisearch.Client {
private final String indexName;
private final byte[] endocdedIndexName;
private final Pool<Jedis> pool;
private final Jedis jedis;

protected Commands.CommandProvider commands;

Expand All @@ -33,10 +34,19 @@ public class Client implements io.redisearch.Client {
public Client(String indexName, Pool<Jedis> pool) {
this.indexName = indexName;
this.endocdedIndexName = SafeEncoder.encode(indexName);
this.jedis = null;
this.pool = pool;
this.commands = new Commands.SingleNodeCommands();
}

public Client(String indexName, Jedis jedis) {
this.indexName = indexName;
this.endocdedIndexName = SafeEncoder.encode(indexName);
this.jedis = jedis;
this.pool = null;
this.commands = new Commands.SingleNodeCommands();
}

/**
* Create a new client to a RediSearch index
*
Expand Down Expand Up @@ -141,7 +151,7 @@ Jedis _conn() {

@Override
public Jedis connection() {
return pool.getResource();
return jedis != null ? jedis : pool.getResource();
}

private BinaryClient sendCommand(Jedis conn, ProtocolCommand provider, String... args) {
Expand Down Expand Up @@ -277,6 +287,7 @@ public String getConfig(ConfigOption option) {
*
* @return all configs map
*/
@SuppressWarnings("unchecked")
@Override
public Map<String, String> getAllConfig() {
try (Jedis conn = connection()) {
Expand Down Expand Up @@ -1232,6 +1243,11 @@ public void serializeRedisArgs(List<String> args) {

@Override
public void close() {
this.pool.close();
if (pool != null) {
pool.close();
}
if (jedis != null) {
jedis.close();
}
}
}
42 changes: 42 additions & 0 deletions src/test/java/io/redisearch/client/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.redisearch.Schema.TagField;
import io.redisearch.Schema.TextField;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.exceptions.JedisDataException;
import redis.clients.jedis.util.SafeEncoder;

Expand Down Expand Up @@ -1332,4 +1333,45 @@ public void testSyn() throws Exception {
expected.put("child", Arrays.asList(String.valueOf(group1), String.valueOf(group2)));
assertEquals(expected, dump);
}

@Test
public void testCreateClientWithJedisInstance() throws Exception {
try (Jedis jedis = new Jedis(Protocol.DEFAULT_HOST, Protocol.DEFAULT_PORT)) {
Client cl = new Client("test_idx", jedis);
cl.connection().flushDB();

Schema sc = new Schema().addTextField("first", 1.0).addTextField("last", 1.0).addNumericField("age");
IndexDefinition rule = new IndexDefinition() //
.setFilter("@age>16") //
.setPrefixes(new String[]{"student:", "pupil:"});

try {
assertTrue(cl.createIndex(sc, Client.IndexOptions.defaultOptions().setDefinition(rule)));
} catch (JedisDataException e) {
// ON was only supported from RediSearch 2.0
assertEquals("Unknown argument `ON`", e.getMessage());
return;
}

jedis.hset("profesor:5555", toMap("first", "Albert", "last", "Blue", "age", "55"));
jedis.hset("student:1111", toMap("first", "Joe", "last", "Dod", "age", "18"));
jedis.hset("pupil:2222", toMap("first", "Jen", "last", "Rod", "age", "14"));
jedis.hset("student:3333", toMap("first", "El", "last", "Mark", "age", "17"));
jedis.hset("pupil:4444", toMap("first", "Pat", "last", "Shu", "age", "21"));
jedis.hset("student:5555", toMap("first", "Joen", "last", "Ko", "age", "20"));
jedis.hset("teacher:6666", toMap("first", "Pat", "last", "Rod", "age", "20"));

SearchResult noFilters = cl.search(new Query());
assertEquals(4, noFilters.totalResults);

SearchResult res1 = cl.search(new Query("@first:Jo*"));
assertEquals(2, res1.totalResults);

SearchResult res2 = cl.search(new Query("@first:Pat"));
assertEquals(1, res2.totalResults);

SearchResult res3 = cl.search(new Query("@last:Rod"));
assertEquals(0, res3.totalResults);
}
}
}