spring-data-redis icon indicating copy to clipboard operation
spring-data-redis copied to clipboard

Executing full-text queries by using Redis template

Open dasivon021 opened this issue 9 months ago • 4 comments

Hi,

Context:

I have a Spring Boot 3.2.3 app which uses spring-boot-starter-data-redis. We have a Redis cluster on the cloud, where RediSearch module is installed and where other applications are doing inserts of hash records. My application will only be executing search queries and have read-only permission towards cluster. So index already exist and I need to find a way to use it within my full-text queries but relaying on spring-boot-starter-data-redis since redis-om-spring has currently few issues which are blocking usage of pre-created index (#437, #438 & #439).

So pre-created index is created like this:

"FT.CREATE" "company:idx" "ON" "HASH" "PREFIX" "1" "session:" "SCHEMA" "User-Name" "TAG" "SEPARATOR" "|" "Event-Timestamp" "NUMERIC" "SORTABLE"

  1. Using Jedis (by excluding it from starter and adding jedis to pom):

Defining a template:

  @Bean
    public RedisTemplate<Long, Student> redisTemplate(RedisConnectionFactory connectionFactory) {
        var template = new RedisTemplate<Long, Student>();
        template.setConnectionFactory(connectionFactory);
        template.setEnableTransactionSupport(true);
        return template;
    }

Using it:

@GetMapping("/get")
public String getEntity(){
  Object o = executeCommand("FT.SEARCH", "comapany:idx".getBytes(), "@User\\-Name{pera}".getBytes(), "SORTBY".getBytes(), "Event-Timestamp".getBytes(), "DESC".getBytes(), "LIMIT".getBytes(), "0".getBytes(), "1".getBytes());
               
  System.out.println(o);
  return "a";
}

public Object executeCommand(String command, byte[]... parts) {
  return redisTemplate.execute(connection ->
	connection.execute(command, parts), false
  );
}

If I were to execute above query in redis-cli, the output would be:

1) (integer) 1
2) "student:abc"
3)  1) "User-Name""
    2) "pera"
     ..key-value pairs

But in code I get a List with bytes, which I don't like and there could be potential performance issues when doing (de)serialization. The problem with connection.execute is that returns byte[] Screenshot_12

Question 1: Is there a more optimal way to do issue these types of queries?

Question 2: Is there a way to configure Redis template so when it issues these types of queries to return a list of entities instead of raw bytes?

  1. Using Lettuce (starter default):
java.lang.UnsupportedOperationException: io.lettuce.core.output.ByteArrayOutput does not support set(long)
	at io.lettuce.core.output.CommandOutput.set(CommandOutput.java:107) ~[lettuce-core-6.3.1.RELEASE.jar:6.3.1.RELEASE/12e6995]

dasivon021 avatar May 16 '24 13:05 dasivon021