Skip to content

Commit 663bada

Browse files
Merge pull request #1077 from redis/DOC-4345-python-home-json
DOC-4345 added testable JSON index/query examples for Python
2 parents 7e5e585 + 6b9f46a commit 663bada

File tree

5 files changed

+50
-97
lines changed

5 files changed

+50
-97
lines changed

content/develop/clients/dotnet/queryjson.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ categories:
1010
- kubernetes
1111
- clients
1212
description: Learn how to use the Redis query engine with JSON
13-
linkTitle: JSON query example
13+
linkTitle: Index and query JSON
1414
title: Example - Index and query JSON documents
1515
weight: 2
1616
---

content/develop/clients/go/queryjson.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ categories:
1010
- kubernetes
1111
- clients
1212
description: Learn how to use the Redis query engine with JSON
13-
linkTitle: JSON query example
13+
linkTitle: Index and query JSON
1414
title: Example - Index and query JSON documents
1515
weight: 2
1616
---

content/develop/clients/jedis/queryjson.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ categories:
1010
- kubernetes
1111
- clients
1212
description: Learn how to use the Redis query engine with JSON
13-
linkTitle: JSON query example
13+
linkTitle: Index and query JSON
1414
title: Example - Index and query JSON documents
1515
weight: 2
1616
---

content/develop/clients/php/queryjson.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ categories:
1010
- kubernetes
1111
- clients
1212
description: Learn how to use the Redis query engine with JSON
13-
linkTitle: JSON query example
13+
linkTitle: Index and query JSON
1414
title: Example - Index and query JSON documents
1515
weight: 2
1616
---

content/develop/clients/redis-py/queryjson.md

Lines changed: 46 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ categories:
1010
- kubernetes
1111
- clients
1212
description: Learn how to use the Redis query engine with JSON
13-
linkTitle: JSON query example
13+
linkTitle: Index and query JSON
1414
title: Example - Index and query JSON documents
1515
weight: 2
1616
---
@@ -24,98 +24,51 @@ Make sure that you have Redis Stack and `redis-py` installed.
2424

2525
Import dependencies:
2626

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 >}}
3629

3730
Connect to your Redis database.
3831

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.
98-
99-
```python
100-
res = rs.search(
101-
Query("Paul @age:[30 40]")
102-
)
103-
# Result{1 total, docs: [Document {'id': 'user:3', 'payload': None, 'json': '{"name":"Paul Zamir","email":"paul.zamir@example.com","age":35,"city":"Tel Aviv"}'}]}
104-
```
105-
106-
Query using JSON Path expressions.
107-
108-
```python
109-
rs.search(
110-
Query("Paul").return_field("$.city", as_field="city")
111-
).docs
112-
# [Document {'id': 'user:1', 'payload': None, 'city': 'London'}, Document {'id': 'user:3', 'payload': None, 'city': 'Tel Aviv'}]
113-
```
114-
115-
Aggregate your results using [`FT.AGGREGATE`]({{< baseurl >}}/commands/ft.aggregate/).
116-
117-
```python
118-
req = aggregations.AggregateRequest("*").group_by('@city', reducers.count().alias('count'))
119-
print(rs.aggregate(req).rows)
120-
# [[b'city', b'Tel Aviv', b'count', b'2'], [b'city', b'London', b'count', b'1']]
121-
```
32+
{{< clients-example py_home_json connect >}}
33+
{{< /clients-example >}}
34+
35+
Create some test data to add to your database.
36+
37+
{{< clients-example py_home_json create_data >}}
38+
{{< /clients-example >}}
39+
40+
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
46+
[JSON]({{< relref "/develop/data-types/json" >}}) objects.
47+
If you use keys with the `user:` prefix then Redis will index the
48+
objects automatically as you add them:
49+
50+
{{< clients-example py_home_json add_data >}}
51+
{{< /clients-example >}}
52+
53+
You can now use the index to search the JSON objects. The
54+
[query]({{< relref "/develop/interact/search-and-query/query" >}})
55+
below searches for objects that have the text "Paul" in any field
56+
and have an `age` value in the range 30 to 40:
57+
58+
{{< clients-example py_home_json query1 >}}
59+
{{< /clients-example >}}
60+
61+
Specify query options to return only the `city` field:
62+
63+
{{< clients-example py_home_json query2 >}}
64+
{{< /clients-example >}}
65+
66+
Use an
67+
[aggregation query]({{< relref "/develop/interact/search-and-query/query/aggregation" >}})
68+
to count all users in each city.
69+
70+
{{< clients-example py_home_json query3 >}}
71+
{{< /clients-example >}}
72+
73+
See the [Redis query engine]({{< relref "/develop/interact/search-and-query" >}}) docs
74+
for a full description of all query features with examples.

0 commit comments

Comments
 (0)