Spring Data Redis extensions for better search, documents models, and more
Spring Data Redis extensions for better search, documents models, and more
Object Mapping (and more) for Redis!
Redis OM Spring extends Spring Data Redis to take full advantage of Redis. Read the Official Docs here Redis OM Spring
Learn / Discuss / Collaborate
| Discord | Twitch | YouTube | |
|---|---|---|---|
| [![Discord][discord-shield]][discord-url] | [![Twitch][twitch-shield]][twitch-url] | [![YouTube][youtube-shield]][youtube-url] | [![Twitter][twitter-shield]][twitter-url] |
Table of contents
The Redis OM family of client libraries provide high-level abstractions, idiomatically implemented for your language and platform of choice. We currently cater to the Node, Python, .NET, and Spring communities.
Redis OM Spring provides powerful repository and custom object-mapping abstractions built on top of the Spring Data Redis (SDR) framework.
Redis OM Spring provides all Spring Data Redis capabilities, plus:
@Document annotation to map Spring Data models to Redis JSON documents@RedisHash via @EnableRedisEnhancedRepositories:@Id annotated fieldsRedisDocumentRepository with automatic implementation of Repository interfaces for complex querying capabilities
using @EnableRedisDocumentRepositories@Indexed@SearchableRedisIndexContextEntityStreams: Streams-based Query and Aggregations Builder@Bloom annotation to determine very fast, with and with high degree of certainty, whether a value is in a
collection.@Vectorize annotation to generate embeddings for text and images for use in Vector Similarity Searches[!IMPORTANT]
Version Compatibility
Choose the correct Redis OM Spring version for your Spring Boot version:
Redis OM Spring Spring Boot Java Status 1.0.x 3.4.x 17+ Maintenance 1.1.x 3.5.x 17+ Current Stable 2.0.x 4.0.x 17+ Latest Always use the Redis OM Spring version that matches your Spring Boot version.
Here is a quick teaser of an application using Redis OM Spring to map a Spring Data model using a RedisJSON document.
Redis OM Spring relies on the search, query, and JSON capabilities of Redis Stack. Before writing any code, you'll need a Redis Stack. The quickest way to get this is with Docker:
docker run -p 6379:6379 -p 8001:8001 redis/redis-stackThis launches redis-stack, an extension of Redis that adds several modern data
structures to Redis. You'll also notice that if you open up http://localhost:8001, you'll have access to the
redis-insight GUI, a GUI you can use to visualize and work with your data in Redis.
We have also provided a Docker Compose YAML file for you to quickly get started
using Redis Stack.
To launch the docker compose application, on the command line (or via Docker Desktop), clone this repository and run (from the root folder):
docker compose upBy default, Redis OM Spring connects to localhost at port 6379. If
your instance is running somewhere else, you can configure the connection
in your application.properties or application.yaml:
In application.properties:
spring.data.redis.host=your.cloud.db.redislabs.com
spring.data.redis.port=12345
spring.data.redis.password=xxxxxxxx
spring.data.redis.username=defaultIn application.yaml:
spring:
data:
redis:
host: your.cloud.db.redislabs.com
port: 12345
password: xxxxxxxx
username: defaultUse the @EnableRedisDocumentRepositories annotation to scan for @Document annotated Spring models,
Inject repositories beans implementing RedisDocumentRepository which you can use for CRUD operations and custom
queries (all by declaring Spring Data Query Interfaces):
…Like many other Spring Data projects, an annotation at the class level determines how instances
of the class are persisted. Redis OM Spring provides the @Document annotation to persist models as JSON documents
using RedisJSON:
…For multi-tenant applications, you can use dynamic index naming with SpEL expressions:
// Dynamic index naming for multi-tenant support
@Document
@IndexingOptions(indexName = "#{@environment.getProperty('app.tenant')}_companies_idx")
public class Company {
@Id private String id;
@Searchable private String name;
// other fields...
}Redis OM Spring, replaces the conventional UUID primary key strategy generation with a ULID (Universally Unique
Lexicographically Sortable Identifier) which is faster to generate and easier on the eyes.
Redis OM Spring data repository's goal, like other Spring Data repositories, is to significantly reduce the amount of
boilerplate code required to implement data access. Simply create a Java interface
that extends RedisDocumentRepository that takes the domain class to manage as well as the ID type of the domain class
as type arguments. RedisDocumentRepository extends the Spring Data class PagingAndSortingRepository.
Declare query methods on the interface. You can both, expose CRUD methods or create declarations for complex queries that Redis OM Spring will fulfill at runtime:
…The repository proxy has two ways to derive a store-specific query from the method name:
@Query or @Aggregation annotations.Redis OM Spring Entity Streams provides a Java 8 Streams interface to Query Redis JSON documents using RediSearch. Entity Streams allow you to process data in a type safe declarative way similar to SQL statements. Streams can be used to express a query as a chain of operations.
Entity Streams in Redis OM Spring provides the same semantics as Java 8 streams. Streams can be made of Redis Mapped
entities (@Document) or one or more properties of an Entity. Entity Streams progressively build the query until a
terminal operation is invoked (such as collect). Whenever a Terminal operation is applied to a Stream, the Stream
cannot accept additional operations to its pipeline and it also means that the Stream is started.
Let's start with a simple example, a Spring @Service which includes EntityStream to query for instances of the
mapped class Person:
package com.redis.om.skeleton.services;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.redis.om.skeleton.models.Person;
import com.redis.om.skeleton.models.Person$;
import com.redis.om.spring.search.stream.EntityStream;
@Service
public class PeopleService {
@Autowired
EntityStream entityStream;
// Find all people
public Iterable findAllPeople() {
return entityStream //
.of(Person.class) //
.collect(Collectors.toList());
}
}The EntityStream is injected into the PeopleService using @Autowired. We can then get a stream for Person
objects by using entityStream.of(Person.class). At this point the stream represents the equivalent of
a SELECT * FROM Person on a relational database. The call to collect will then execute the underlying query and
return a collection of all Person objects in Redis.
To produce more elaborate queries, you're provided with a generated metamodel, which is a class with the same name as
your model but ending with a dollar sign. In the
example below, our entity model is Person therefore we get a metamodel named Person$. With the metamodel you have
access to the operations related to the
underlying search engine field. For example, in the example we have an age property which is an integer. Therefore,
our metamodel has an AGE property which has
numeric operations we can use with the stream's filter method such as between.
// Find people by age range
public Iterable findByAgeBetween(int minAge, int maxAge) {
return entityStream //
.of(Person.class) //
.filter(Person$.AGE.between(minAge, maxAge)) //
.sorted(Person$.AGE, SortOrder.ASC) //
.collect(Collectors.toList());
}In this example we also make
No open issues yet, or sync has not completed.