Skip to content

Instantly share code, notes, and snippets.

@abrookins
Created November 20, 2021 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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

dhavalaprashanth commented Feb 7, 2023

Can you brief about what are you doing in the line 29 and 30 . Are you trying to send redis connection to Migrator (well but Migrator accepts a module name which should be quoted in strings). I am a beginner and could not understand that case and also i am getting an error in 33 line saying "unknown command ft.search". @abrookins

@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