You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix documentation that incorrectly described Redis OM Spring hash mapping as using
Spring Data Redis' basic Set-based indexing. Redis OM Spring actually creates
RediSearch indexes using FT.CREATE commands for Hash entities.
Key corrections:
- Clarify that Redis OM Spring uses RediSearch indexes, not Redis Sets
- Add comparison table showing differences from Spring Data Redis
- Show actual Redis commands (FT.CREATE ON HASH) generated
- Explain O(log N) index-based query performance vs O(N) scans
- Add migration guide from Spring Data Redis
- Update examples to show RediSearch field types (TEXT, TAG, NUMERIC, GEO, VECTOR)
Spring Data Redis (SDR), the library that Redis OM Spring extends, provides mapping of Spring Entities to Redis Hashes. Redis OM Spring enhances this capability by integrating with the Query Engine available in Redis 8.0.0+ (or via Redis Stack for older versions).
9
+
Redis OM Spring fundamentally transforms how Redis Hashes work by adding RediSearch indexing capabilities on top of the standard hash storage. This is a key distinction from Spring Data Redis, which only provides basic hash operations.
10
10
11
-
This integration brings powerful search and indexing capabilities to Redis Hashes, while maintaining compatibility with existing Spring Data Redis code.
11
+
=== The Core Difference
12
12
13
-
== Key Benefits of Redis OM Spring Hash Mapping
13
+
**Spring Data Redis**: Stores entities as Redis Hashes with basic secondary indexing using Redis Sets
14
+
**Redis OM Spring**: Stores entities as Redis Hashes PLUS creates RediSearch indexes for powerful querying
14
15
15
-
* **Enhanced Query Capabilities**: Full text search, numeric ranges, geo-spatial queries
16
-
* **Backward Compatibility**: Works with existing Spring Data Redis code
17
-
* **Simple Annotations**: Easy to add indexing without changing your data model
18
-
* **Performance**: Efficient storage format for simple objects
16
+
[source,bash]
17
+
----
18
+
# Spring Data Redis approach (basic)
19
+
HSET person:123 name "John" email "john@example.com" # Store hash
20
+
SADD person:name:John person:123 # Basic index
21
+
22
+
# Redis OM Spring approach (enhanced)
23
+
HSET person:123 name "John" email "john@example.com" # Store hash (same)
24
+
FT.CREATE PersonIdx ON HASH ... # RediSearch index (NEW!)
25
+
----
26
+
27
+
Redis OM Spring creates full RediSearch indexes using `FT.CREATE` commands, providing:
28
+
29
+
== Key Differences from Spring Data Redis
30
+
31
+
[cols="1,2,2"]
32
+
|===
33
+
|Feature |Spring Data Redis |Redis OM Spring
34
+
35
+
|Indexing Mechanism
36
+
|Redis Sets for @Indexed fields
37
+
|RediSearch indexes via FT.CREATE
38
+
39
+
|Query Performance
40
+
|O(N) scans for complex queries
41
+
|O(log N) index-based queries
42
+
43
+
|Search Capabilities
44
+
|Basic equality checks only
45
+
|Full-text search, ranges, aggregations
46
+
47
+
|Field Types
48
+
|Simple hash fields
49
+
|TEXT, TAG, NUMERIC, GEO, VECTOR fields
50
+
51
+
|Repository Type
52
+
|CrudRepository/RedisRepository
53
+
|RedisEnhancedRepository
54
+
55
+
|Query Methods
56
+
|Limited to findBy patterns
57
+
|Complex queries, @Query, Entity Streams
58
+
|===
19
59
20
60
== Basic Usage
21
61
@@ -167,9 +207,9 @@ found.ifPresent(person -> {
167
207
repository.deleteById(newPerson.getId());
168
208
----
169
209
170
-
== How Redis OM Spring Stores Redis Hashes
210
+
== How Data is Stored and Indexed
171
211
172
-
Redis Hashes are flat key-value structures. When storing Java objects, Redis OM Spring follows these mapping rules:
212
+
Redis OM Spring stores data in standard Redis Hashes (just like Spring Data Redis) but additionally creates RediSearch indexes for querying:
173
213
174
214
=== Object-to-Hash Mapping
175
215
@@ -207,25 +247,39 @@ Redis Hashes are flat key-value structures. When storing Java objects, Redis OM
207
247
|`addresses.[0].city = "Tear"`
208
248
|===
209
249
210
-
Additionally, Redis OM Spring adds a `_class` attribute to store type information:
250
+
The data is stored in a standard Redis Hash:
211
251
212
252
[source,text]
213
253
----
214
-
_class = com.example.Person
215
-
id = 01HXYZ123ABC
216
-
name = Mat Cauthon
217
-
email = mat@bandoftheredhand.com
218
-
roles.[0] = general
219
-
roles.[1] = gambler
220
-
address.city = Tear
221
-
address.street = High Street
222
-
----
254
+
# Standard Redis Hash (same as Spring Data Redis)
255
+
HSET people:01HXYZ123ABC
256
+
_class "com.example.Person"
257
+
id "01HXYZ123ABC"
258
+
name "Mat Cauthon"
259
+
email "mat@bandoftheredhand.com"
260
+
roles.[0] "general"
261
+
roles.[1] "gambler"
262
+
address.city "Tear"
263
+
address.street "High Street"
264
+
265
+
# But Redis OM Spring ALSO creates a RediSearch index
266
+
FT.CREATE PersonIdx ON HASH PREFIX 1 people: SCHEMA
267
+
name TAG SORTABLE
268
+
email TEXT
269
+
roles TAG SEPARATOR |
270
+
address.city TAG
271
+
----
272
+
273
+
This dual approach means:
274
+
- Your data remains compatible with Spring Data Redis
275
+
- You get powerful search capabilities through RediSearch
276
+
- Queries use the index for performance, not scanning
223
277
224
278
== Indexing and Searching
225
279
226
-
=== Simple Property Indexes
280
+
=== How Redis OM Spring Creates Indexes
227
281
228
-
Use the `@Indexed` annotation to create secondary indexes for fields:
282
+
When you annotate fields with `@Indexed`, Redis OM Spring creates a RediSearch index:
229
283
230
284
[source,java]
231
285
----
@@ -234,25 +288,32 @@ public class Person {
234
288
@Id
235
289
private String id;
236
290
237
-
@Indexed
291
+
@Indexed // Creates a TAG field in RediSearch
238
292
private String name;
239
293
240
-
@Indexed
294
+
@Searchable // Creates a TEXT field for full-text search
241
295
private String email;
296
+
297
+
@Indexed // Creates a TAG field for set values
298
+
private Set<String> roles;
242
299
}
243
300
----
244
301
245
-
This creates Redis Sets for each value:
302
+
This generates a RediSearch index creation command:
Redis OM Spring provides a fluent API for complex queries:
413
+
414
+
[source,java]
415
+
----
416
+
@Autowired
417
+
EntityStream entityStream;
418
+
419
+
// Complex query using Entity Streams
420
+
List<Person> admins = entityStream
421
+
.of(Person.class)
422
+
.filter(Person$.ROLES.contains("admin"))
423
+
.filter(Person$.NAME.startsWith("J"))
424
+
.sorted(Person$.EMAIL, SortOrder.ASC)
425
+
.collect(Collectors.toList());
426
+
----
427
+
309
428
== Time To Live (TTL)
310
429
311
430
You can set expiration times for entities:
@@ -393,11 +512,40 @@ public class RedisConfig {
393
512
}
394
513
----
395
514
396
-
== Redis Cluster Considerations
515
+
== Migration from Spring Data Redis
516
+
517
+
Migrating from Spring Data Redis to Redis OM Spring is straightforward:
397
518
398
-
When using Redis Cluster, it's important to ensure that related data is stored in the same hash slot to enable atomic operations and efficient queries.
519
+
1. **Change the repository interface**:
520
+
```java
521
+
// Before: Spring Data Redis
522
+
public interface PersonRepository extends CrudRepository<Person, String> { }
523
+
524
+
// After: Redis OM Spring
525
+
public interface PersonRepository extends RedisEnhancedRepository<Person, String> { }
526
+
```
399
527
400
-
Use the `@IdAsHashTag` annotation to ensure that keys for an entity and its indexes are stored in the same hash slot:
528
+
2. **Enable enhanced repositories**:
529
+
```java
530
+
// Before
531
+
@EnableRedisRepositories
532
+
533
+
// After
534
+
@EnableRedisEnhancedRepositories
535
+
```
536
+
537
+
3. **Add indexing annotations** (optional but recommended):
538
+
```java
539
+
@Indexed // For exact matches
540
+
@Searchable // For full-text search
541
+
@NumericIndexed // For numeric ranges
542
+
```
543
+
544
+
Your existing data remains compatible, and you immediately gain access to powerful search capabilities.
545
+
546
+
== Redis Cluster Considerations
547
+
548
+
When using Redis Cluster with RediSearch indexes, use the `@IdAsHashTag` annotation to ensure proper data locality:
401
549
402
550
[source,java]
403
551
----
@@ -415,14 +563,28 @@ public class HashWithHashTagId {
415
563
416
564
== Performance Considerations
417
565
418
-
* Redis Hashes are very efficient for simple data structures
0 commit comments