redis / redis-om-python

Object mapping, and more, for Redis and Python
MIT License
1.09k stars 107 forks source link

Unable to use redisearch on cluster #412

Open XChikuX opened 1 year ago

XChikuX commented 1 year ago

Hi Everyone,

I've been trying out redis-om and I like the simplicity it offers. Thank you for taking the time to write it.

I have run into an issue though. When trying to run the following line of code

Customer.find(Customer.age == 38).all()

The above line of code works fine for a single redis instance. However, in cluster mode which redisearch should be using RSCoordinator under the hood I get redis moved errors.

redis.exceptions.ResponseError: MOVED 14592 10.9.9.6:7003

I have made sure to run: Migrator.run()

And I don't see what other issue I could have run into other than RSCoordinator not responding as its supposed in the redis-om backend. I come to this conclusion since save() & get() work just fine.

XChikuX commented 1 year ago

Just checking in since its been 3 months. Any movement on this?

jkuehnemundt commented 1 year ago

I unfortunately also have this problem when using a cluster. Is there already a solution for this?

jrwagz commented 1 year ago

I too am trying to get RediSearch with Python OM working on a Redis Cluster. However I ran into this problem a different way.

I was setting the database connection in my model via it's Meta class as follows:

from redis import RedisCluster

class MyModel(JsonModel):
    field: str = Field(index=True)

    class Meta:
        database = RedisCluster(
            host=os.environ["REDIS_DB_HOST"],
            port=os.environ["REDIS_DB_PORT"],
            username=os.environ["REDIS_DB_USER"],
            password=os.environ["REDIS_DB_PASS"],
        )

And when I try to just run Migrator().run I get the following error:

Traceback (most recent call last):
  File ".../debug_model_search.py", line 9, in <module>
    Migrator().run()
  File ".../site-packages/redis_om/model/migrations/migrator.py", line 165, in run
    migration.run()
  File ".../site-packages/redis_om/model/migrations/migrator.py", line 76, in run
    self.create()
  File ".../site-packages/redis_om/model/migrations/migrator.py", line 82, in create
    create_index(self.conn, self.index_name, self.schema, self.hash)
  File ".../site-packages/redis_om/model/migrations/migrator.py", line 43, in create_index
    db_number = conn.connection_pool.connection_kwargs.get("db")
AttributeError: 'RedisCluster' object has no attribute 'connection_pool'

However, if I instead replace the database setup as follows, then the Migrator seems to work just fine:

from redis import Redis

class MyModel(JsonModel):
    field: str = Field(index=True)

    class Meta:
        database = Redis(
            host=os.environ["REDIS_DB_HOST"],
            port=os.environ["REDIS_DB_PORT"],
            username=os.environ["REDIS_DB_USER"],
            password=os.environ["REDIS_DB_PASS"],
        )

Could it be as simple as the fact that the Python OM implementation doesn't support the RedisCluster connection (https://github.com/redis/redis-py/blob/9f503578d1ffed20d63e8023bcd8a7dccd15ecc5/redis/cluster.py#L421), but only supports the Redis connection (https://github.com/redis/redis-py/blob/9f503578d1ffed20d63e8023bcd8a7dccd15ecc5/redis/client.py#L852) instead?

XChikuX commented 1 year ago

Very possible. @chayim or @dvora-h will have to pitch in for us to be sure.