skip to Main Content

I have been reading about spring Data Redis which is quite similar to spring data JPA.
I have a USER class that has key has user id and value as the User object. I am trying to find all the user id’s (key) from the "/user" hash where this data is stored.

Spring data had this cool feature of using @Query which is apparently not working with spring data redis.

@Query("Select c.key from User c")
Set<String> getAllUserKey();

I have tried the above code but it is giving me error that unrecognized property Key. I have gone through the documentation and google and don’t think so this can be done using spring-data redis.

Just wanted to confirm my understanding and also would like to know what is the best way to approach this use case. I want to gather statistics around how many keys are stored under a hash and other things which would have been very easy to do with @Query

2

Answers


  1. I think you can use redisTemplate and redisTemplate.keys("*")

    Login or Signup to reply.
  2. No, in Spring Data Redis, the searching capability is limited to searching over secondary indices (created/maintained in Redis SETs). Spring Data Redis provides "Automatic implementation of Repository interfaces including support for custom query methods." So you can do simple repository-powered repo search methods like findByXXXandYYY.

    To get full-search capabilities, Redis introduces RediSearch (https://redis.io/docs/stack/search/quick_start/), a search engine packaged with Redis Stack (https://redis.io/docs/stack/) that enables searching over HASHes and JSON documents stored in Redis.

    To use Redis Stack features from Spring, there is Redis OM Spring (https://github.com/redis/redis-om-spring). A library that extends Spring Data Redis with Redis Stack functionality. It’s still early in its development but getting close to a 1.0 release (disclaimer: I’m the main contributor to the library).

    With OM you can create search indices for a collection (Hash/JSON) and user repositories methods to do more complex searching, or use the EntityStream class to search with a Java-streams-like API.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search