4

From a Spring boot application, I want to use Redis and retrieve paged and sorted results.

Maven config:

<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-redis</artifactId>
  <version>2.1.4.RELEASE</version>
</dependency>

<dependency>
  <groupId>redis.clients</groupId>
  <artifactId>jedis</artifactId>
  <version>2.10.2</version>
  <type>jar</type>
</dependency>

Based on an online example, I implemented the following repository:

@Repository
public interface StudentRepository extends PagingAndSortingRepository<Student, String> {

  List<Student> findByName(String Name, Pageable page);
  List<Student> findByName(String Name);

}

Now I want to filter my Student using

studentRepository.findAll(Sort.by("range").ascending());

With Student:

@RedisHash("Student")
public class Student implements Serializable {
  @Id
  private String id;
  @Indexed
  private String name;
  private Gender gender;
  private int grade;

Whatever the sort is, the order is always the same. After a debug session, I understood that the reason is:

class RedisQueryEngine extends QueryEngine<RedisKeyValueAdapter, RedisOperationChain, Comparator<?>> {

/**
 * Creates new {@link RedisQueryEngine} with defaults.
 */
RedisQueryEngine() {
    this(new RedisCriteriaAccessor(), null);
}

/**
 * Creates new {@link RedisQueryEngine}.
 *
 * @param criteriaAccessor
 * @param sortAccessor
 * @see QueryEngine#QueryEngine(CriteriaAccessor, SortAccessor)
 */
private RedisQueryEngine(CriteriaAccessor<RedisOperationChain> criteriaAccessor,
        @Nullable SortAccessor<Comparator<?>> sortAccessor) {
    super(criteriaAccessor, sortAccessor);
}

The public constructor that is called by Spring has no sortAccessor defined. This sort accessor is then used in the QueryEngine class by my query:

public <T> Collection<T> execute(KeyValueQuery<?> query, String keyspace, Class<T> type) {

    CRITERIA criteria = this.criteriaAccessor.map(it -> it.resolve(query)).orElse(null);
    SORT sort = this.sortAccessor.map(it -> it.resolve(query)).orElse(null);

    return execute(criteria, sort, query.getOffset(), query.getRows(), keyspace, type);
}

Is this behavior expected? Can Redis only page results (this works, I tested it) but not sort them (in this case, paging is totally useless)?

Rolintocour
  • 2,934
  • 4
  • 32
  • 63

0 Answers0