0

I am new to Redis and developing Spring Boot + Spring Data Redis example. I am using CrudRepository, Example and ExampleMatchers API to do the searching from the Redis Key value store DB.

Now when I simply run my code, I saw that persons data saved as SET and HASH as well. Is this correct ? What's the use of saving the Person details both as SET and HASH

enter image description here

Showing all my code

public enum Gender {
    MALE, FEMALE {
        @Override
        public String toString() {
            return "Superwoman";
        }
    }
}

Species.java

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Species {

    @Indexed
    private String name;
}

Person.java

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@RedisHash("persons")
public class Person {

    @Id
    private String id;

    @Indexed
    private String firstname;
    private String lastname;
    @Indexed
    private Gender gender;

    private List<String> nicknames;
    @Indexed
    private Integer age;

    private Map<String, String> physicalAttributes;

    @Reference
    private Person relative;

    private Species species;
}

PersonRepository.java

public interface PersonRepository extends CrudRepository<Person, String>, QueryByExampleExecutor<Person> {

}

RedisExampleDemoApplication.java

@SpringBootApplication
public class RedisExampleDemoApplication implements CommandLineRunner{
    RedisMappingContext mappingContext = new RedisMappingContext();
    ExampleQueryMapper mapper = new ExampleQueryMapper(mappingContext, new PathIndexResolver(mappingContext));

    @Autowired
    private PersonRepository personRepository;

    public static void main(String[] args) {
        SpringApplication.run(RedisExampleDemoApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        Person person = Person.builder().firstname("Walter").gender(Gender.MALE).age(50).build();
        Person person1 = Person.builder().firstname("Savani").gender(Gender.FEMALE).age(35).build();

        personRepository.save(person);
        personRepository.save(person1);

        // [firstname:Walter, gender:MALE, age:50]
        RedisOperationChain operationChain = mapper.getMappedExample(Example.of(person, ExampleMatcher.matchingAny()));
        System.out.println(operationChain.getOrSismember());


        System.out.println("----------------------------------------------");
        Person p = Person.builder().lastname("Foo").build();
        RedisOperationChain roc = mapper.getMappedExample(Example.of(p));
        System.out.println(" == "+roc.getOrSismember());
        System.out.println("-- "+roc.getSismember());
    }
}
Jeff Cook
  • 7,956
  • 36
  • 115
  • 186

1 Answers1

0

May be it is late to answer now , the reason that SET is visible is because of the secondary Index. I.e in your example First name is annotated as Indexed. Redis consider this as secondary index which is default a SET.

BharathyKannan
  • 148
  • 2
  • 3
  • 19