我开始使用 Redis Om 和 spring 来存储 Json 实体。 在我的应用程序开始时,我需要按 Id 进行查询,但是当尚未保存任何文档时,此查询会失败。 我收到错误“由以下原因引起:redis.clients.jedis.exceptions.JedisDataException:com.gfa.redis.model.PositionIdx:没有这样的索引”
这是我的实体:
@AllArgsConstructor
@Data
@Document
public class Position {
@Id
private String tradingStrategyId;
@Indexed
@NonNull
private String triggerId;
@Indexed
private int tradeId;
@Indexed
@NonNull
private String symbol;
@NonNull
private IntervalEnum interval;
@NonNull
private Num pricePerAsset;
@Indexed
@NonNull
private TradeType tradeType;
@Indexed
@NonNull
private OrderType orderType;
@NonNull
private Long tradeTime;
@NonNull
private Long openKlineTimestamp;
这是回购代码:
@Repository
public interface LiveStrategyRepository extends RedisDocumentRepository<Position, String> {
Position findByTradingStrategyId(String tradingStrategyId);
Position findByTriggerId(String triggerId);
}
最后我使用@EnableRedisDocumentRepositories启用文档
代码的起始流程是调用 findByTradingStrategyId("123") 以获取 null 或存储的仓位。由于我还没有保存任何位置,所以我希望得到一个 Null。但是我收到错误。
我猜问题是没有创建位置实体索引,但我还没有找到在启动时创建它的方法。有什么想法吗?
看起来索引尚未创建,您可以启动 redis-cli 并执行以下操作吗:
ft._list
这应该显示系统中的索引列表。另外,您正在运行什么版本的 Redis。 Redis OM 与 Redis Stack 配合使用,Redis Stack 具有搜索引擎和 JSON 等额外数据类型。
如果您遇到困难,请停止使用 Redis Discord 服务器,我可以更深入地帮助您。