|
34 | 34 | "from redis.commands.search.query import NumericFilter, Query\n",
|
35 | 35 | "\n",
|
36 | 36 | "\n",
|
37 |
| - "r = redis.Redis(host='localhost', port=36379)\n", |
| 37 | + "r = redis.Redis(host='localhost', port=6379)\n", |
38 | 38 | "user1 = {\n",
|
39 | 39 | " \"user\":{\n",
|
40 | 40 | " \"name\": \"Paul John\",\n",
|
|
59 | 59 | " \"city\": \"Tel Aviv\"\n",
|
60 | 60 | " }\n",
|
61 | 61 | "}\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", |
62 | 71 | "r.json().set(\"user:1\", Path.root_path(), user1)\n",
|
63 | 72 | "r.json().set(\"user:2\", Path.root_path(), user2)\n",
|
64 | 73 | "r.json().set(\"user:3\", Path.root_path(), user3)\n",
|
| 74 | + "r.json().set(\"user:4\", Path.root_path(), user4)\n", |
65 | 75 | "\n",
|
66 | 76 | "schema = (TextField(\"$.user.name\", as_name=\"name\"),TagField(\"$.user.city\", as_name=\"city\"), NumericField(\"$.user.age\", as_name=\"age\"))\n",
|
67 | 77 | "r.ft().create_index(schema, definition=IndexDefinition(prefix=[\"user:\"], index_type=IndexType.JSON))"
|
|
102 | 112 | ]
|
103 | 113 | },
|
104 | 114 | {
|
| 115 | + "attachments": {}, |
105 | 116 | "cell_type": "markdown",
|
106 | 117 | "metadata": {},
|
107 | 118 | "source": [
|
|
133 | 144 | "cell_type": "markdown",
|
134 | 145 | "metadata": {},
|
135 | 146 | "source": [
|
136 |
| - "### Projecting using JSON Path expressions " |
| 147 | + "### Paginating and Ordering search Results" |
137 | 148 | ]
|
138 | 149 | },
|
139 | 150 | {
|
140 | 151 | "cell_type": "code",
|
141 | 152 | "execution_count": 4,
|
142 | 153 | "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": {}, |
143 | 213 | "outputs": [
|
144 | 214 | {
|
145 | 215 | "data": {
|
|
148 | 218 | " Document {'id': 'user:3', 'payload': None, 'city': 'Tel Aviv'}]"
|
149 | 219 | ]
|
150 | 220 | },
|
151 |
| - "execution_count": 4, |
| 221 | + "execution_count": 6, |
152 | 222 | "metadata": {},
|
153 | 223 | "output_type": "execute_result"
|
154 | 224 | }
|
|
166 | 236 | },
|
167 | 237 | {
|
168 | 238 | "cell_type": "code",
|
169 |
| - "execution_count": 5, |
| 239 | + "execution_count": 7, |
170 | 240 | "metadata": {},
|
171 | 241 | "outputs": [
|
172 | 242 | {
|
|
175 | 245 | "[[b'age', b'35'], [b'age', b'42']]"
|
176 | 246 | ]
|
177 | 247 | },
|
178 |
| - "execution_count": 5, |
| 248 | + "execution_count": 7, |
179 | 249 | "metadata": {},
|
180 | 250 | "output_type": "execute_result"
|
181 | 251 | }
|
|
184 | 254 | "req = aggregations.AggregateRequest(\"Paul\").sort_by(\"@age\")\n",
|
185 | 255 | "r.ft().aggregate(req).rows"
|
186 | 256 | ]
|
| 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 | + ] |
187 | 287 | }
|
188 | 288 | ],
|
189 | 289 | "metadata": {
|
190 | 290 | "interpreter": {
|
191 | 291 | "hash": "d45c99ba0feda92868abafa8257cbb4709c97f1a0b5dc62bbeebdf89d4fad7fe"
|
192 | 292 | },
|
193 | 293 | "kernelspec": {
|
194 |
| - "display_name": "Python 3.8.12 64-bit ('venv': venv)", |
| 294 | + "display_name": "redis-py", |
195 | 295 | "language": "python",
|
196 |
| - "name": "python3" |
| 296 | + "name": "redis-py" |
197 | 297 | },
|
198 | 298 | "language_info": {
|
199 | 299 | "codemirror_mode": {
|
|
205 | 305 | "name": "python",
|
206 | 306 | "nbconvert_exporter": "python",
|
207 | 307 | "pygments_lexer": "ipython3",
|
208 |
| - "version": "3.8.12" |
209 |
| - }, |
210 |
| - "orig_nbformat": 4 |
| 308 | + "version": "3.11.3" |
| 309 | + } |
211 | 310 | },
|
212 | 311 | "nbformat": 4,
|
213 |
| - "nbformat_minor": 2 |
| 312 | + "nbformat_minor": 4 |
214 | 313 | }
|
0 commit comments