@@ -221,54 +221,20 @@ fmt.Println("foo", val)
221221
222222## Example: Index and search JSON documents
223223
224- Start by connecting to the Redis server :
224+ Start by importing dependencies :
225225
226- ``` go
227- import (
228- " context"
229- " fmt"
230-
231- " github.com/redis/go-redis/v9"
232- )
233-
234- func main () {
235- ctx := context.Background ()
226+ {{< clients-example go_home_json import >}}
227+ {{< /clients-example >}}
236228
237- rdb := redis.NewClient (&redis.Options {
238- Addr: " localhost:6379" ,
239- Password: " " ,
240- DB: 0 ,
241- Protocol: 2 ,
242- })
229+ Connect to the Redis server:
243230
244- // ...
245- }
246- ```
231+ {{< clients-example go_home_json connect >}}
232+ {{< /clients-example >}}
247233
248234Add some ` map ` objects to store in JSON format in the database:
249235
250- ``` go
251- user1 := map [string ]interface {}{
252- " name" : " Paul John" ,
253- 254- " age" : 42 ,
255- " city" : " London" ,
256- }
257-
258- user2 := map [string ]interface {}{
259- " name" : " Eden Zamir" ,
260- 261- " age" : 29 ,
262- " city" : " Tel Aviv" ,
263- }
264-
265- user3 := map [string ]interface {}{
266- " name" : " Paul Zamir" ,
267- 268- " age" : 35 ,
269- " city" : " Tel Aviv" ,
270- }
271- ```
236+ {{< clients-example go_home_json create_data >}}
237+ {{< /clients-example >}}
272238
273239Use the code below to create a search index. The ` FTCreateOptions ` parameter enables
274240indexing only for JSON objects where the key has a ` user: ` prefix.
@@ -282,82 +248,34 @@ to provide an alias for the JSON path expression. You can use
282248the alias in queries as a short and intuitive way to refer to the
283249expression, instead of typing it in full:
284250
285- ``` go
286- _ , err := rdb.FTCreate (
287- ctx,
288- " idx:users" ,
289- // Options:
290- &redis.FTCreateOptions {
291- OnJSON : true ,
292- Prefix : []interface {}{" user:" },
293- },
294- // Index schema fields:
295- &redis.FieldSchema {
296- FieldName : " $.name" ,
297- As : " name" ,
298- FieldType : redis.SearchFieldTypeText ,
299- },
300- &redis.FieldSchema {
301- FieldName : " $.city" ,
302- As : " city" ,
303- FieldType : redis.SearchFieldTypeTag ,
304- },
305- &redis.FieldSchema {
306- FieldName : " $.age" ,
307- As : " age" ,
308- FieldType : redis.SearchFieldTypeNumeric ,
309- },
310- ).Result ()
311-
312- if err != nil {
313- panic (err)
314- }
315- ```
251+ {{< clients-example go_home_json make_index >}}
252+ {{< /clients-example >}}
316253
317254Add the three sets of user data to the database as
318255[ JSON] ({{< relref "/develop/data-types/json" >}}) objects.
319256If you use keys with the ` user: ` prefix then Redis will index the
320257objects automatically as you add them:
321258
322- ``` go
323- _, err = rdb.JSONSet (ctx, " user:1" , " $" , user1).Result ()
324-
325- if err != nil {
326- panic (err)
327- }
328-
329- _, err = rdb.JSONSet (ctx, " user:2" , " $" , user2).Result ()
330-
331- if err != nil {
332- panic (err)
333- }
334-
335- _, err = rdb.JSONSet (ctx, " user:3" , " $" , user3).Result ()
336-
337- if err != nil {
338- panic (err)
339- }
340- ```
259+ {{< clients-example go_home_json add_data >}}
260+ {{< /clients-example >}}
341261
342262You can now use the index to search the JSON objects. The
343263[ query] ({{< relref "/develop/interact/search-and-query/query" >}})
344264below searches for objects that have the text "Paul" in any field
345265and have an ` age ` value in the range 30 to 40:
346266
347- ``` go
348- searchResult , err := rdb.FTSearch (
349- ctx,
350- " idx:users" ,
351- " Paul @age:[30 40]" ,
352- ).Result ()
267+ {{< clients-example go_home_json query1 >}}
268+ {{< /clients-example >}}
353269
354- if err != nil {
355- panic (err)
356- }
270+ Return only the city field:
357271
358- fmt.Println (searchResult)
359- // >>> {1 [{user:3 <nil> <nil> <nil> map[$:{"age":35,"city":"Tel Aviv"...
360- ```
272+ {{< clients-example go_home_json query2 >}}
273+ {{< /clients-example >}}
274+
275+ Count the users in each city:
276+
277+ {{< clients-example go_home_json query3 >}}
278+ {{< /clients-example >}}
361279
362280## Example: Index and search hash documents
363281
0 commit comments