spring-projects/spring-data-redis

Index value for `@Index` column not removed after setting value to `null`

soheilrahsaz opened this issue · 0 comments

Using Spring data 3.2.4 and repositories and following this link https://docs.spring.io/spring-data/redis/reference/redis/redis-repositories/indexes.html

@RedisHash("Fruit")
public class Fruit {
    @Id
    private Integer id;
    private String name;
    @Indexed
    private String color;
}

public interface FruitRedisRepository extends CrudRepository<Fruit, Integer> {
    List<Fruit> findByColor(String color);
}

@Configuration
public class RedisConfig {
    @Bean
    public RedisConnectionFactory redisConnectionFactory()
    {
        return new LettuceConnectionFactory("localhost", 16379);
    }
}

This test fails:

@SpringBootTest
class TestSpringRedisApplicationTests {

    @Autowired
    FruitRedisRepository fruitRedisRepository;

    @Test
    void test() {
        //sample fruit with `yellow` as index value
        Fruit banana = Fruit.builder()
                .id(1)
                .name("banana")
                .color("yellow")
                .build();
        fruitRedisRepository.save(banana);

        //retrieving the banana and setting null as color, expecting that the index would be removed
        Fruit foundBanana = fruitRedisRepository.findById(1).orElseThrow();
        foundBanana.setColor(null);
        fruitRedisRepository.save(foundBanana);

        //finding by color yellow, expect empty list, but it actually returns the banana.
        assertThat(fruitRedisRepository.findByColor("yellow")).isEmpty();
    }
}

Expected result is that the index for Fruit:color:yellow is removed, but it still exists in Redis with 1 value with id 1:

Redis output:

smembers Fruit:color:yellow

  1. "1"

Note: If I set another value for color, say purpule it works fine and Fruit:color:yellow is removed and Fruit:color:purpule is created