skip to Main Content

I started using Redis Om with spring to store a Json entity.
At the begining of my application I need to query by Id, but this query fails when there are no documents saved already.
I get the error "Caused by: redis.clients.jedis.exceptions.JedisDataException: com.gfa.redis.model.PositionIdx: no such index"

Here is my entity:

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

and this is the Repo code:

@Repository
public interface LiveStrategyRepository  extends RedisDocumentRepository<Position, String> {

    Position findByTradingStrategyId(String tradingStrategyId);

    Position findByTriggerId(String triggerId);

}

Finally I enable the document with @EnableRedisDocumentRepositories

The starting flow of the code is to call findByTradingStrategyId("123") to get either a null or the Position stored. Since I have not saved any position yet, I would expect to get a Null. However I get the error.

I guess the issue is that the Index for Position entity is not created, but I have not found a way to create it at startup. Any ideas?

2

Answers


  1. Chosen as BEST ANSWER

    I finally found a solution. I think the redis Om for Spring is a bit broken, but that later.

    First Finding: My entity definition was looking for index ".<entity_class_name>Idx". For some reason The whole startup and setup is not creating the index automatically (not sure if that is one of the problems).

    I tried to change the Index of the document by passing Arguments to the annotation like:

    @Document(value = "PositionIdx", prefixes = {"com", "gfa", "redis", "model", "Position"})
    

    But the index searched for was still the same.

    I had to mannually create the index myself with redis_cli and FT.CREATE

    Once that was created, the original exception of index not found was resolved.

    Then, the second problem: no documents were returned. I was searching for tradingStrategyId that was indexed using the @Indexed annotation in the Entity:

    @NonNull
    @Indexed
    private  String tradingStrategyId;
    

    For some reason, the actual query performed in Redis was:

    "FT.SEARCH" "com.gfa.redis.model.PositionIdx" "@tradingStrategyId:{emaCross1}" "LIMIT" "0" "10000"
    

    This returned a 0 match. Checking online for examples, I saw the query should be using parentheses, so running this in the cli returned matches:

     ft.search com.gfa.redis.model.PositionIdx '@tradingStrategyId:(emaCross1)' LIMIT "0" "1000"
    

    Notice "@tradingStrategyId:{emaCross1}" changed to '@tradingStrategyId:(emaCross1)' Apparently the JPA was treating my String as some other type, thus finding no match. When I changed the Annotation to @TextIndexed it worked.

    I guess the @Indexed annotation is broken for inferring the actual type.

    I am still trying to figure out how to auto create the index if not existant, any help would be appreciated!


  2. It looks like the index is not getting created, can you launch the redis-cli and do a:

    ft._list
    

    That should show the list of indices in the system. Also, what distribution of Redis are you running. Redis OM works with Redis Stack which has a search engine and extra datatypes like JSON.

    If you’re stuck stop by the Redis Discord server and I can help you there in more depth.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search