Skip to content

Commit 2681025

Browse files
DOC-5111 small fixes
1 parent 9127aac commit 2681025

File tree

1 file changed

+37
-18
lines changed

1 file changed

+37
-18
lines changed

content/develop/clients/go/queryjson.md

Lines changed: 37 additions & 18 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/rc" >}})
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
@@ -123,8 +142,8 @@ need to specify some slightly different options.
123142

124143
When you create the schema for a hash index, you don't need to
125144
add aliases for the fields, since you use the basic names to access
126-
the fields anyway. Also, you must use `HASH` for the `IndexType`
127-
when you create the index. The code below shows these changes with
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
128147
a new index called `hash-idx:users`, which is otherwise the same as
129148
the `idx:users` index used for JSON documents in the previous examples.
130149

@@ -142,8 +161,8 @@ hash or JSON:
142161
The query commands work the same here for hash as they do for JSON (but
143162
the name of the hash index is different). The format of the result is
144163
also almost the same except that the fields are returned directly in the
145-
result `Document` object map instead of in an enclosing `json` string
146-
under the key "$":
164+
`Document` object map of the result (for JSON, the fields are all enclosed
165+
in a string under the key "$"):
147166

148167
{{< clients-example go_home_json query1_hash >}}
149168
{{< /clients-example >}}

0 commit comments

Comments
 (0)