I am trying to search for a nested @Indexed field using redis-om-spring
For some reason a find returns expected 1 entry for level 1, but returns unexpected 0 entries for level 2.
I am not sure if I can only search one level deep or if I am making a mistake.
@SpringBootApplication
@Configuration
@EnableRedisDocumentRepositories
@Slf4j
public class Application {
@Autowired
ProductRepository productRepository;
@Bean
CommandLineRunner loadTestData() {
return args -> {
productRepository.deleteAll();
productRepository.save(new Product(new MyKey(new MyId("A_level2Key1"),"A_level1Key1"),"FirstColour"));
productRepository.save(new Product(new MyKey(new MyId("B_level2Key1"),"B_level1Key1"),"SecondColour"));
var byMyKeyLevel1Key = productRepository.findByMyKeyLevel1Key("A_level1Key1");
System.out.println(byMyKeyLevel1Key.size());//returns expected 1 entry for level 1
var byMyKeyMyIdLevel2Key = productRepository.findByMyKeyMyIdLevel2Key("A_level2Key1");
System.out.println(byMyKeyMyIdLevel2Key.size());//returns unexpected 0 entries for level 2
};
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Product
import com.redis.om.spring.annotations.Document;
import com.redis.om.spring.annotations.Indexed;
import com.redis.om.spring.annotations.Searchable;
@Slf4j
@Data
@AllArgsConstructor
@NoArgsConstructor
@Document
public class Product {
@Indexed
@Id
private MyKey myKey;
@Searchable
@NonNull
private String colourDesc;
}
ProductRepository
public interface ProductRepository extends RedisDocumentRepository<Product, String> {
List<Product> findByMyKeyLevel1Key(String level1Key);
List<Product> findByMyKeyMyIdLevel2Key(String level2Key);
}
MyKey
import com.redis.om.spring.annotations.Indexed;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class MyKey {
@Indexed
MyId myId;
@Indexed
String level1Key;
}
Data in Redis:
FT.INFO "com.redis.om.documents.domain.ProductIdx"
From the logs, it generates searches:
FT.SEARCH "com.redis.om.documents.domain.ProductIdx" @myKey_level1Key:{A_level1Key1}
FT.SEARCH "com.redis.om.documents.domain.ProductIdx" @myKey_myId_level2Key:{A_level2Key1}
I start Redis with:
docker run -d --name redis-stack -p 6379:6379 -p 8001:8001 redis/redis-stack:latest
The issue is likely in the naming of the repository methods. I wrote your example as a test in the project repo, this will be using release 0.7.0 - but it should work with 0.6.x-SNAPSHOTs:
The entity:
The first-level key:
The second-level key:
The repo, here's where things probably went wrong for you...
and here's a snippet of the test class I used:
Hope that helps! Thanks for trying Redis OM Spring!