Skip to content
This repository has been archived by the owner on Apr 24, 2022. It is now read-only.

RediSearch/lettusearch

Repository files navigation

LettuSearch

License Latest Actions Codecov Language grade: Java Known Vulnerabilities

Forum Discord

Java client for RediSearch based on Lettuce

⚠️
LettuSearch has been merged into multi-module client LettuceMod. Updates will now happen in LettuceMod.

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);
}
  1. Create a RediSearch client

  2. Connect to RediSearch

  3. Use sync, async, or reactive commands

  4. Create an index

  5. Add a document to the index

  6. 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)
  1. Use async commands

  2. Disable automatic flushing of commands

  3. Call commands to be executed in the pipeline

  4. Add command execution future to the list

  5. Flush commands

  6. Wait for response from each future

  7. 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)
    // ...
}
  1. Create a connection pool configuration

  2. Create the connection pool

  3. In worker threads, get connections in a try-with statement to automatically return them to the pool

  4. Use sync or async commands