"""
JSON examples from redis-py "home" page"
https://redis.ac.cn/docs/latest/develop/connect/clients/python/redis-py/#example-indexing-and-querying-json-documents
"""
import redis
from redis.commands.json.path import Path
import redis.commands.search.aggregation as aggregations
import redis.commands.search.reducers as reducers
from redis.commands.search.field import TextField, NumericField, TagField
from redis.commands.search.index_definition import IndexDefinition, IndexType
from redis.commands.search.query import Query
import redis.exceptions
r = redis.Redis(decode_responses=True)
user1 = {
"name": "Paul John",
"email": "[email protected]",
"age": 42,
"city": "London"
}
user2 = {
"name": "Eden Zamir",
"email": "[email protected]",
"age": 29,
"city": "Tel Aviv"
}
user3 = {
"name": "Paul Zamir",
"email": "[email protected]",
"age": 35,
"city": "Tel Aviv"
}
schema = (
TextField("$.name", as_name="name"),
TagField("$.city", as_name="city"),
NumericField("$.age", as_name="age")
)
indexCreated = r.ft("idx:users").create_index(
schema,
definition=IndexDefinition(
prefix=["user:"], index_type=IndexType.JSON
)
)
# Tests for 'make_index' step.
user1Set = r.json().set("user:1", Path.root_path(), user1)
user2Set = r.json().set("user:2", Path.root_path(), user2)
user3Set = r.json().set("user:3", Path.root_path(), user3)
# Tests for 'add_data' step.
findPaulResult = r.ft("idx:users").search(
Query("Paul @age:[30 40]")
)
print(findPaulResult)
# >>> Result{1 total, docs: [Document {'id': 'user:3', ...
# Tests for 'query1' step.
citiesResult = r.ft("idx:users").search(
Query("Paul").return_field("$.city", as_field="city")
).docs
print(citiesResult)
# >>> [Document {'id': 'user:1', 'payload': None, ...
# Tests for 'query2' step.
req = aggregations.AggregateRequest("*").group_by(
'@city', reducers.count().alias('count')
)
aggResult = r.ft("idx:users").aggregate(req).rows
print(aggResult)
# >>> [['city', 'London', 'count', '1'], ['city', 'Tel Aviv', 'count', '2']]
# Tests for 'query3' step.
hashSchema = (
TextField("name"),
TagField("city"),
NumericField("age")
)
hashIndexCreated = r.ft("hash-idx:users").create_index(
hashSchema,
definition=IndexDefinition(
prefix=["huser:"], index_type=IndexType.HASH
)
)
huser1Set = r.hset("huser:1", mapping=user1)
huser2Set = r.hset("huser:2", mapping=user2)
huser3Set = r.hset("huser:3", mapping=user3)
findPaulHashResult = r.ft("hash-idx:users").search(
Query("Paul @age:[30 40]")
)
print(findPaulHashResult)
# >>> Result{1 total, docs: [Document {'id': 'huser:3',
# >>> 'payload': None, 'name': 'Paul Zamir', ...
r.close()