Skip to content

Instantly share code, notes, and snippets.

@up1
Last active June 5, 2026 16:20
Show Gist options
  • Select an option

  • Save up1/f4e812e550b0f56edb4ea5873472ae46 to your computer and use it in GitHub Desktop.

Select an option

Save up1/f4e812e550b0f56edb4ea5873472ae46 to your computer and use it in GitHub Desktop.
Demo Redis with SQL
$docker container run -d -p 6379:6379 redis:8.8
$docker container ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
89bdbf4df9c9 redis:8.8 "docker-entrypoint.s…" 15 seconds ago Up 15 seconds 0.0.0.0:6379->6379/tcp, [::]:6379->6379/tcp pensive_mendel
$pip install sentence-transformers
$pip install "redisvl[sql-redis]"
$pip list
Package Version
----------------- -------
annotated-types 0.7.0
jsonpath-ng 1.8.0
ml_dtypes 0.5.4
numpy 2.4.6
pip 24.3.1
pydantic 2.13.4
pydantic_core 2.46.4
python-ulid 3.1.0
PyYAML 6.0.3
redis 7.4.1
redisvl 0.20.0
sql-redis 0.6.0
sqlglot 30.9.0
tenacity 9.1.4
typing_extensions 4.15.0
typing-inspection 0.4.2
from redisvl.utils.vectorize import HFTextVectorizer
from redisvl.index import SearchIndex
from redis import Redis
# 1. Design schema of data
hf = HFTextVectorizer()
schema = {
"index": {
"name": "user_simple",
"prefix": "user_simple_docs",
"storage_type": "json",
},
"fields": [
{"name": "user", "type": "tag"},
{"name": "region", "type": "tag"},
{"name": "job", "type": "tag"},
{"name": "job_description", "type": "text"},
{"name": "age", "type": "numeric"},
{"name": "office_location", "type": "geo"},
{
"name": "job_embedding",
"type": "vector",
"attrs": {
"dims": len(hf.embed("get embed length")),
"distance_metric": "cosine",
"algorithm": "flat",
"datatype": "float32"
}
}
]
}
# 2. Sample data
data = [
{
'user': 'john',
'age': 34,
'job': 'software engineer',
'region': 'us-west',
'job_description': 'Designs, develops, and maintains software applications and systems.',
'office_location': '-122.4194,37.7749' # San Francisco
},
{
'user': 'bill',
'age': 54,
'job': 'engineer',
'region': 'us-central',
'job_description': 'Applies scientific and mathematical principles to solve technical problems.',
'office_location': '-87.6298,41.8781' # Chicago
}
]
# 3. Create index and insert data
client = Redis.from_url("redis://localhost:6379")
index = SearchIndex.from_dict(schema, redis_client=client)
index.create(overwrite=True, drop=True)
index.load(data)
# Close the connection
client.close()
from redisvl.query import SQLQuery
from redisvl.index import SearchIndex
from redis import Redis
from redisvl.utils.vectorize import HFTextVectorizer
hf = HFTextVectorizer()
schema = {
"index": {
"name": "user_simple",
"prefix": "user_simple_docs",
"storage_type": "json",
},
"fields": [
{"name": "user", "type": "tag"},
{"name": "region", "type": "tag"},
{"name": "job", "type": "tag"},
{"name": "job_description", "type": "text"},
{"name": "age", "type": "numeric"},
{"name": "office_location", "type": "geo"},
{
"name": "job_embedding",
"type": "vector",
"attrs": {
"dims": len(hf.embed("get embed length")),
"distance_metric": "cosine",
"algorithm": "flat",
"datatype": "float32"
}
}
]
}
sql_str = """
SELECT user, region, job, age
FROM user_simple
WHERE age > 40
"""
sql_query = SQLQuery(sql_str)
client = Redis.from_url("redis://localhost:6379")
index = SearchIndex.from_dict(schema, redis_client=client)
results = index.query(sql_query)
print(results)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment