Skip to content

Commit 2ada095

Browse files
Merge pull request #1454 from redis/DOC-5107-py-hash-query-example
DOC-5107 added Python hash search examples
2 parents 52a8c59 + 0a6de91 commit 2ada095

File tree

1 file changed

+69
-12
lines changed

1 file changed

+69
-12
lines changed

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

Lines changed: 69 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,61 @@ categories:
99
- oss
1010
- kubernetes
1111
- clients
12-
description: Learn how to use the Redis query engine with JSON
13-
linkTitle: Index and query JSON
14-
title: Example - Index and query JSON documents
12+
description: Learn how to use the Redis query engine with JSON and hash documents.
13+
linkTitle: Index and query documents
14+
title: Index and query documents
1515
weight: 30
1616
---
1717

1818
This example shows how to create a
1919
[search index]({{< relref "/develop/interact/search-and-query/indexing" >}})
20-
for [JSON]({{< relref "/develop/data-types/json" >}}) data and
21-
run queries against the index.
20+
for [JSON]({{< relref "/develop/data-types/json" >}}) documents and
21+
run queries against the index. It then goes on to show the slight differences
22+
in the equivalent code for [hash]({{< relref "/develop/data-types/hashes" >}})
23+
documents.
2224

23-
Make sure that you have Redis Community Edition and `redis-py` installed.
25+
## Initialize
2426

25-
Import dependencies:
27+
Make sure that you have [Redis Community Edition]({{< relref "/operate/oss_and_stack/" >}})
28+
or another Redis server available. Also install the
29+
[`redis-py`]({{< relref "/develop/clients/redis-py" >}}) client library if you
30+
haven't already done so.
31+
32+
Add the following dependencies. All of them are applicable to both JSON and hash,
33+
except for the `Path` class, which is specific to JSON (see
34+
[Path]({{< relref "/develop/data-types/json/path" >}}) for a description of the
35+
JSON path syntax).
2636

2737
{{< clients-example py_home_json import >}}
2838
{{< /clients-example >}}
2939

30-
Connect to your Redis database.
40+
## Create data
3141

32-
{{< clients-example py_home_json connect >}}
42+
Create some test data to add to your database. The example data shown
43+
below is compatible with both JSON and hash objects.
44+
45+
{{< clients-example py_home_json create_data >}}
3346
{{< /clients-example >}}
3447

35-
Create some test data to add to your database.
48+
## Add the index
3649

37-
{{< clients-example py_home_json create_data >}}
50+
Connect to your Redis database. The code below shows the most
51+
basic connection but see
52+
[Connect to the server]({{< relref "/develop/clients/redis-py/connect" >}})
53+
to learn more about the available connection options.
54+
55+
{{< clients-example py_home_json connect >}}
3856
{{< /clients-example >}}
3957

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/" >}}).
58+
Create an index for the JSON data. The code below specifies that only JSON documents with
59+
the key prefix `user:` are indexed. For more information, see
60+
[Query syntax]({{< relref "/develop/interact/search-and-query/query/" >}}).
4161

4262
{{< clients-example py_home_json make_index >}}
4363
{{< /clients-example >}}
4464

65+
## Add the data
66+
4567
Add the three sets of user data to the database as
4668
[JSON]({{< relref "/develop/data-types/json" >}}) objects.
4769
If you use keys with the `user:` prefix then Redis will index the
@@ -50,6 +72,8 @@ objects automatically as you add them:
5072
{{< clients-example py_home_json add_data >}}
5173
{{< /clients-example >}}
5274

75+
## Query the data
76+
5377
You can now use the index to search the JSON objects. The
5478
[query]({{< relref "/develop/interact/search-and-query/query" >}})
5579
below searches for objects that have the text "Paul" in any field
@@ -70,5 +94,38 @@ to count all users in each city.
7094
{{< clients-example py_home_json query3 >}}
7195
{{< /clients-example >}}
7296

97+
## Differences with hash documents
98+
99+
Indexing for hash documents is very similar to JSON indexing but you
100+
need to specify some slightly different options.
101+
102+
When you create the schema for a hash index, you don't need to
103+
add aliases for the fields, since you use the basic names to access
104+
the fields anyway. Also, you must use `HASH` for the `IndexType`
105+
when you create the index. The code below shows these changes with
106+
a new index called `hash-idx:users`, which is otherwise the same as
107+
the `idx:users` index used for JSON documents in the previous examples.
108+
109+
{{< clients-example py_home_json make_hash_index >}}
110+
{{< /clients-example >}}
111+
112+
You use [`hset()`]({{< relref "/commands/hset" >}}) to add the hash
113+
documents instead of [`json().set()`]({{< relref "/commands/json.set" >}}),
114+
but the same flat `userX` dictionaries work equally well with either
115+
hash or JSON:
116+
117+
{{< clients-example py_home_json add_hash_data >}}
118+
{{< /clients-example >}}
119+
120+
The query commands work the same here for hash as they do for JSON (but
121+
the name of the hash index is different). The format of the result is
122+
almost the same except that the fields are returned directly in the
123+
result `Document` object instead of in an enclosing `json` dictionary:
124+
125+
{{< clients-example py_home_json query1_hash >}}
126+
{{< /clients-example >}}
127+
128+
## More information
129+
73130
See the [Redis query engine]({{< relref "/develop/interact/search-and-query" >}}) docs
74131
for a full description of all query features with examples.

0 commit comments

Comments
 (0)