From 4f93c7877b2e248194fadf23034f8c623c5bfd9e Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Mon, 7 Oct 2024 16:50:50 +0100 Subject: [PATCH 1/4] DOC-4345 added testable JSON search examples for home page --- doctests/home_json.py | 138 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 doctests/home_json.py diff --git a/doctests/home_json.py b/doctests/home_json.py new file mode 100644 index 0000000000..16565a4b40 --- /dev/null +++ b/doctests/home_json.py @@ -0,0 +1,138 @@ +# EXAMPLE: py_home_json +""" +JSON examples from redis-py "home" page" + https://redis.io/docs/latest/develop/connect/clients/python/redis-py/#example-indexing-and-querying-json-documents +""" + +# STEP_START import +import redis +from redis.commands.json.path import Path +import redis.commands.search.aggregation as aggregations +import redis.commands.search.reducers as reducers +from redis.commands.search.field import TextField, NumericField, TagField +from redis.commands.search.indexDefinition import IndexDefinition, IndexType +from redis.commands.search.query import Query +import redis.exceptions +# STEP_END + +# STEP_START connect +r = redis.Redis(decode_responses=True) +# STEP_END + +# REMOVE_START +try: + r.ft("idx:users").dropindex(True) +except redis.exceptions.ResponseError: + pass +r.delete("user:1", "user:2", "user:3") +# REMOVE_END + +# STEP_START create_data +user1 = { + "name": "Paul John", + "email": "paul.john@example.com", + "age": 42, + "city": "London" +} +user2 = { + "name": "Eden Zamir", + "email": "eden.zamir@example.com", + "age": 29, + "city": "Tel Aviv" +} +user3 = { + "name": "Paul Zamir", + "email": "paul.zamir@example.com", + "age": 35, + "city": "Tel Aviv" +} +# STEP_END + +# STEP_START make_index +schema = ( + TextField("$.name", as_name="name"), + TagField("$.city", as_name="city"), + NumericField("$.age", as_name="age") +) + +indexCreated = r.ft("idx:users").create_index( + schema, + definition=IndexDefinition( + prefix=["user:"], index_type=IndexType.JSON + ) +) +# STEP_END + +# Tests for 'make_index' step. +# REMOVE_START +assert indexCreated +# REMOVE_END + + +# STEP_START add_data +user1Set = r.json().set("user:1", Path.root_path(), user1) +user2Set = r.json().set("user:2", Path.root_path(), user2) +user3Set = r.json().set("user:3", Path.root_path(), user3) +# STEP_END + +# Tests for 'add_data' step. +# REMOVE_START +assert user1Set +assert user2Set +assert user3Set +# REMOVE_END + + +# STEP_START query1 +findPaulResult = r.ft("idx:users").search( + Query("Paul @age:[30 40]") +) + +print(findPaulResult) +# >>> Result{1 total, docs: [Document {'id': 'user:3', ... +# STEP_END + +# Tests for 'query1' step. +# REMOVE_START +assert str(findPaulResult) == ( + "Result{1 total, docs: [Document {'id': 'user:3', 'payload': None, " + + "'json': '{\"name\":\"Paul Zamir\",\"email\":" + + "\"paul.zamir@example.com\",\"age\":35,\"city\":\"Tel Aviv\"}'}]}" +) +# REMOVE_END + + +# STEP_START query2 +citiesResult = r.ft("idx:users").search( + Query("Paul").return_field("$.city", as_field="city") +).docs + +print(citiesResult) +# >>> [Document {'id': 'user:1', 'payload': None, ... +# STEP_END + +# Tests for 'query2' step. +# REMOVE_START +assert str(citiesResult) == ( + "[Document {'id': 'user:1', 'payload': None, 'city': 'London'}, " + + "Document {'id': 'user:3', 'payload': None, 'city': 'Tel Aviv'}]" +) +# REMOVE_END + + +# STEP_START query3 +req = aggregations.AggregateRequest("*").group_by( + '@city', reducers.count().alias('count') +) + +aggResult = r.ft("idx:users").aggregate(req).rows +print(aggResult) +# >>> [['city', 'London', 'count', '1'], ['city', 'Tel Aviv', 'count', '2']] +# STEP_END + +# Tests for 'query3' step. +# REMOVE_START +assert str(aggResult) == ( + "[['city', 'London', 'count', '1'], ['city', 'Tel Aviv', 'count', '2']]" +) +# REMOVE_END From 78f96f472a2a66c2af9bdda8f0f11b95d216be11 Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Tue, 5 Nov 2024 10:14:43 +0000 Subject: [PATCH 2/4] DOC-4345 avoid possible non-deterministic results in tests --- doctests/home_json.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/doctests/home_json.py b/doctests/home_json.py index 16565a4b40..bb8acd435c 100644 --- a/doctests/home_json.py +++ b/doctests/home_json.py @@ -24,6 +24,7 @@ r.ft("idx:users").dropindex(True) except redis.exceptions.ResponseError: pass + r.delete("user:1", "user:2", "user:3") # REMOVE_END @@ -34,12 +35,14 @@ "age": 42, "city": "London" } + user2 = { "name": "Eden Zamir", "email": "eden.zamir@example.com", "age": 29, "city": "Tel Aviv" } + user3 = { "name": "Paul Zamir", "email": "paul.zamir@example.com", @@ -113,6 +116,8 @@ # Tests for 'query2' step. # REMOVE_START +citiesResult.sort(key=lambda doc: doc['id']) + assert str(citiesResult) == ( "[Document {'id': 'user:1', 'payload': None, 'city': 'London'}, " + "Document {'id': 'user:3', 'payload': None, 'city': 'Tel Aviv'}]" @@ -132,6 +137,8 @@ # Tests for 'query3' step. # REMOVE_START +aggResult.sort(key=lambda row: row[1]) + assert str(aggResult) == ( "[['city', 'London', 'count', '1'], ['city', 'Tel Aviv', 'count', '2']]" ) From c639228ce1c8521eb50ff9ddf6dc839dacce2b30 Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Tue, 5 Nov 2024 11:38:00 +0000 Subject: [PATCH 3/4] DOC-4345 close connection at end of example --- doctests/home_json.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doctests/home_json.py b/doctests/home_json.py index bb8acd435c..ffdcb9acb9 100644 --- a/doctests/home_json.py +++ b/doctests/home_json.py @@ -143,3 +143,5 @@ "[['city', 'London', 'count', '1'], ['city', 'Tel Aviv', 'count', '2']]" ) # REMOVE_END + +r.close() From 89fe21f9a48a5f255b294faad0e5cff123c0fc26 Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Thu, 7 Nov 2024 15:33:49 +0000 Subject: [PATCH 4/4] DOC-4345 remove unnecessary blank lines --- doctests/home_json.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/doctests/home_json.py b/doctests/home_json.py index ffdcb9acb9..922c83d2fe 100644 --- a/doctests/home_json.py +++ b/doctests/home_json.py @@ -27,7 +27,6 @@ r.delete("user:1", "user:2", "user:3") # REMOVE_END - # STEP_START create_data user1 = { "name": "Paul John", @@ -65,19 +64,16 @@ ) ) # STEP_END - # Tests for 'make_index' step. # REMOVE_START assert indexCreated # REMOVE_END - # STEP_START add_data user1Set = r.json().set("user:1", Path.root_path(), user1) user2Set = r.json().set("user:2", Path.root_path(), user2) user3Set = r.json().set("user:3", Path.root_path(), user3) # STEP_END - # Tests for 'add_data' step. # REMOVE_START assert user1Set @@ -85,7 +81,6 @@ assert user3Set # REMOVE_END - # STEP_START query1 findPaulResult = r.ft("idx:users").search( Query("Paul @age:[30 40]") @@ -94,7 +89,6 @@ print(findPaulResult) # >>> Result{1 total, docs: [Document {'id': 'user:3', ... # STEP_END - # Tests for 'query1' step. # REMOVE_START assert str(findPaulResult) == ( @@ -104,7 +98,6 @@ ) # REMOVE_END - # STEP_START query2 citiesResult = r.ft("idx:users").search( Query("Paul").return_field("$.city", as_field="city") @@ -113,7 +106,6 @@ print(citiesResult) # >>> [Document {'id': 'user:1', 'payload': None, ... # STEP_END - # Tests for 'query2' step. # REMOVE_START citiesResult.sort(key=lambda doc: doc['id']) @@ -124,7 +116,6 @@ ) # REMOVE_END - # STEP_START query3 req = aggregations.AggregateRequest("*").group_by( '@city', reducers.count().alias('count') @@ -134,7 +125,6 @@ print(aggResult) # >>> [['city', 'London', 'count', '1'], ['city', 'Tel Aviv', 'count', '2']] # STEP_END - # Tests for 'query3' step. # REMOVE_START aggResult.sort(key=lambda row: row[1])