LettuSearch
Java client for RediSearch based on Lettuce
|
LettuSearch has been merged into multi-module client Mesclun. Updates will now happen in Mesclun. |
Latest release: https://github.com/RediSearch/lettusearch/releases/latest
Getting Started
Add LettuSearch to your application dependencies:
Gradle
dependencies {
implementation 'com.redislabs:lettusearch:x.y.x'
}
Maven
<dependency>
<groupId>com.redislabs</groupId>
<artifactId>lettusearch</artifactId>
<version>x.y.z</version>
</dependency>
Basic Usage
RediSearchClient client = RediSearchClient.create(RedisURI.create(host, port)); // (1)
StatefulRediSearchConnection<String, String> connection = client.connect(); // (2)
RediSearchCommands<String, String> commands = connection.sync(); // (3)
commands.create("beers", Field.text("name").build()); // (4)
commands.hmset("beer:1", Map.of("name", "Chouffe")); // (5)
SearchResults<String, String> results = commands.search("beers", "chou*"); // (6)
System.out.println("Found " + results.getCount() + " documents matching query");
for (Document<String, String> doc : results) {
System.out.println(doc);
}
-
Create a RediSearch client
-
Connect to RediSearch
-
Use sync, async, or reactive commands
-
Create an index
-
Add a document to the index
-
Search the index
Pipelining
RediSearchAsyncCommands<String, String> commands = connection.async(); // (1)
commands.setAutoFlushCommands(false); // (2)
List<RedisFuture<?>> futures = new ArrayList<>();
for (java.util.Map<String, String> doc : docs) {
RedisFuture<String> future = commands.hmset(doc.get("id"), doc);// (3)
futures.add(future); // (4)
}
commands.flushCommands(); // (5)
for (RedisFuture<?> future : futures) {
try {
future.get(1, TimeUnit.SECONDS); // (6)
} catch (InterruptedException e) {
log.debug("Command interrupted", e);
} catch (ExecutionException e) {
log.error("Command execution returned an error", e);
} catch (TimeoutException e) {
log.error("Command timed out", e);
}
}
commands.setAutoFlushCommands(true); // (7)
-
Use async commands
-
Disable automatic flushing of commands
-
Call commands to be executed in the pipeline
-
Add command execution future to the list
-
Flush commands
-
Wait for response from each future
-
Disable automatic flushing of commands
Connection pooling
GenericObjectPoolConfig<StatefulRediSearchConnection<String, String>> config = new GenericObjectPoolConfig<>(); // (1)
config.setMaxTotal(8);
// config.setX...
GenericObjectPool<StatefulRediSearchConnection<String, String>> pool = ConnectionPoolSupport.createGenericObjectPool(client::connect, config); // (2)
try (StatefulRediSearchConnection<String, String> connection = pool.borrowObject()) { // (3)
RediSearchAsyncCommands<String, String> commands = connection.async(); // (4)
// ...
}
-
Create a connection pool configuration
-
Create the connection pool
-
In worker threads, get connections in a try-with statement to automatically return them to the pool
-
Use sync or async commands