diff options
author | Itamar Haber <itamar@redis.com> | 2023-01-22 21:21:22 +0200 |
---|---|---|
committer | Itamar Haber <itamar@redis.com> | 2023-01-22 21:21:22 +0200 |
commit | 4375f271a067908d513c25750e13bdefb2219b42 (patch) | |
tree | 91a3800d0c55e1b83c7a5d958fac187ac2fbc3a4 | |
parent | 18560b43ed759f4ace7ac16f4a3dd53ab2841969 (diff) | |
download | redis-py-4375f271a067908d513c25750e13bdefb2219b42.tar.gz |
Initial example
-rw-r--r-- | docs/examples/json_search/example.json | 11 | ||||
-rw-r--r-- | docs/examples/json_search/main.py | 47 |
2 files changed, 58 insertions, 0 deletions
diff --git a/docs/examples/json_search/example.json b/docs/examples/json_search/example.json new file mode 100644 index 0000000..d26848e --- /dev/null +++ b/docs/examples/json_search/example.json @@ -0,0 +1,11 @@ +{ + "id": "json_search", + "file": "main.py", + "highlight": [ + "37-41", + "43-45", + "47" + ], + "hide": [], + "collapse": [] +} diff --git a/docs/examples/json_search/main.py b/docs/examples/json_search/main.py new file mode 100644 index 0000000..44551a9 --- /dev/null +++ b/docs/examples/json_search/main.py @@ -0,0 +1,47 @@ +import redis +from redis.commands.json.path import Path +from redis.commands.search.field import TextField, NumericField, TagField +from redis.commands.search.indexDefinition import IndexDefinition, IndexType + + +r = redis.Redis(host='localhost', port=6379) +user1 = { + "user":{ + "name": "Paul John", + "email": "paul.john@example.com", + "age": 42, + "city": "London" + } +} +user2 = { + "user":{ + "name": "Eden Zamir", + "email": "eden.zamir@example.com", + "age": 29, + "city": "Tel Aviv" + } +} +user3 = { + "user":{ + "name": "Paul Zamir", + "email": "paul.zamir@example.com", + "age": 35, + "city": "Tel Aviv" + } +} + +r.json().set("user:1", Path.root_path(), user1) +r.json().set("user:2", Path.root_path(), user2) +r.json().set("user:3", Path.root_path(), user3) + +schema = ( + TextField("$.user.name", as_name="name"), + TagField("$.user.city", as_name="city"), + NumericField("$.user.age", as_name="age") +) + +r.ft().create_index(schema, definition= + IndexDefinition(prefix=["user:"], index_type=IndexType.JSON) +) + +r.ft().search("Paul") |