Skip to content

Commit 3032fe1

Browse files
Merge pull request #880 from redis/DOC-4345-more-json-examples
DOC-4345 more JSON query examples
2 parents db6d1ca + 759ffcf commit 3032fe1

File tree

3 files changed

+110
-113
lines changed

3 files changed

+110
-113
lines changed

content/develop/clients/go/queryjson.md

Lines changed: 35 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -19,56 +19,22 @@ This example shows how to create a
1919
for [JSON]({{< relref "/develop/data-types/json" >}}) data and
2020
run queries against the index.
2121

22-
Make sure that you have Redis Stack and `NRedisStack` installed.
23-
24-
Start by connecting to the Redis server:
25-
26-
```go
27-
import (
28-
"context"
29-
"fmt"
30-
31-
"github.com/redis/go-redis/v9"
32-
)
33-
34-
func main() {
35-
ctx := context.Background()
36-
37-
rdb := redis.NewClient(&redis.Options{
38-
Addr: "localhost:6379",
39-
Password: "",
40-
DB: 0,
41-
Protocol: 2,
42-
})
43-
44-
// ...
45-
}
46-
```
47-
48-
Add some `map` objects to store in JSON format in the database:
49-
50-
```go
51-
user1 := map[string]interface{}{
52-
"name": "Paul John",
53-
"email": "paul.john@example.com",
54-
"age": 42,
55-
"city": "London",
56-
}
57-
58-
user2 := map[string]interface{}{
59-
"name": "Eden Zamir",
60-
"email": "eden.zamir@example.com",
61-
"age": 29,
62-
"city": "Tel Aviv",
63-
}
64-
65-
user3 := map[string]interface{}{
66-
"name": "Paul Zamir",
67-
"email": "paul.zamir@example.com",
68-
"age": 35,
69-
"city": "Tel Aviv",
70-
}
71-
```
22+
Make sure that you have Redis Stack and `go-redis` installed.
23+
24+
Start by importing dependencies:
25+
26+
{{< clients-example go_home_json import >}}
27+
{{< /clients-example >}}
28+
29+
Connect to the database:
30+
31+
{{< clients-example go_home_json connect >}}
32+
{{< /clients-example >}}
33+
34+
Create some test data to add to the database:
35+
36+
{{< clients-example go_home_json create_data >}}
37+
{{< /clients-example >}}
7238

7339
Use the code below to create a search index. The `FTCreateOptions` parameter enables
7440
indexing only for JSON objects where the key has a `user:` prefix.
@@ -82,79 +48,36 @@ to provide an alias for the JSON path expression. You can use
8248
the alias in queries as a short and intuitive way to refer to the
8349
expression, instead of typing it in full:
8450

85-
```go
86-
_, err := rdb.FTCreate(
87-
ctx,
88-
"idx:users",
89-
// Options:
90-
&redis.FTCreateOptions{
91-
OnJSON: true,
92-
Prefix: []interface{}{"user:"},
93-
},
94-
// Index schema fields:
95-
&redis.FieldSchema{
96-
FieldName: "$.name",
97-
As: "name",
98-
FieldType: redis.SearchFieldTypeText,
99-
},
100-
&redis.FieldSchema{
101-
FieldName: "$.city",
102-
As: "city",
103-
FieldType: redis.SearchFieldTypeTag,
104-
},
105-
&redis.FieldSchema{
106-
FieldName: "$.age",
107-
As: "age",
108-
FieldType: redis.SearchFieldTypeNumeric,
109-
},
110-
).Result()
111-
112-
if err != nil {
113-
panic(err)
114-
}
115-
```
51+
{{< clients-example go_home_json make_index >}}
52+
{{< /clients-example >}}
11653

11754
Add the three sets of user data to the database as
11855
[JSON]({{< relref "/develop/data-types/json" >}}) objects.
11956
If you use keys with the `user:` prefix then Redis will index the
12057
objects automatically as you add them:
12158

122-
```go
123-
_, err = rdb.JSONSet(ctx, "user:1", "$", user1).Result()
124-
125-
if err != nil {
126-
panic(err)
127-
}
128-
129-
_, err = rdb.JSONSet(ctx, "user:2", "$", user2).Result()
130-
131-
if err != nil {
132-
panic(err)
133-
}
134-
135-
_, err = rdb.JSONSet(ctx, "user:3", "$", user3).Result()
136-
137-
if err != nil {
138-
panic(err)
139-
}
140-
```
59+
{{< clients-example go_home_json add_data >}}
60+
{{< /clients-example >}}
14161

