Skip to content

Commit 2a1ff87

Browse files
authored
Merge branch 'master' into master
2 parents 387ddde + b2dd8e8 commit 2a1ff87

File tree

18 files changed

+332
-97
lines changed

18 files changed

+332
-97
lines changed

.github/workflows/integration.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ jobs:
7474
max-parallel: 15
7575
fail-fast: false
7676
matrix:
77-
redis-version: [ '${{ needs.redis_version.outputs.CURRENT }}', '7.2.6', '6.2.16']
77+
redis-version: ['8.0-M02', '${{ needs.redis_version.outputs.CURRENT }}', '7.2.6', '6.2.16']
7878
python-version: ['3.8', '3.12']
7979
parser-backend: ['plain']
8080
event-loop: ['asyncio']

doctests/home_json.py

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# EXAMPLE: py_home_json
2+
"""
3+
JSON examples from redis-py "home" page"
4+
https://redis.io/docs/latest/develop/connect/clients/python/redis-py/#example-indexing-and-querying-json-documents
5+
"""
6+
7+
# STEP_START import
8+
import redis
9+
from redis.commands.json.path import Path
10+
import redis.commands.search.aggregation as aggregations
11+
import redis.commands.search.reducers as reducers
12+
from redis.commands.search.field import TextField, NumericField, TagField
13+
from redis.commands.search.indexDefinition import IndexDefinition, IndexType
14+
from redis.commands.search.query import Query
15+
import redis.exceptions
16+
# STEP_END
17+
18+
# STEP_START connect
19+
r = redis.Redis(decode_responses=True)
20+
# STEP_END
21+
22+
# REMOVE_START
23+
try:
24+
r.ft("idx:users").dropindex(True)
25+
except redis.exceptions.ResponseError:
26+
pass
27+
28+
r.delete("user:1", "user:2", "user:3")
29+
# REMOVE_END
30+
# STEP_START create_data
31+
user1 = {
32+
"name": "Paul John",
33+
"email": "[email protected]",
34+
"age": 42,
35+
"city": "London"
36+
}
37+
38+
user2 = {
39+
"name": "Eden Zamir",
40+
"email": "[email protected]",
41+
"age": 29,
42+
"city": "Tel Aviv"
43+
}
44+
45+
user3 = {
46+
"name": "Paul Zamir",
47+
"email": "[email protected]",
48+
"age": 35,
49+
"city": "Tel Aviv"
50+
}
51+
# STEP_END
52+
53+
# STEP_START make_index
54+
schema = (
55+
TextField("$.name", as_name="name"),
56+
TagField("$.city", as_name="city"),
57+
NumericField("$.age", as_name="age")
58+
)
59+
60+
indexCreated = r.ft("idx:users").create_index(
61+
schema,
62+
definition=IndexDefinition(
63+
prefix=["user:"], index_type=IndexType.JSON
64+
)
65+
)
66+
# STEP_END
67+
# Tests for 'make_index' step.
68+
# REMOVE_START
69+
assert indexCreated
70+
# REMOVE_END
71+
72+
# STEP_START add_data
73+
user1Set = r.json().set("user:1", Path.root_path(), user1)
74+
user2Set = r.json().set("user:2", Path.root_path(), user2)
75+
user3Set = r.json().set("user:3", Path.root_path(), user3)
76+
# STEP_END
77+
# Tests for 'add_data' step.
78+
# REMOVE_START
79+
assert user1Set
80+
assert user2Set
81+
assert user3Set
82+
# REMOVE_END
83+
84+
# STEP_START query1
85+
findPaulResult = r.ft("idx:users").search(
86+
Query("Paul @age:[30 40]")
87+
)
88+
89+
print(findPaulResult)
90+
# >>> Result{1 total, docs: [Document {'id': 'user:3', ...
91+
# STEP_END
92+
# Tests for 'query1' step.
93+
# REMOVE_START
94+
assert str(findPaulResult) == (
95+
"Result{1 total, docs: [Document {'id': 'user:3', 'payload': None, "
96+
+ "'json': '{\"name\":\"Paul Zamir\",\"email\":"
97+
+ "\"[email protected]\",\"age\":35,\"city\":\"Tel Aviv\"}'}]}"
98+
)
99+
# REMOVE_END
100+
101+
# STEP_START query2
102+
citiesResult = r.ft("idx:users").search(
103+
Query("Paul").return_field("$.city", as_field="city")
104+
).docs
105+
106+
print(citiesResult)
107+
# >>> [Document {'id': 'user:1', 'payload': None, ...
108+
# STEP_END
109+
# Tests for 'query2' step.
110+
# REMOVE_START
111+
citiesResult.sort(key=lambda doc: doc['id'])
112+
113+
assert str(citiesResult) == (
114+
"[Document {'id': 'user:1', 'payload': None, 'city': 'London'}, "
115+
+ "Document {'id': 'user:3', 'payload': None, 'city': 'Tel Aviv'}]"
116+
)
117+
# REMOVE_END
118+
119+
# STEP_START query3
120+
req = aggregations.AggregateRequest("*").group_by(
121+
'@city', reducers.count().alias('count')
122+
)
123+
124+
aggResult = r.ft("idx:users").aggregate(req).rows
125+
print(aggResult)
126+
# >>> [['city', 'London', 'count', '1'], ['city', 'Tel Aviv', 'count', '2']]
127+
# STEP_END
128+
# Tests for 'query3' step.
129+
# REMOVE_START
130+
aggResult.sort(key=lambda row: row[1])
131+
132+
assert str(aggResult) == (
133+
"[['city', 'London', 'count', '1'], ['city', 'Tel Aviv', 'count', '2']]"
134+
)
135+
# REMOVE_END
136+
137+
r.close()

