Skip to content

Commit eff310f

Browse files
DOC-4345 added testable JSON search examples for home page (#3407)
* DOC-4345 added testable JSON search examples for home page * DOC-4345 avoid possible non-deterministic results in tests * DOC-4345 close connection at end of example * DOC-4345 remove unnecessary blank lines
1 parent afabed6 commit eff310f

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed

doctests/home_json.py

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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+
28+
r.delete("user:1", "user:2", "user:3")
29+
# REMOVE_END
30+
# STEP_START create_data
31+
user1 = {
32+
"name": "Paul John",
33+
"email": "[email protected]",
34+
"age": 42,
35+
"city": "London"
36+
}
37+
38+
user2 = {
39+
"name": "Eden Zamir",
40+
"email": "[email protected]",
41+
"age": 29,
42+
"city": "Tel Aviv"
43+
}
44+
45+
user3 = {
46+
"name": "Paul Zamir",
47+
"email": "[email protected]",
48+
"age": 35,
49+
"city": "Tel Aviv"
50+
}
51+
# STEP_END
52+
53+
# STEP_START make_index
54+
schema = (
55+
TextField("$.name", as_name="name"),
56+
TagField("$.city", as_name="city"),
57+
NumericField("$.age", as_name="age")
58+
)
59+
60+
indexCreated = r.ft("idx:users").create_index(
61+
schema,
62+
definition=IndexDefinition(
63+
prefix=["user:"], index_type=IndexType.JSON
64+
)
65+
)
66+
# STEP_END
67+
# Tests for 'make_index' step.
68+
# REMOVE_START
69+
assert indexCreated
70+
# REMOVE_END
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+
# Tests for 'add_data' step.
78+
# REMOVE_START
79+
assert user1Set
80+
assert user2Set
81+
assert user3Set
82+
# REMOVE_END
83+
84+
# STEP_START query1
85+
findPaulResult = r.ft("idx:users").search(
86+
Query("Paul @age:[30 40]")
87+
)
88+
89+
print(findPaulResult)
90+
# >>> Result{1 total, docs: [Document {'id': 'user:3', ...
91+
# STEP_END
92+
# Tests for 'query1' step.
93+
# REMOVE_START
94+
assert str(findPaulResult) == (
95+
"Result{1 total, docs: [Document {'id': 'user:3', 'payload': None, "
96+
+ "'json': '{\"name\":\"Paul Zamir\",\"email\":"
97+
+ "\"[email protected]\",\"age\":35,\"city\":\"Tel Aviv\"}'}]}"
98+
)
99+
# REMOVE_END
100+
101+
# STEP_START query2
102+
citiesResult = r.ft("idx:users").search(
103+
Query("Paul").return_field("$.city", as_field="city")
104+
).docs
105+
106+
print(citiesResult)
107+
# >>> [Document {'id': 'user:1', 'payload': None, ...
108+
# STEP_END
109+
# Tests for 'query2' step.
110+
# REMOVE_START
111+
citiesResult.sort(key=lambda doc: doc['id'])
112+
113+
assert str(citiesResult) == (
114+
"[Document {'id': 'user:1', 'payload': None, 'city': 'London'}, "
115+
+ "Document {'id': 'user:3', 'payload': None, 'city': 'Tel Aviv'}]"
116+
)
117+
# REMOVE_END
118+
119+
# STEP_START query3
120+
req = aggregations.AggregateRequest("*").group_by(
121+
'@city', reducers.count().alias('count')
122+
)
123+
124+
aggResult = r.ft("idx:users").aggregate(req).rows
125+
print(aggResult)
126+
# >>> [['city', 'London', 'count', '1'], ['city', 'Tel Aviv', 'count', '2']]
127+
# STEP_END
128+
# Tests for 'query3' step.
129+
# REMOVE_START
130+
aggResult.sort(key=lambda row: row[1])
131+
132+
assert str(aggResult) == (
133+
"[['city', 'London', 'count', '1'], ['city', 'Tel Aviv', 'count', '2']]"
134+
)
135+
# REMOVE_END
136+
137+
r.close()

0 commit comments

Comments
 (0)