Skip to content

Commit 509c77c

Browse files
authored
add pagination, sorting and grouping examples to search json example (#2890)
1 parent 578fb26 commit 509c77c

File tree

1 file changed

+110
-11
lines changed

1 file changed

+110
-11
lines changed

docs/examples/search_json_examples.ipynb

Lines changed: 110 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"from redis.commands.search.query import NumericFilter, Query\n",
3535
"\n",
3636
"\n",
37-
"r = redis.Redis(host='localhost', port=36379)\n",
37+
"r = redis.Redis(host='localhost', port=6379)\n",
3838
"user1 = {\n",
3939
" \"user\":{\n",
4040
" \"name\": \"Paul John\",\n",
@@ -59,9 +59,19 @@
5959
" \"city\": \"Tel Aviv\"\n",
6060
" }\n",
6161
"}\n",
62+
"\n",
63+
"user4 = {\n",
64+
" \"user\":{\n",
65+
" \"name\": \"Sarah Zamir\",\n",
66+
" \"email\": \"[email protected]\",\n",
67+
" \"age\": 30,\n",
68+
" \"city\": \"Paris\"\n",
69+
" }\n",
70+
"}\n",
6271
"r.json().set(\"user:1\", Path.root_path(), user1)\n",
6372
"r.json().set(\"user:2\", Path.root_path(), user2)\n",
6473
"r.json().set(\"user:3\", Path.root_path(), user3)\n",
74+
"r.json().set(\"user:4\", Path.root_path(), user4)\n",
6575
"\n",
6676
"schema = (TextField(\"$.user.name\", as_name=\"name\"),TagField(\"$.user.city\", as_name=\"city\"), NumericField(\"$.user.age\", as_name=\"age\"))\n",
6777
"r.ft().create_index(schema, definition=IndexDefinition(prefix=[\"user:\"], index_type=IndexType.JSON))"
@@ -102,6 +112,7 @@
102112
]
103113
},
104114
{
115+
"attachments": {},
105116
"cell_type": "markdown",
106117
"metadata": {},
107118
"source": [
@@ -133,13 +144,72 @@
133144
"cell_type": "markdown",
134145
"metadata": {},
135146
"source": [
136-
"### Projecting using JSON Path expressions "
147+
"### Paginating and Ordering search Results"
137148
]
138149
},
139150
{
140151
"cell_type": "code",
141152
"execution_count": 4,
142153
"metadata": {},
154+
"outputs": [
155+
{
156+
"data": {
157+
"text/plain": [
158+
"Result{4 total, docs: [Document {'id': 'user:1', 'payload': None, 'age': '42', 'json': '{\"user\":{\"name\":\"Paul John\",\"email\":\"[email protected]\",\"age\":42,\"city\":\"London\"}}'}, Document {'id': 'user:3', 'payload': None, 'age': '35', 'json': '{\"user\":{\"name\":\"Paul Zamir\",\"email\":\"[email protected]\",\"age\":35,\"city\":\"Tel Aviv\"}}'}]}"
159+
]
160+
},
161+
"execution_count": 4,
162+
"metadata": {},
163+
"output_type": "execute_result"
164+
}
165+
],
166+
"source": [
167+
"# Search for all users, returning 2 users at a time and sorting by age in descending order\n",
168+
"offset = 0\n",
169+
"num = 2\n",
170+
"q = Query(\"*\").paging(offset, num).sort_by(\"age\", asc=False) # pass asc=True to sort in ascending order\n",
171+
"r.ft().search(q)"
172+
]
173+
},
174+
{
175+
"cell_type": "markdown",
176+
"metadata": {},
177+
"source": [
178+
"### Counting the total number of Items"
179+
]
180+
},
181+
{
182+
"cell_type": "code",
183+
"execution_count": 5,
184+
"metadata": {},
185+
"outputs": [
186+
{
187+
"data": {
188+
"text/plain": [
189+
"4"
190+
]
191+
},
192+
"execution_count": 5,
193+
"metadata": {},
194+
"output_type": "execute_result"
195+
}
196+
],
197+
"source": [
198+
"q = Query(\"*\").paging(0, 0)\n",
199+
"r.ft().search(q).total"
200+
]
201+
},
202+
{
203+
"cell_type": "markdown",
204+
"metadata": {},
205+
"source": [
206+
"### Projecting using JSON Path expressions "
207+
]
208+
},
209+
{
210+
"cell_type": "code",
211+
"execution_count": 6,
212+
"metadata": {},
143213
"outputs": [
144214
{
145215
"data": {
@@ -148,7 +218,7 @@
148218
" Document {'id': 'user:3', 'payload': None, 'city': 'Tel Aviv'}]"
149219
]
150220
},
151-
"execution_count": 4,
221+
"execution_count": 6,
152222
"metadata": {},
153223
"output_type": "execute_result"
154224
}
@@ -166,7 +236,7 @@
166236
},
167237
{
168238
"cell_type": "code",
169-
"execution_count": 5,
239+
"execution_count": 7,
170240
"metadata": {},
171241
"outputs": [
172242
{
@@ -175,7 +245,7 @@
175245
"[[b'age', b'35'], [b'age', b'42']]"
176246
]
177247
},
178-
"execution_count": 5,
248+
"execution_count": 7,
179249
"metadata": {},
180250
"output_type": "execute_result"
181251
}
@@ -184,16 +254,46 @@
184254
"req = aggregations.AggregateRequest(\"Paul\").sort_by(\"@age\")\n",
185255
"r.ft().aggregate(req).rows"
186256
]
257+
},
258+
{
259+
"cell_type": "markdown",
260+
"metadata": {},
261+
"source": [
262+
"### Count the total number of Items"
263+
]
264+
},
265+
{
266+
"cell_type": "code",
267+
"execution_count": 8,
268+
"metadata": {},
269+
"outputs": [
270+
{
271+
"data": {
272+
"text/plain": [
273+
"[[b'total', b'4']]"
274+
]
275+
},
276+
"execution_count": 8,
277+
"metadata": {},
278+
"output_type": "execute_result"
279+
}
280+
],
281+
"source": [
282+
"# The group_by expects a string or list of strings to group the results before applying the aggregation function to\n",
283+
"# each group. Passing an empty list here acts as `GROUPBY 0` which applies the aggregation function to the whole results\n",
284+
"req = aggregations.AggregateRequest(\"*\").group_by([], reducers.count().alias(\"total\"))\n",
285+
"r.ft().aggregate(req).rows"
286+
]
187287
}
188288
],
189289
"metadata": {
190290
"interpreter": {
191291
"hash": "d45c99ba0feda92868abafa8257cbb4709c97f1a0b5dc62bbeebdf89d4fad7fe"
192292
},
193293
"kernelspec": {
194-
"display_name": "Python 3.8.12 64-bit ('venv': venv)",
294+
"display_name": "redis-py",
195295
"language": "python",
196-
"name": "python3"
296+
"name": "redis-py"
197297
},
198298
"language_info": {
199299
"codemirror_mode": {
@@ -205,10 +305,9 @@
205305
"name": "python",
206306
"nbconvert_exporter": "python",
207307
"pygments_lexer": "ipython3",
208-
"version": "3.8.12"
209-
},
210-
"orig_nbformat": 4
308+
"version": "3.11.3"
309+
}
211310
},
212311
"nbformat": 4,
213-
"nbformat_minor": 2
312+
"nbformat_minor": 4
214313
}

0 commit comments

Comments
 (0)