doctests/query_agg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from redis.commands.search import Search
77
from redis.commands.search.aggregation import AggregateRequest
88
from redis.commands.search.field import NumericField, TagField
9-
from redis.commands.search.indexDefinition import IndexDefinition, IndexType
9+
from redis.commands.search.index_definition import IndexDefinition, IndexType
1010
import redis.commands.search.reducers as reducers
1111

1212
r = redis.Redis(decode_responses=True)

doctests/query_combined.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import warnings
77
from redis.commands.json.path import Path
88
from redis.commands.search.field import NumericField, TagField, TextField, VectorField
9-
from redis.commands.search.indexDefinition import IndexDefinition, IndexType
9+
from redis.commands.search.index_definition import IndexDefinition, IndexType
1010
from redis.commands.search.query import Query
1111
from sentence_transformers import SentenceTransformer
1212

doctests/query_em.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import redis
55
from redis.commands.json.path import Path
66
from redis.commands.search.field import TextField, NumericField, TagField
7-
from redis.commands.search.indexDefinition import IndexDefinition, IndexType
7+
from redis.commands.search.index_definition import IndexDefinition, IndexType
88
from redis.commands.search.query import NumericFilter, Query
99

1010
r = redis.Redis(decode_responses=True)

doctests/query_ft.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import redis
66
from redis.commands.json.path import Path
77
from redis.commands.search.field import TextField, NumericField, TagField
8-
from redis.commands.search.indexDefinition import IndexDefinition, IndexType
8+
from redis.commands.search.index_definition import IndexDefinition, IndexType
99
from redis.commands.search.query import NumericFilter, Query
1010

1111
r = redis.Redis(decode_responses=True)

doctests/query_geo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import redis
66
from redis.commands.json.path import Path
77
from redis.commands.search.field import GeoField, GeoShapeField
8-
from redis.commands.search.indexDefinition import IndexDefinition, IndexType
8+
from redis.commands.search.index_definition import IndexDefinition, IndexType
99
from redis.commands.search.query import Query
1010

1111
r = redis.Redis(decode_responses=True)

doctests/query_range.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import redis
66
from redis.commands.json.path import Path
77
from redis.commands.search.field import TextField, NumericField, TagField
8-
from redis.commands.search.indexDefinition import IndexDefinition, IndexType
8+
from redis.commands.search.index_definition import IndexDefinition, IndexType
99
from redis.commands.search.query import NumericFilter, Query
1010

1111
r = redis.Redis(decode_responses=True)

doctests/search_quickstart.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import redis.commands.search.reducers as reducers
1111
from redis.commands.json.path import Path
1212
from redis.commands.search.field import NumericField, TagField, TextField
13-
from redis.commands.search.indexDefinition import IndexDefinition, IndexType
13+
from redis.commands.search.index_definition import IndexDefinition, IndexType
1414
from redis.commands.search.query import Query
1515

1616
# HIDE_END

doctests/search_vss.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
TextField,
2121
VectorField,
2222
)
23-
from redis.commands.search.indexDefinition import IndexDefinition, IndexType
23+
from redis.commands.search.index_definition import IndexDefinition, IndexType
2424
from redis.commands.search.query import Query
2525
from sentence_transformers import SentenceTransformer
2626

0 commit comments

Comments
 (0)