lettusearch icon indicating copy to clipboard operation
lettusearch copied to clipboard

Lettuce-based client for RediSearch

= LettuSearch :project-repo: RediSearch/lettusearch :uri-repo: https://github.com/{project-repo} ifdef::env-github[] :badges: :tag: master :!toc-title: :tip-caption: :bulb: :note-caption: :paperclip: :important-caption: :heavy_exclamation_mark: :caution-caption: :fire: :warning-caption: :warning: endif::[]

// Badges ifdef::badges[] image:https://img.shields.io/github/license/RediSearch/lettusearch.svg["License", link="https://github.com/RediSearch/lettusearch"] image:https://img.shields.io/github/release/RediSearch/lettusearch.svg["Latest", link="https://github.com/RediSearch/lettusearch/releases/latest"] image:https://github.com/RediSearch/lettusearch/workflows/CI/badge.svg["Actions", link="https://github.com/RediSearch/lettusearch/actions?query=workflow%3ACI"] image:https://codecov.io/gh/RediSearch/lettusearch/branch/master/graph/badge.svg["Codecov", link="https://codecov.io/gh/RediSearch/lettusearch"] image:https://img.shields.io/lgtm/grade/java/g/RediSearch/lettusearch.svg?logo=lgtm&logoWidth=18["Language grade: Java", link="https://lgtm.com/projects/g/RediSearch/lettusearch/context:java"] image:https://snyk.io/test/github/RediSearch/lettusearch/badge.svg?targetFile=build.gradle["Known Vulnerabilities", link="https://snyk.io/test/github/RediSearch/lettusearch?targetFile=build.gradle"]

image:https://img.shields.io/badge/Forum-RediSearch-blue["Forum", link=https://forum.redislabs.com/c/modules/redisearch/] image:https://img.shields.io/discord/697882427875393627?style=flat-square["Discord", link="https://discord.gg/xTbqgTB"] endif::[]

Java client for https://redisearch.io[RediSearch] based on https://lettuce.io[Lettuce]

WARNING: LettuSearch has been merged into multi-module client https://github.com/redis-developer/lettucemod[LettuceMod]. Updates will now happen in LettuceMod.

Latest release: https://github.com/RediSearch/lettusearch/releases/latest

== Getting Started

Add LettuSearch to your application dependencies:

.Gradle [source,groovy]

dependencies { implementation 'com.redislabs:lettusearch:x.y.x' }

.Maven [source,xml]

com.redislabs lettusearch x.y.z ----

=== Basic Usage

[source,java]

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

[source,java]

RediSearchAsyncCommands<String, String> commands = connection.async(); // <1> commands.setAutoFlushCommands(false); // <2> List<RedisFuture>> futures = new ArrayList(); for (java.util.Map doc : docs) { RedisFuture future = commands.hmset(doc.get("id"), doc);// futures.add(future); // } commands.flushCommands(); // 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

[source,java]

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