Search code examples
javaredisspring-dataspring-data-redis

Looking for examples on how to use spring-data RedisZSetCommands.zRangeByLex


Specially, I need to know:

  1. How Range's gt, gte, lt, and lte methods work.

  2. Is there a way to peek just one item? For ex. from command line it is possible to do:

zadd test 0 aaaa
(integer) 1

zadd test 0 bbbb
(integer) 1

zadd test 0 aa
(integer) 1

zadd test 0 aaaaaa
(integer) 1

zrangebylex test [aaaa [aaaa
1) "aaaa"

Can the same be done using RedisZSetCommands.zRangeByLex ?


Solution

  • Support for ZRANGEBYLEX directly via ZSetOperations and RedisZSet is still an open issue. Though it is possible using RedisCallback.

    template.execute(new RedisCallback<Set<byte[]>>() {
    
      @Override
      public Set<byte[]> doInRedis(RedisConnection connection) throws DataAccessException {
        return connection.zRangeByLex(key, Range.range().gte("aaaa").lte("aaaa"));
      }
    });