redisredis-py

Difference of get() and find() in redis-om redisjson


I was wondering why the following 2 examples do NOT return the same.

ts = Signal.find(Signal.pk == pk).all()
ts = Signal.get(pk)

The Signals was before successfully saved as JSON using the Python OM library for redis https://github.com/redis/redis-om-python. That is signal.save() in my case.

Is there anything one needs to consider when saving or querying that record by PK using find()?

FYI - the model

from enum import Enum
from typing import Optional
from uuid import UUID
from famodels.models.direction import Direction
from famodels.models.side import Side
from redis_om import Migrator
from redis_om import (Field, JsonModel)

class Signal(JsonModel):
    provider_id: str = Field(index=True)
    algo_id: str = Field(index=True)
    provider_signal_id: Optional[str]
    provider_trade_id: str = Field(index=True)
    is_hot_signal: bool = Field(default=False )
    market: str = Field(index=True)
    exchange: str = Field(index=True)
    direction: Direction = Field(index=True)
    side: Side = Field(index=True)
    price: float
    tp: float
    sl: float     

    class Meta:
        global_key_prefix="signal-processing"
        model_key_prefix="raw-signal"
    # class Config:
    #     orm_mode = True

Migrator().run()

FastAPI code running the Migrator().run() additonally.

@app.get("/signals")
async def get_signal():
    logger.debug("get all Signals.")    
    Migrator().run()
    return Signal.find().all()

It is interesting to observe, that if I add a new attribute to the Model, and create new records (which I can see under the same index in RedisInsight), they do not show up upon invoking Signal.find().all(). As if find().all() cannot consider new versions of the model.

Another preculiarity I have noted: Boolean need to be set to False imperatively. Missing out on it returns an empty array. That would imply one cannot search for true/false on that attribute and needs to transform it to 1/0 instead. Very unfortunate.

is_hot_signal: bool = Field(default=False, index=False)

Solution

  • find uses the Search capability of Redis Stack, so your Redis Server will need to be a Redis Stack one or have the Search module installed. Additionally, to make find work you will need to run Redis OM Python's Migrator in order to create a search index in Redis that the search capability will then use to track changes on qualifying data.

    The get function doesn't use Search, it uses JSON.GET for JSON models or HGET / HGETALL for Hash models. For this to work, you need the key of the item you want to retrieve.

    Additionally I suspect your find doesn't return anything as pk isn't an indexed value. To advise further I'd need to see your model class and where you are calling the Migrator.

    References: