You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/develop/clients/redis-py/queryjson.md
+46-93Lines changed: 46 additions & 93 deletions
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ categories:
10
10
- kubernetes
11
11
- clients
12
12
description: Learn how to use the Redis query engine with JSON
13
-
linkTitle: JSON query example
13
+
linkTitle: Index and query JSON
14
14
title: Example - Index and query JSON documents
15
15
weight: 2
16
16
---
@@ -24,98 +24,51 @@ Make sure that you have Redis Stack and `redis-py` installed.
24
24
25
25
Import dependencies:
26
26
27
-
```python
28
-
import redis
29
-
from redis.commands.json.path import Path
30
-
import redis.commands.search.aggregation as aggregations
31
-
import redis.commands.search.reducers as reducers
32
-
from redis.commands.search.field import TextField, NumericField, TagField
33
-
from redis.commands.search.indexDefinition import IndexDefinition, IndexType
34
-
from redis.commands.search.query import NumericFilter, Query
35
-
```
27
+
{{< clients-example py_home_json import >}}
28
+
{{< /clients-example >}}
36
29
37
30
Connect to your Redis database.
38
31
39
-
```python
40
-
r = redis.Redis(host='localhost', port=6379)
41
-
```
42
-
43
-
Let's create some test data to add to your database.
44
-
45
-
```python
46
-
user1 = {
47
-
"name": "Paul John",
48
-
"email": "paul.john@example.com",
49
-
"age": 42,
50
-
"city": "London"
51
-
}
52
-
user2 = {
53
-
"name": "Eden Zamir",
54
-
"email": "eden.zamir@example.com",
55
-
"age": 29,
56
-
"city": "Tel Aviv"
57
-
}
58
-
user3 = {
59
-
"name": "Paul Zamir",
60
-
"email": "paul.zamir@example.com",
61
-
"age": 35,
62
-
"city": "Tel Aviv"
63
-
}
64
-
```
65
-
66
-
Define indexed fields and their data types using `schema`. Use JSON path expressions to map specific JSON elements to the schema fields.
67
-
68
-
```python
69
-
schema = (
70
-
TextField("$.name", as_name="name"),
71
-
TagField("$.city", as_name="city"),
72
-
NumericField("$.age", as_name="age")
73
-
)
74
-
```
75
-
76
-
Create an index. In this example, all JSON documents with the key prefix `user:` will be indexed. For more information, see [Query syntax]({{< relref "/develop/interact/search-and-query/query/" >}}).
77
-
78
-
```python
79
-
rs = r.ft("idx:users")
80
-
rs.create_index(
81
-
schema,
82
-
definition=IndexDefinition(
83
-
prefix=["user:"], index_type=IndexType.JSON
84
-
)
85
-
)
86
-
# b'OK'
87
-
```
88
-
89
-
Use [`JSON.SET`]({{< baseurl >}}/commands/json.set/) to set each user value at the specified path.
90
-
91
-
```python
92
-
r.json().set("user:1", Path.root_path(), user1)
93
-
r.json().set("user:2", Path.root_path(), user2)
94
-
r.json().set("user:3", Path.root_path(), user3)
95
-
```
96
-
97
-
Let's find user `Paul` and filter the results by age.
Create an index. In this example, only JSON documents with the key prefix `user:` are indexed. For more information, see [Query syntax]({{< relref "/develop/interact/search-and-query/query/" >}}).
41
+
42
+
{{< clients-example py_home_json make_index >}}
43
+
{{< /clients-example >}}
44
+
45
+
Add the three sets of user data to the database as
0 commit comments