@@ -9,39 +9,58 @@ 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
1515weight : 2
1616---
1717
1818This 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 ` Jedis ` installed.
25+ ## Initialize
2426
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).
2636
2737{{< clients-example java_home_json import >}}
2838{{< /clients-example >}}
2939
30- Connect to the database:
31-
32- {{< clients-example java_home_json connect >}}
33- {{< /clients-example >}}
40+ ## Create data
3441
3542Create some test data to add to the database:
3643
3744{{< clients-example java_home_json create_data >}}
3845{{< /clients-example >}}
3946
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+
4057Create 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/" >}}).
4158
4259{{< clients-example java_home_json make_index >}}
4360{{< /clients-example >}}
4461
62+ ## Add the data
63+
4564Add the three sets of user data to the database as
4665[ JSON] ({{< relref "/develop/data-types/json" >}}) objects.
4766If you use keys with the ` user: ` prefix then Redis will index the
@@ -50,6 +69,8 @@ objects automatically as you add them:
5069{{< clients-example java_home_json add_data >}}
5170{{< /clients-example >}}
5271
72+ ## Query the data
73+
5374You can now use the index to search the JSON objects. The
5475[ query] ({{< relref "/develop/interact/search-and-query/query" >}})
5576below searches for objects that have the text "Paul" in any field
@@ -70,5 +91,35 @@ to count all users in each city.
7091{{< clients-example java_home_json query3 >}}
7192{{< /clients-example >}}
7293
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+
73124See the [ Redis query engine] ({{< relref "/develop/interact/search-and-query" >}}) docs
74125for a full description of all query features with examples.
0 commit comments