14262
You can now use the index to search the JSON objects. The
14363
[query]({{< relref "/develop/interact/search-and-query/query" >}})
14464
below searches for objects that have the text "Paul" in any field
14565
and have an `age` value in the range 30 to 40:
14666

147-
```go
148-
searchResult, err := rdb.FTSearch(
149-
ctx,
150-
"idx:users",
151-
"Paul @age:[30 40]",
152-
).Result()
67+
{{< clients-example go_home_json query1 >}}
68+
{{< /clients-example >}}
69+
70+
Specify query options to return only the `city` field:
71+
72+
{{< clients-example go_home_json query2 >}}
73+
{{< /clients-example >}}
74+
75+
Use an
76+
[aggregation query]({{< relref "/develop/interact/search-and-query/query/aggregation" >}})
77+
to count all users in each city.
15378

154-
if err != nil {
155-
panic(err)
156-
}
79+
{{< clients-example go_home_json query3 >}}
80+
{{< /clients-example >}}
15781

158-
fmt.Println(searchResult)
159-
// >>> {1 [{user:3 <nil> <nil> <nil> map[$:{"age":35,"city":"Tel Aviv"...
160-
```
82+
See the [Redis query engine]({{< relref "/develop/interact/search-and-query" >}}) docs
83+
for a full description of all query features with examples.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
categories:
3+
- docs
4+
- develop
5+
- stack
6+
- oss
7+
- rs
8+
- rc
9+
- oss
10+
- kubernetes
11+
- clients
12+
description: Learn how to use the Redis query engine with JSON
13+
linkTitle: JSON query example
14+
title: Example - Index and query JSON documents
15+
weight: 2
16+
---
17+
18+
This example shows how to create a
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.
22+
23+
Make sure that you have Redis Stack and `Jedis` installed.
24+
25+
Start by importing dependencies:
26+
27+
{{< clients-example java_home_json import >}}
28+
{{< /clients-example >}}
29+
30+
Connect to the database:
31+
32+
{{< clients-example java_home_json connect >}}
33+
{{< /clients-example >}}
34+
35+
Create some test data to add to the database:
36+
37+
{{< clients-example java_home_json create_data >}}
38+
{{< /clients-example >}}
39+
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/" >}}).
41+
42+
{{< clients-example java_home_json make_index >}}
43+
{{< /clients-example >}}
44+
45+
Add the three sets of user data to the database as
46+
[JSON]({{< relref "/develop/data-types/json" >}}) objects.
47+
If you use keys with the `user:` prefix then Redis will index the
48+
objects automatically as you add them:
49+
50+
{{< clients-example java_home_json add_data >}}
51+
{{< /clients-example >}}
52+
53+
You can now use the index to search the JSON objects. The
54+
[query]({{< relref "/develop/interact/search-and-query/query" >}})
55+
below searches for objects that have the text "Paul" in any field
56+
and have an `age` value in the range 30 to 40:
57+
58+
{{< clients-example java_home_json query1 >}}
59+
{{< /clients-example >}}
60+
61+
Specify query options to return only the `city` field:
62+
63+
{{< clients-example java_home_json query2 >}}
64+
{{< /clients-example >}}
65+
66+
Use an
67+
[aggregation query]({{< relref "/develop/interact/search-and-query/query/aggregation" >}})
68+
to count all users in each city.
69+
70+
{{< clients-example java_home_json query3 >}}
71+
{{< /clients-example >}}
72+
73+
See the [Redis query engine]({{< relref "/develop/interact/search-and-query" >}}) docs
74+
for a full description of all query features with examples.

layouts/commands/list.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ <h1>Commands</h1>
5050
<option value="list" data-kind="core">List</option>
5151
<option value="pubsub" data-kind="core">Pub/Sub</option>
5252
<option value="scripting" data-kind="core">Scripting and functions</option>
53-
<option value="server" data-kind="core">Server managment</option>
53+
<option value="server" data-kind="core">Server management</option>
5454
<option value="set" data-kind="core">Set</option>
5555
<option value="sorted-set" data-kind="core">Sorted set</option>
5656
<option value="stream" data-kind="core">Stream</option>

0 commit comments

Comments
 (0)