Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 35 additions & 112 deletions content/develop/clients/go/queryjson.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,56 +19,22 @@ This example shows how to create a
for [JSON]({{< relref "/develop/data-types/json" >}}) data and
run queries against the index.

Make sure that you have Redis Stack and `NRedisStack` installed.

Start by connecting to the Redis server:

```go
import (
"context"
"fmt"

"github.com/redis/go-redis/v9"
)

func main() {
ctx := context.Background()

rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
Protocol: 2,
})

// ...
}
```

Add some `map` objects to store in JSON format in the database:

```go
user1 := map[string]interface{}{
"name": "Paul John",
"email": "[email protected]",
"age": 42,
"city": "London",
}

user2 := map[string]interface{}{
"name": "Eden Zamir",
"email": "[email protected]",
"age": 29,
"city": "Tel Aviv",
}

user3 := map[string]interface{}{
"name": "Paul Zamir",
"email": "[email protected]",
"age": 35,
"city": "Tel Aviv",
}
```
Make sure that you have Redis Stack and `go-redis` installed.

Start by importing dependencies:

{{< clients-example go_home_json import >}}
{{< /clients-example >}}

Connect to the database:

{{< clients-example go_home_json connect >}}
{{< /clients-example >}}

Create some test data to add to the database:

{{< clients-example go_home_json create_data >}}
{{< /clients-example >}}

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

```go
_, err := rdb.FTCreate(
ctx,
"idx:users",
// Options:
&redis.FTCreateOptions{
OnJSON: true,
Prefix: []interface{}{"user:"},
},
// Index schema fields:
&redis.FieldSchema{
FieldName: "$.name",
As: "name",
FieldType: redis.SearchFieldTypeText,
},
&redis.FieldSchema{
FieldName: "$.city",
As: "city",
FieldType: redis.SearchFieldTypeTag,
},
&redis.FieldSchema{
FieldName: "$.age",
As: "age",
FieldType: redis.SearchFieldTypeNumeric,
},
).Result()

if err != nil {
panic(err)
}
```
{{< clients-example go_home_json make_index >}}
{{< /clients-example >}}

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

```go
_, err = rdb.JSONSet(ctx, "user:1", "$", user1).Result()

if err != nil {
panic(err)
}

_, err = rdb.JSONSet(ctx, "user:2", "$", user2).Result()

if err != nil {
panic(err)
}

_, err = rdb.JSONSet(ctx, "user:3", "$", user3).Result()

if err != nil {
panic(err)
}
```
{{< clients-example go_home_json add_data >}}
{{< /clients-example >}}

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

```go
searchResult, err := rdb.FTSearch(
ctx,
"idx:users",
"Paul @age:[30 40]",
).Result()
{{< clients-example go_home_json query1 >}}
{{< /clients-example >}}

Specify query options to return only the `city` field:

{{< clients-example go_home_json query2 >}}
{{< /clients-example >}}

Use an
[aggregation query]({{< relref "/develop/interact/search-and-query/query/aggregation" >}})
to count all users in each city.

if err != nil {
panic(err)
}
{{< clients-example go_home_json query3 >}}
{{< /clients-example >}}

fmt.Println(searchResult)
// >>> {1 [{user:3 <nil> <nil> <nil> map[$:{"age":35,"city":"Tel Aviv"...
```
See the [Redis query engine]({{< relref "/develop/interact/search-and-query" >}}) docs
for a full description of all query features with examples.
74 changes: 74 additions & 0 deletions content/develop/clients/jedis/queryjson.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
categories:
- docs
- develop
- stack
- oss
- rs
- rc
- oss
- kubernetes
- clients
description: Learn how to use the Redis query engine with JSON
linkTitle: JSON query example
title: Example - Index and query JSON documents
weight: 2
---

This example shows how to create a
[search index]({{< relref "/develop/interact/search-and-query/indexing" >}})
for [JSON]({{< relref "/develop/data-types/json" >}}) data and
run queries against the index.

Make sure that you have Redis Stack and `Jedis` installed.

Start by importing dependencies:

{{< clients-example java_home_json import >}}
{{< /clients-example >}}

Connect to the database:

{{< clients-example java_home_json connect >}}
{{< /clients-example >}}

Create some test data to add to the database:

{{< clients-example java_home_json create_data >}}
{{< /clients-example >}}

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/" >}}).

{{< clients-example java_home_json make_index >}}
{{< /clients-example >}}

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

{{< clients-example java_home_json add_data >}}
{{< /clients-example >}}

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

{{< clients-example java_home_json query1 >}}
{{< /clients-example >}}

Specify query options to return only the `city` field:

{{< clients-example java_home_json query2 >}}
{{< /clients-example >}}

Use an
[aggregation query]({{< relref "/develop/interact/search-and-query/query/aggregation" >}})
to count all users in each city.

{{< clients-example java_home_json query3 >}}
{{< /clients-example >}}

See the [Redis query engine]({{< relref "/develop/interact/search-and-query" >}}) docs
for a full description of all query features with examples.
2 changes: 1 addition & 1 deletion layouts/commands/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ <h1>Commands</h1>
<option value="list" data-kind="core">List</option>
<option value="pubsub" data-kind="core">Pub/Sub</option>
<option value="scripting" data-kind="core">Scripting and functions</option>
<option value="server" data-kind="core">Server managment</option>
<option value="server" data-kind="core">Server management</option>
<option value="set" data-kind="core">Set</option>
<option value="sorted-set" data-kind="core">Sorted set</option>
<option value="stream" data-kind="core">Stream</option>
Expand Down
Loading