@@ -9,39 +9,58 @@ categories:
9
9
- oss
10
10
- kubernetes
11
11
- 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
15
15
weight : 2
16
16
---
17
17
18
18
This example shows how to create a
19
19
[ 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.
22
24
23
- Make sure that you have Redis Community Edition and ` Jedis ` installed.
25
+ ## Initialize
24
26
25
- Start by importing 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
+ [ Jedis] ({{< relref "/develop/clients/jedis" >}}) 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 ` and ` JSONObject ` classes, which are specific to JSON (see
34
+ [ Path] ({{< relref "/develop/data-types/json/path" >}}) for a description of the
35
+ JSON path syntax).
26
36
27
37
{{< clients-example java_home_json import >}}
28
38
{{< /clients-example >}}
29
39
30
- Connect to the database:
31
-
32
- {{< clients-example java_home_json connect >}}
33
- {{< /clients-example >}}
40
+ ## Create data
34
41
35
42
Create some test data to add to the database:
36
43
37
44
{{< clients-example java_home_json create_data >}}
38
45
{{< /clients-example >}}
39
46
47
+ ## Add the index
48
+
49
+ Connect to your Redis database. The code below shows the most
50
+ basic connection but see
51
+ [ Connect to the server] ({{< relref "/develop/clients/jedis/connect" >}})
52
+ to learn more about the available connection options.
53
+
54
+ {{< clients-example java_home_json connect >}}
55
+ {{< /clients-example >}}
56
+
40
57
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
58
42
59
{{< clients-example java_home_json make_index >}}
43
60
{{< /clients-example >}}
44
61
62
+ ## Add the data
63
+
45
64
Add the three sets of user data to the database as
46
65
[ JSON] ({{< relref "/develop/data-types/json" >}}) objects.
47
66
If you use keys with the ` user: ` prefix then Redis will index the
@@ -50,6 +69,8 @@ objects automatically as you add them:
50
69
{{< clients-example java_home_json add_data >}}
51
70
{{< /clients-example >}}
52
71
72
+ ## Query the data
73
+
53
74
You can now use the index to search the JSON objects. The
54
75
[ query] ({{< relref "/develop/interact/search-and-query/query" >}})
55
76
below searches for objects that have the text "Paul" in any field
@@ -70,5 +91,35 @@ to count all users in each city.
70
91
{{< clients-example java_home_json query3 >}}
71
92
{{< /clients-example >}}
72
93
94
+ ## Differences with hash documents
95
+
96
+ Indexing for hash documents is very similar to JSON indexing but you
97
+ need to specify some slightly different options.
98
+
99
+ When you create the schema for a hash index, you don't need to
100
+ add aliases for the fields, since you use the basic names to access
101
+ the fields anyway. Also, you must use ` IndexDataType.HASH ` for the ` On() `
102
+ option of ` FTCreateParams ` when you create the index. The code below shows these
103
+ changes with a new index called ` hash-idx:users ` , which is otherwise the same as
104
+ the ` idx:users ` index used for JSON documents in the previous examples.
105
+
106
+ {{< clients-example java_home_json make_hash_index >}}
107
+ {{< /clients-example >}}
108
+
109
+ Use [ ` hset() ` ] ({{< relref "/commands/hset" >}}) to add the hash
110
+ documents instead of [ ` jsonSet() ` ] ({{< relref "/commands/json.set" >}}).
111
+
112
+ {{< clients-example java_home_json add_hash_data >}}
113
+ {{< /clients-example >}}
114
+
115
+ The query commands work the same here for hash as they do for JSON (but
116
+ the name of the hash index is different). The results are returned in
117
+ a ` List ` of ` Document ` objects, as with JSON:
118
+
119
+ {{< clients-example java_home_json query1_hash >}}
120
+ {{< /clients-example >}}
121
+
122
+ ## More information
123
+
73
124
See the [ Redis query engine] ({{< relref "/develop/interact/search-and-query" >}}) docs
74
125
for a full description of all query features with examples.
0 commit comments