Redis OM Spring Documentation

repository·main·Indexed 20 days ago

https://github.com/redis/redis-om-spring

An object-mapping library extending Spring Data Redis to provide high-level abstractions for Redis JSON, RediSearch, and Vector Similarity Search. It includes support for JSON document mapping via @Document, secondary indexing for Redis Hashes via @RediHash and @Indexed, and hybrid search combining BM25 full-text search with vector similarity using the EntityStream API.

Tokens
101.4K
Snippets
288
Records
362
Agent score
68%

What's inside Redis OM Spring

  1. What is Redis OM Spring?

    main

    Redis OM Spring is an extension of the Spring Data Redis framework. It provides high-level object-mapping and repository abstractions designed to leverage advanced Redis features.

    Key capabilities include:

    • Redis JSON Mapping: Use the @Document annotation to map Spring Data models directly to Redis JSON documents.
    • Enhanced @RedisHash: Via @EnableRedisEnhancedRepositories, it adds support for RediSearch secondary indexing and uses ULID for @Id fields.
    • Advanced Indexing: Declarative secondary indexing with @Indexed and full-text search indexing with @Searchable.
    • Search & Querying: Provides RedisDocumentRepository for complex queries and EntityStreams for stream-based query and aggregation building.
    • Specialized Data Types: Support for Bloom filters via @Bloom and vector embeddings via @Vectorize for Vector Similarity Search.
    • Multi-tenancy: Dynamic index naming using SpEL expressions and RedisIndexContext.
    • Index Management: An index migration service supporting Blue-Green, Dual-Write, and In-Place migration strategies.
  2. Overview of Redis OM Spring capabilities

    main

    Redis OM Spring is an Object-Mapping (OM) framework for Spring applications that allows developers to map Java objects to Redis data structures. It provides a declarative approach to data modeling, indexing, and querying.

    Key Capabilities:

    • Data Modeling: Supports both Redis Hash and Redis JSON mappings.
    • Indexing & Search: Integrates with the Redis Query Engine for full-text search and indexing via annotations.
    • Querying: Offers multiple ways to query data, including Repository method names, @Query annotations, Query By Example (QBE), and a fluent EntityStream API.
    • Advanced Search: Supports Vector Similarity Search (AI integration), autocomplete, and probabilistic data structures like Bloom and Cuckoo filters.
    • Type-Safety: Provides generated metamodel classes for type-safe queries.
    • Enterprise/High Availability: Supports Redis Sentinel.
  3. Overview of Redis OM Spring Core Module

    main

    The Core module (redis-om-spring) provides the fundamental functionality for object mapping, repositories, and search capabilities in Redis. It is organized into several key functional packages:

    • Annotations (com.redis.om.spring.annotations): Core annotations for mapping and indexing.
    • Repository (com.redis.om.spring.repository): Repository interfaces and implementations.
    • Search (com.redis.om.spring.search): Search and query functionality.
    • Metamodel (com.redis.om.spring.metamodel): Type-safe query metamodel.
    • Indexing (com.redis.om.spring.indexing): Index creation and management.
    • Operations (com.redis.om.spring.ops): Redis operations and client abstractions.
    • Tuple (com.redis.om.spring.tuple): Tuple system for structured data handling.
  4. Overview of Redis OM Spring AI Module

    main

    The AI extension module (redis-om-spring-ai) provides vector embedding and similarity search capabilities with multiple AI provider integrations. Key packages include:

    • Annotations (com.redis.om.spring.annotations): AI and vectorization annotations.
    • Vectorize (com.redis.om.spring.vectorize): Embedding generation and processing.
    • Configuration (com.redis.om.spring): AI configuration and auto-configuration.
  5. What is Redis OM Spring?

    main

    Redis OM Spring is an Object Mapping framework for Spring applications that leverages advanced Redis features. It extends Spring Data Redis to provide a more intuitive developer experience for working with Redis data using annotations, repository patterns, and fluent APIs.

    It consists of two main modules:

    • redis-om-spring: The core module providing modeling, indexing, search, and repository capabilities.
    • redis-om-spring-ai: An AI-focused module that leverages Spring AI for features like vector embedding generation.
  6. Overview of Redis OM Spring

    main

    Redis OM Spring is an object-mapping and querying library for Spring applications that use Redis. It extends Spring Data Redis and Spring AI to provide advanced capabilities directly within the Spring ecosystem.

    Key capabilities include:

    • Document & Hash Mapping: Map Java objects to Redis JSON documents or Redis Hashes.
    • Repository Pattern: Type-safe data access using Spring Data repositories.
    • Advanced Querying: Support for searching, filtering, and sorting via the Redis Query Engine.
    • Entity Streams: A fluent API for querying and aggregating data.
    • Vector Search: Semantic search and AI-powered applications using vector similarity search.
    • AI Integration: Annotation-based vectorization support leveraging Spring AI and DJL.
    • Probabilistic Data Structures: Native support for Bloom filters, Cuckoo filters, Count-Min Sketch, and more.
    • Autocomplete: Built-in capabilities for application autocomplete features.
  7. Use @RediHash and @Indexed for secondary indexing

    main

    Redis OM Spring allows you to create secondary indexes on models mapped to Redis Hashes using the @RediHash annotation. To enable indexing on specific fields so they can be queried via RediSearch, apply the @Indexed annotation (com.redis.om.spring.annotations.Indexed) to those fields within your model class.

    @RediHash
    public class MyModel {
        @Id
        private String id;
    
        @Indexed
        private String searchableField;
    }
  8. Enable lexicographic string range queries

    main

    By setting lexicographic = true on an @Indexed or @Searchable field, you enable string range queries (e.g., findBySkuGreaterThan, between) by creating an additional Redis sorted set index (e.g., Product:sku:lex).

    This is useful for:

    • ID ranges
    • SKU comparisons
    • Alphabetical ordering
    • Version strings

    Note: This requires additional storage for the sorted set. Only enable it for fields where range queries are necessary.

    @Document
    public class Product {
        @Id
        private String id;
    
        @Indexed(lexicographic = true)
        private String sku;  // Enables findBySkuGreaterThan("ABC123")
    
        @Indexed(lexicographic = true)
        private String productCode;  // Enables range queries on product codes
    }
  9. Enable lexicographic indexing for string range queries

    main

    By setting lexicographic = true on @Indexed or @Searchable annotations, Redis OM Spring creates an additional sorted set index. This allows for efficient alphabetical or sequential string range queries (e.g., >, <, between).

    Use Cases

    • ID ranges: Finding entities within specific ID ranges
    • SKU/Product codes: Filtering products by code ranges
    • Version strings: Comparing semantic versions
    • Alphabetical filtering: Finding names in alphabetical ranges
    @Document
    public class Product {
      @Id
      private String id;
    
      @Indexed(lexicographic = true)
      private String sku;
    
      @Searchable(lexicographic = true)
      private String productName;
    
      @Indexed(lexicographic = true)
      private String version;
    }
  10. Handle Jedis 6.0.0 Query Escaping for RediSearch

    main

    Jedis 6.0.0 changed how it handles query string escaping for RediSearch. Multi-word search terms must now use double quotes (") instead of single quotes (').

    Affected Users:

    • Users implementing custom repository methods that construct Query objects with Jedis directly.
    • Users directly using SearchOperations with string-based queries containing spaces.

    NOT Affected:

    • Users using built-in repository query methods.
    • Users using EntityStream API.
    • Users using @Query annotations (handled internally).
    #### Before (Jedis 5.2.0)
    ```java
    SearchOperations<String> ops = modulesOperations.opsForSearch("myIndex");
    SearchResult result = ops.search(new Query("@title:'hello world'"));

    After (Jedis 6.0.0)

    SearchOperations<String> ops = modulesOperations.opsForSearch("myIndex");
    SearchResult result = ops.search(new Query("@title:\"hello world\""));
  11. Understand Vector Similarity Search and Semantic Search features

    main

    The demo utilizes the Redis Query Engine to provide:

    Enables semantic search and similarity matching using vector embeddings. Use cases include:

    • Semantic search across movie descriptions.
    • Finding similar movies based on content.
    • Content-based recommendations.
    • Multi-modal search (e.g., text, images).

    Implements a system that can:

    • Find movies based on semantic meaning rather than exact keyword matches.
    • Provide relevant results even when exact terms do not match.
    • Support hybrid search by combining text and vector queries.
  12. Understand Keyspaces in Redis OM Spring

    main

    Keyspaces provide a way to logically organize and namespace Redis keys. This enables multi-tenancy, environment separation (dev/staging/prod), and prevents key collisions when multiple applications share a single Redis instance.

    Redis OM Spring uses the Spring Data @KeySpace annotation under the hood. It automatically manages keyspaces for both @Document and @RedisHash entities, ensuring that entity data, search indexes, and metadata all use consistent prefixes.