Skip to content

Instantly share code, notes, and snippets.

@abrookins
Created November 20, 2021 01:36
Show Gist options
  • Save abrookins/ed2420a35126e4237b892bec8d283401 to your computer and use it in GitHub Desktop.
Save abrookins/ed2420a35126e4237b892bec8d283401 to your computer and use it in GitHub Desktop.
Rich query expressions with Redis OM for Python
import datetime
from typing import Optional
from pydantic import EmailStr
from redis_om import (
Field,
HashModel,
Migrator
)
from redis_om import get_redis_connection
class Customer(HashModel):
first_name: str
last_name: str = Field(index=True)
email: EmailStr
join_date: datetime.date
age: int = Field(index=True)
bio: Optional[str]
# Now, if we use this model with a Redis deployment that has the
# RediSearch module installed, we can run queries like the following.
# Before running queries, we need to run migrations to set up the
# indexes that Redis OM will use. You can also use the `migrate`
# CLI tool for this!
redis = get_redis_connection()
Migrator(redis).run()
# Find all customers with the last name "Brookins"
Customer.find(Customer.last_name == "Brookins").all()
# Find all customers that do NOT have the last name "Brookins"
Customer.find(Customer.last_name != "Brookins").all()
# Find all customers whose last name is "Brookins" OR whose age is
# 100 AND whose last name is "Smith"
Customer.find((Customer.last_name == "Brookins") | (
Customer.age == 100
) & (Customer.last_name == "Smith")).all()
@dhavalaprashanth
Copy link

Hey @abrookins i cleared these two issues first one is no need to send anything for Migrator().run() because it accepts a optional python package not connection and also i ran redis server in redis stack docker so that i can perform ft.search

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment