-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add pagination, sorting and grouping examples to search json example (#…
- Loading branch information
Showing
1 changed file
with
110 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,7 @@ | |
"from redis.commands.search.query import NumericFilter, Query\n", | ||
"\n", | ||
"\n", | ||
"r = redis.Redis(host='localhost', port=36379)\n", | ||
"r = redis.Redis(host='localhost', port=6379)\n", | ||
"user1 = {\n", | ||
" \"user\":{\n", | ||
" \"name\": \"Paul John\",\n", | ||
|
@@ -59,9 +59,19 @@ | |
" \"city\": \"Tel Aviv\"\n", | ||
" }\n", | ||
"}\n", | ||
"\n", | ||
"user4 = {\n", | ||
" \"user\":{\n", | ||
" \"name\": \"Sarah Zamir\",\n", | ||
" \"email\": \"[email protected]\",\n", | ||
" \"age\": 30,\n", | ||
" \"city\": \"Paris\"\n", | ||
" }\n", | ||
"}\n", | ||
"r.json().set(\"user:1\", Path.root_path(), user1)\n", | ||
"r.json().set(\"user:2\", Path.root_path(), user2)\n", | ||
"r.json().set(\"user:3\", Path.root_path(), user3)\n", | ||
"r.json().set(\"user:4\", Path.root_path(), user4)\n", | ||
"\n", | ||
"schema = (TextField(\"$.user.name\", as_name=\"name\"),TagField(\"$.user.city\", as_name=\"city\"), NumericField(\"$.user.age\", as_name=\"age\"))\n", | ||
"r.ft().create_index(schema, definition=IndexDefinition(prefix=[\"user:\"], index_type=IndexType.JSON))" | ||
|
@@ -102,6 +112,7 @@ | |
] | ||
}, | ||
{ | ||
"attachments": {}, | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
|
@@ -133,13 +144,72 @@ | |
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Projecting using JSON Path expressions " | ||
"### Paginating and Ordering search Results" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"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\"}}'}]}" | ||
] | ||
}, | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"# Search for all users, returning 2 users at a time and sorting by age in descending order\n", | ||
"offset = 0\n", | ||
"num = 2\n", | ||
"q = Query(\"*\").paging(offset, num).sort_by(\"age\", asc=False) # pass asc=True to sort in ascending order\n", | ||
"r.ft().search(q)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Counting the total number of Items" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"4" | ||
] | ||
}, | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"q = Query(\"*\").paging(0, 0)\n", | ||
"r.ft().search(q).total" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Projecting using JSON Path expressions " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
|
@@ -148,7 +218,7 @@ | |
" Document {'id': 'user:3', 'payload': None, 'city': 'Tel Aviv'}]" | ||
] | ||
}, | ||
"execution_count": 4, | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
|
@@ -166,7 +236,7 @@ | |
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"execution_count": 7, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
|
@@ -175,7 +245,7 @@ | |
"[[b'age', b'35'], [b'age', b'42']]" | ||
] | ||
}, | ||
"execution_count": 5, | ||
"execution_count": 7, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
|
@@ -184,16 +254,46 @@ | |
"req = aggregations.AggregateRequest(\"Paul\").sort_by(\"@age\")\n", | ||
"r.ft().aggregate(req).rows" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Count the total number of Items" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"[[b'total', b'4']]" | ||
] | ||
}, | ||
"execution_count": 8, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"# The group_by expects a string or list of strings to group the results before applying the aggregation function to\n", | ||
"# each group. Passing an empty list here acts as `GROUPBY 0` which applies the aggregation function to the whole results\n", | ||
"req = aggregations.AggregateRequest(\"*\").group_by([], reducers.count().alias(\"total\"))\n", | ||
"r.ft().aggregate(req).rows" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"interpreter": { | ||
"hash": "d45c99ba0feda92868abafa8257cbb4709c97f1a0b5dc62bbeebdf89d4fad7fe" | ||
}, | ||
"kernelspec": { | ||
"display_name": "Python 3.8.12 64-bit ('venv': venv)", | ||
"display_name": "redis-py", | ||
"language": "python", | ||
"name": "python3" | ||
"name": "redis-py" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
|
@@ -205,10 +305,9 @@ | |
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.8.12" | ||
}, | ||
"orig_nbformat": 4 | ||
"version": "3.11.3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
"nbformat_minor": 4 | ||
} |