|
| 1 | +// EXAMPLE: go_home_json |
| 2 | +// HIDE_START |
| 3 | +package example_commands_test |
| 4 | + |
| 5 | +// HIDE_END |
| 6 | +// STEP_START import |
| 7 | +import ( |
| 8 | +"context" |
| 9 | +"fmt" |
| 10 | +"sort" |
| 11 | + |
| 12 | +"github.com/redis/go-redis/v9" |
| 13 | +) |
| 14 | + |
| 15 | +// STEP_END |
| 16 | + |
| 17 | +func ExampleClient_search_json() { |
| 18 | +// STEP_START connect |
| 19 | +ctx := context.Background() |
| 20 | + |
| 21 | +rdb := redis.NewClient(&redis.Options{ |
| 22 | +Addr: "localhost:6379", |
| 23 | +Password: "", // no password docs |
| 24 | +DB: 0, // use default DB |
| 25 | +Protocol: 2, |
| 26 | +}) |
| 27 | +// STEP_END |
| 28 | +// REMOVE_START |
| 29 | +rdb.Del(ctx, "user:1", "user:2", "user:3") |
| 30 | +rdb.FTDropIndex(ctx, "idx:users") |
| 31 | +// REMOVE_END |
| 32 | + |
| 33 | +// STEP_START create_data |
| 34 | +user1 := map[string]interface{}{ |
| 35 | +"name": "Paul John", |
| 36 | +"email": "paul.john@example.com", |
| 37 | +"age": 42, |
| 38 | +"city": "London", |
| 39 | +} |
| 40 | + |
| 41 | +user2 := map[string]interface{}{ |
| 42 | +"name": "Eden Zamir", |
| 43 | +"email": "eden.zamir@example.com", |
| 44 | +"age": 29, |
| 45 | +"city": "Tel Aviv", |
| 46 | +} |
| 47 | + |
| 48 | +user3 := map[string]interface{}{ |
| 49 | +"name": "Paul Zamir", |
| 50 | +"email": "paul.zamir@example.com", |
| 51 | +"age": 35, |
| 52 | +"city": "Tel Aviv", |
| 53 | +} |
| 54 | +// STEP_END |
| 55 | + |
| 56 | +// STEP_START make_index |
| 57 | +_, err := rdb.FTCreate( |
| 58 | +ctx, |
| 59 | +"idx:users", |
| 60 | +// Options: |
| 61 | +&redis.FTCreateOptions{ |
| 62 | +OnJSON: true, |
| 63 | +Prefix: []interface{}{"user:"}, |
| 64 | +}, |
| 65 | +// Index schema fields: |
| 66 | +&redis.FieldSchema{ |
| 67 | +FieldName: "$.name", |
| 68 | +As: "name", |
| 69 | +FieldType: redis.SearchFieldTypeText, |
| 70 | +}, |
| 71 | +&redis.FieldSchema{ |
| 72 | +FieldName: "$.city", |
| 73 | +As: "city", |
| 74 | +FieldType: redis.SearchFieldTypeTag, |
| 75 | +}, |
| 76 | +&redis.FieldSchema{ |
| 77 | +FieldName: "$.age", |
| 78 | +As: "age", |
| 79 | +FieldType: redis.SearchFieldTypeNumeric, |
| 80 | +}, |
| 81 | +).Result() |
| 82 | + |
| 83 | +if err != nil { |
| 84 | +panic(err) |
| 85 | +} |
| 86 | +// STEP_END |
| 87 | + |
| 88 | +// STEP_START add_data |
| 89 | +_, err = rdb.JSONSet(ctx, "user:1", "$", user1).Result() |
| 90 | + |
| 91 | +if err != nil { |
| 92 | +panic(err) |
| 93 | +} |
| 94 | + |
| 95 | +_, err = rdb.JSONSet(ctx, "user:2", "$", user2).Result() |
| 96 | + |
| 97 | +if err != nil { |
| 98 | +panic(err) |
| 99 | +} |
| 100 | + |
| 101 | +_, err = rdb.JSONSet(ctx, "user:3", "$", user3).Result() |
| 102 | + |
| 103 | +if err != nil { |
| 104 | +panic(err) |
| 105 | +} |
| 106 | +// STEP_END |
| 107 | + |
| 108 | +// STEP_START query1 |
| 109 | +findPaulResult, err := rdb.FTSearch( |
| 110 | +ctx, |
| 111 | +"idx:users", |
| 112 | +"Paul @age:[30 40]", |
| 113 | +).Result() |
| 114 | + |
| 115 | +if err != nil { |
| 116 | +panic(err) |
| 117 | +} |
| 118 | + |
| 119 | +fmt.Println(findPaulResult) |
| 120 | +// >>> {1 [{user:3 <nil> <nil> <nil> map[$:{"age":35,"city":"Tel Aviv"... |
| 121 | +// STEP_END |
| 122 | + |
| 123 | +// STEP_START query2 |
| 124 | +citiesResult, err := rdb.FTSearchWithArgs( |
| 125 | +ctx, |
| 126 | +"idx:users", |
| 127 | +"Paul", |
| 128 | +&redis.FTSearchOptions{ |
| 129 | +Return: []redis.FTSearchReturn{ |
| 130 | +{ |
| 131 | +FieldName: "$.city", |
| 132 | +As: "city", |
| 133 | +}, |
| 134 | +}, |
| 135 | +}, |
| 136 | +).Result() |
| 137 | + |
| 138 | +if err != nil { |
| 139 | +panic(err) |
| 140 | +} |
| 141 | + |
| 142 | +sort.Slice(citiesResult.Docs, func(i, j int) bool { |
| 143 | +return citiesResult.Docs[i].Fields["city"] < citiesResult.Docs[j].Fields["city"] |
| 144 | +}) |
| 145 | + |
| 146 | +for _, result := range citiesResult.Docs { |
| 147 | +fmt.Println(result.Fields["city"]) |
| 148 | +} |
| 149 | +// >>> London |
| 150 | +// >>> Tel Aviv |
| 151 | +// STEP_END |
| 152 | + |
| 153 | +// STEP_START query3 |
| 154 | +aggOptions := redis.FTAggregateOptions{ |
| 155 | +GroupBy: []redis.FTAggregateGroupBy{ |
| 156 | +{ |
| 157 | +Fields: []interface{}{"@city"}, |
| 158 | +Reduce: []redis.FTAggregateReducer{ |
| 159 | +{ |
| 160 | +Reducer: redis.SearchCount, |
| 161 | +As: "count", |
| 162 | +}, |
| 163 | +}, |
| 164 | +}, |
| 165 | +}, |
| 166 | +} |
| 167 | + |
| 168 | +aggResult, err := rdb.FTAggregateWithArgs( |
| 169 | +ctx, |
| 170 | +"idx:users", |
| 171 | +"*", |
| 172 | +&aggOptions, |
| 173 | +).Result() |
| 174 | + |
| 175 | +if err != nil { |
| 176 | +panic(err) |
| 177 | +} |
| 178 | + |
| 179 | +sort.Slice(aggResult.Rows, func(i, j int) bool { |
| 180 | +return aggResult.Rows[i].Fields["city"].(string) < |
| 181 | +aggResult.Rows[j].Fields["city"].(string) |
| 182 | +}) |
| 183 | + |
| 184 | +for _, row := range aggResult.Rows { |
| 185 | +fmt.Printf("%v - %v\n", |
| 186 | +row.Fields["city"], row.Fields["count"], |
| 187 | +) |
| 188 | +} |
| 189 | +// >>> City: London - 1 |
| 190 | +// >>> City: Tel Aviv - 2 |
| 191 | +// STEP_END |
| 192 | + |
| 193 | +// Output: |
| 194 | +// {1 [{user:3 <nil> <nil> <nil> map[$:{"age":35,"city":"Tel Aviv","email":"paul.zamir@example.com","name":"Paul Zamir"}]}]} |
| 195 | +// London |
| 196 | +// Tel Aviv |
| 197 | +// London - 1 |
| 198 | +// Tel Aviv - 2 |
| 199 | +} |
0 commit comments