1

We have model class saved in Redis as mentioned below:-

@Data
@NoArgsConstructor
@AllArgsConstructor
@RedisHash("book")
public class Book implements Serializable {

 private static final long serialVersionUID = 2208852329346517265L;

 @Id
 private Integer bookID;
 @Indexed
 private String title;
 @Indexed
 private String authors;
 private String averageRating;
 private String isbn;
 private String languageCode;
 private String ratingsCount;
 private BigDecimal price;
}

We have title and authors as our indexed property.

Now we wanted to search all the records from Redis by passing title and a list of authors using the spring crud repository as mentioned below.

public interface BookSpringRepository extends  CrudRepository<Book, String> {

  List<Book> findAllByTitleAndAuthors(String title, List<String> authors);
}

Service layer:-

@Override
public Optional<List<Book>> searchBooksByTitleAndAuthorNames(String title, List<String> 
autherNames) {
    return Optional.ofNullable(bookSpringRepository.findAllByTitleAndAuthors(title, 
    autherNames));
}

Here we are getting below exception

Unable to fetch data from Spring data Redis cache using List of Integer or 
String.
 Getting error while fetching - "Resolved 
[org.springframework.core.convert.ConversionFailedException: Failed to convert from type 
[java.lang.String] to type [byte] for value 'Ronak';
nested exception is java.lang.NumberFormatException: For input string: "Ronak"]."
  1. We would not want to convert the list of string/integer to byte as it is a time-consuming process and as we tried took so much amount of time. Also when the results are retrieved we will again have to convert back to normal integer or string values.

  2. The other option is to loop through the list and pass a single value at a time to the Redis crud repository and this time Redis crud repository is happy but that will be a loop call to Redis and network latency.

  3. We cannot add ID attributes on authors' property as these can be duplicate records.

  4. Does the spring crud repository support the LIKE query in search that way we can create a unique id having these authors' names and make put ID annotation on that new derived property to search the records using spring crud repository using LIKE or contains kind of query.

Any suggestions here are highly appreciated!!

2
  • How did you configure Redis? Commented Mar 8, 2022 at 13:11
  • Thanks, @pringi for your prompt response. In the AWS environment Redis is configured by the different teams so we don't have much insight on this but yes we setup normal Redis client and server in our local and run the same program and it was giving us the same exception Commented Mar 8, 2022 at 13:50

1 Answer 1

0

Try to add serialization to your redis key and value. This might help : https://medium.com/@betul5634/redis-serialization-with-spring-redis-data-lettuce-codec-1a1d2bc73d26

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.