Skip to content

Commit 52a8c59

Browse files
Merge pull request #1452 from redis/DOC-5111-go-hash-query-example
DOC-5111 Go hash query example
2 parents 9b8f7eb + 075236f commit 52a8c59

File tree

1 file changed

+68
-15
lines changed

1 file changed

+68
-15
lines changed

content/develop/clients/go/queryjson.md

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,45 @@ 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: 2
1616
---
17+
1718
This example shows how to create a
1819
[search index]({{< relref "/develop/interact/search-and-query/indexing" >}})
19-
for [JSON]({{< relref "/develop/data-types/json" >}}) data and
20-
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.
24+
25+
## Initialize
2126

22-
Make sure that you have Redis Community Edition and `go-redis` installed.
27+
Make sure that you have [Redis Community Edition]({{< relref "/operate/oss_and_stack/" >}})
28+
or another Redis server available. Also install the
29+
[`go-redis`]({{< relref "/develop/clients/go" >}}) client library if you
30+
haven't already done so.
2331

24-
Start by importing dependencies:
32+
Add the following dependencies:
2533

2634
{{< clients-example go_home_json import >}}
2735
{{< /clients-example >}}
2836

29-
Connect to the database:
37+
## Create data
38+
39+
Create some test data to add to your database. The example data shown
40+
below is compatible with both JSON and hash objects.
41+
42+
{{< clients-example go_home_json create_data >}}
43+
{{< /clients-example >}}
44+
45+
## Add the index
46+
47+
Connect to your Redis database. The code below shows the most
48+
basic connection but see
49+
[Connect to the server]({{< relref "/develop/clients/go/connect" >}})
50+
to learn more about the available connection options.
3051

3152
{{< clients-example go_home_json connect >}}
3253
{{< /clients-example >}}
@@ -60,12 +81,6 @@ val1 := client.FTSearchWithArgs(
6081
```
6182
{{< /note >}}
6283

63-
64-
Create some test data to add to the database:
65-
66-
{{< clients-example go_home_json create_data >}}
67-
{{< /clients-example >}}
68-
6984
Use the code below to create a search index. The `FTCreateOptions` parameter enables
7085
indexing only for JSON objects where the key has a `user:` prefix.
7186
The
@@ -81,6 +96,8 @@ expression, instead of typing it in full:
8196
{{< clients-example go_home_json make_index >}}
8297
{{< /clients-example >}}
8398

99+
## Add the data
100+
84101
Add the three sets of user data to the database as
85102
[JSON]({{< relref "/develop/data-types/json" >}}) objects.
86103
If you use keys with the `user:` prefix then Redis will index the
@@ -89,6 +106,8 @@ objects automatically as you add them:
89106
{{< clients-example go_home_json add_data >}}
90107
{{< /clients-example >}}
91108

109+
## Query the data
110+
92111
You can now use the index to search the JSON objects. The
93112
[query]({{< relref "/develop/interact/search-and-query/query" >}})
94113
below searches for objects that have the text "Paul" in any field
@@ -116,5 +135,39 @@ to count all users in each city.
116135
{{< clients-example go_home_json query3 >}}
117136
{{< /clients-example >}}
118137

138+
## Differences with hash documents
139+
140+
Indexing for hash documents is very similar to JSON indexing but you
141+
need to specify some slightly different options.
142+
143+
When you create the schema for a hash index, you don't need to
144+
add aliases for the fields, since you use the basic names to access
145+
the fields anyway. Also, you must set `OnHash` to `true` in the `FTCreateOptions`
146+
object when you create the index. The code below shows these changes with
147+
a new index called `hash-idx:users`, which is otherwise the same as
148+
the `idx:users` index used for JSON documents in the previous examples.
149+
150+
{{< clients-example go_home_json make_hash_index >}}
151+
{{< /clients-example >}}
152+
153+
You use [`HSet()`]({{< relref "/commands/hset" >}}) to add the hash
154+
documents instead of [`JSONSet()`]({{< relref "/commands/json.set" >}}),
155+
but the same flat `userX` maps work equally well with either
156+
hash or JSON:
157+
158+
{{< clients-example go_home_json add_hash_data >}}
159+
{{< /clients-example >}}
160+
161+
The query commands work the same here for hash as they do for JSON (but
162+
the name of the hash index is different). The format of the result is
163+
almost the same except that the fields are returned directly in the
164+
`Document` object map of the result (for JSON, the fields are all enclosed
165+
in a string under the key "$"):
166+
167+
{{< clients-example go_home_json query1_hash >}}
168+
{{< /clients-example >}}
169+
170+
## More information
171+
119172
See the [Redis query engine]({{< relref "/develop/interact/search-and-query" >}}) docs
120-
for a full description of all query features with examples.
173+
for a full description of all query features with examples.

0 commit comments

Comments
 (0)