在redis-om redisjson中get()和find()的区别是什么?

huangapple go评论50阅读模式
英文:

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 first line returns an empty array. (Only if I restart the FastAPI, after inserting, it returns the values)
  • The second line returns the full JsonModel object (as intended).

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)
英文:

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 first line returns an empty array. (Only if I restart the FastAPI, after inserting, it returns the values)
  • The second line returns the full JsonModel object (as intended).

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)

答案1

得分: 1

find 使用 Redis Stack 的搜索功能,因此您的 Redis 服务器需要是一个 Redis Stack,或者安装了搜索模块。此外,为了使 find 起作用,您需要运行 Redis OM Python 的 Migrator,以便在 Redis 中创建一个搜索索引,搜索功能将使用该索引来跟踪符合条件的数据的更改。

get 函数不使用搜索功能,而是对于 JSON 模型使用 JSON.GET,对于 Hash 模型使用 HGET / HGETALL。为了使其工作,您需要知道要检索的项目的键。

另外,我怀疑您的 find 不会返回任何内容,因为 pk 不是一个索引值。要进一步提供建议,我需要看到您的模型类以及您调用 Migrator 的位置。

参考链接:

  • RediSearch 功能:https://redis.io/docs/stack/search/
  • Redis OM Python 查询和迁移工具:https://github.com/redis/redis-om-python#querying
英文:

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:

huangapple
  • 本文由 发表于 2023年6月8日 14:58:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76429326.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定