Skip to content

Commit 4f93c78

Browse files
DOC-4345 added testable JSON search examples for home page
1 parent 2e46613 commit 4f93c78

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed

doctests/home_json.py

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# EXAMPLE: py_home_json
2+
"""
3+
JSON examples from redis-py "home" page"
4+
https://redis.io/docs/latest/develop/connect/clients/python/redis-py/#example-indexing-and-querying-json-documents
5+
"""
6+
7+
# STEP_START import
8+
import redis
9+
from redis.commands.json.path import Path
10+
import redis.commands.search.aggregation as aggregations
11+
import redis.commands.search.reducers as reducers
12+
from redis.commands.search.field import TextField, NumericField, TagField
13+
from redis.commands.search.indexDefinition import IndexDefinition, IndexType
14+
from redis.commands.search.query import Query
15+
import redis.exceptions
16+
# STEP_END
17+
18+
# STEP_START connect
19+
r = redis.Redis(decode_responses=True)
20+
# STEP_END
21+
22+
# REMOVE_START
23+
try:
24+
r.ft("idx:users").dropindex(True)
25+
except redis.exceptions.ResponseError:
26+
pass
27+
r.delete("user:1", "user:2", "user:3")
28+
# REMOVE_END
29+
30+
# STEP_START create_data
31+
user1 = {
32+
"name": "Paul John",
33+
"email": "[email protected]",
34+
"age": 42,
35+
"city": "London"
36+
}
37+
user2 = {
38+
"name": "Eden Zamir",
39+
"email": "[email protected]",
40+
"age": 29,
41+
"city": "Tel Aviv"
42+
}
43+
user3 = {
44+
"name": "Paul Zamir",
45+
"email": "[email protected]",
46+
"age": 35,
47+
"city": "Tel Aviv"
48+
}
49+
# STEP_END
50+
51+
# STEP_START make_index
52+
schema = (
53+
TextField("$.name", as_name="name"),
54+
TagField("$.city", as_name="city"),
55+
NumericField("$.age", as_name="age")
56+
)
57+
58+
indexCreated = r.ft("idx:users").create_index(
59+
schema,
60+
definition=IndexDefinition(
61+
prefix=["user:"], index_type=IndexType.JSON
62+
)
63+
)
64+
# STEP_END
65+
66+
# Tests for 'make_index' step.
67+
# REMOVE_START
68+
assert indexCreated
69+
# REMOVE_END
70+
71+
72+
# STEP_START add_data
73+
user1Set = r.json().set("user:1", Path.root_path(), user1)
74+
user2Set = r.json().set("user:2", Path.root_path(), user2)
75+
user3Set = r.json().set("user:3", Path.root_path(), user3)
76+
# STEP_END
77+
78+
# Tests for 'add_data' step.
79+
# REMOVE_START
80+
assert user1Set
81+
assert user2Set
82+
assert user3Set
83+
# REMOVE_END
84+
85+
86+
# STEP_START query1
87+
findPaulResult = r.ft("idx:users").search(
88+
Query("Paul @age:[30 40]")
89+
)
90+
91+
print(findPaulResult)
92+
# >>> Result{1 total, docs: [Document {'id': 'user:3', ...
93+
# STEP_END
94+
95+
# Tests for 'query1' step.
96+
# REMOVE_START
97+
assert str(findPaulResult) == (
98+
"Result{1 total, docs: [Document {'id': 'user:3', 'payload': None, "
99+
+ "'json': '{\"name\":\"Paul Zamir\",\"email\":"
100+
+ "\"[email protected]\",\"age\":35,\"city\":\"Tel Aviv\"}'}]}"
101+
)
102+
# REMOVE_END
103+
104+
105+
# STEP_START query2
106+
citiesResult = r.ft("idx:users").search(
107+
Query("Paul").return_field("$.city", as_field="city")
108+
).docs
109+
110+
print(citiesResult)
111+
# >>> [Document {'id': 'user:1', 'payload': None, ...
112+
# STEP_END
113+
114+
# Tests for 'query2' step.
115+
# REMOVE_START
116+
assert str(citiesResult) == (
117+
"[Document {'id': 'user:1', 'payload': None, 'city': 'London'}, "
118+
+ "Document {'id': 'user:3', 'payload': None, 'city': 'Tel Aviv'}]"
119+
)
120+
# REMOVE_END
121+
122+
123+
# STEP_START query3
124+
req = aggregations.AggregateRequest("*").group_by(
125+
'@city', reducers.count().alias('count')
126+
)
127+
128+
aggResult = r.ft("idx:users").aggregate(req).rows
129+
print(aggResult)
130+
# >>> [['city', 'London', 'count', '1'], ['city', 'Tel Aviv', 'count', '2']]
131+
# STEP_END
132+
133+
# Tests for 'query3' step.
134+
# REMOVE_START
135+
assert str(aggResult) == (
136+
"[['city', 'London', 'count', '1'], ['city', 'Tel Aviv', 'count', '2']]"
137+
)
138+
# REMOVE_END

0 commit comments

Comments
 (0)