You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Redis OM Spring嵌套@Indexed字段搜索异常:一级可查二级无结果

redis-om-spring嵌套@Indexed字段查询异常解决

问题现象

使用redis-om-spring查询嵌套@Indexed字段时,一级字段myKey.level1Key查询返回1条预期结果,但二级字段myKey.myId.level2Key查询返回0条结果,不确定是框架限制还是实现错误。

相关代码

启动类Application

@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()); // 返回预期的1条结果

      var byMyKeyMyIdLevel2Key = productRepository.findByMyKeyMyIdLevel2Key("A_level2Key1");
      System.out.println(byMyKeyMyIdLevel2Key.size()); // 意外返回0条结果
    };
  }

  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;
import lombok.*;

@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;
}

Redis环境与查询信息

  • Redis启动命令:
    docker run -d --name redis-stack -p 6379:6379 -p 8001:8001 redis/redis-stack:latest
    
  • 生成的查询语句:
    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}
    
  • 索引与数据情况:索引中存在myKey_level1Key字段,但未正确生成myKey_myId_level2Key的索引,Redis数据中实际存在二级嵌套的level2Key值。

问题根源与解决方法

根源

redis-om-spring在处理作为@Id的嵌套对象时,不会自动递归识别二级嵌套字段的@Indexed配置。之前的代码中,MyId类的level2Key未添加@Indexed注解,导致框架未生成对应的索引字段,因此查询无结果。

解决步骤

  1. 给MyId类的level2Key添加@Indexed注解:
import com.redis.om.spring.annotations.Indexed;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class MyId {
    @Indexed
    private String level2Key;
}
  1. 保留MyKey类中myId字段的@Indexed注解,确保框架能递归处理二级嵌套字段的索引。
  2. 重启应用,框架会自动创建myKey_myId_level2Key索引字段,此时二级字段查询即可返回预期的1条结果。

内容的提问来源于stack exchange,提问作者rjdkolb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 21:05:21