Skip to content

Commit 1b5ba6e

Browse files
DOC-4335 added missing examples using latest client updates
1 parent 5c44541 commit 1b5ba6e

File tree

1 file changed

+89
-5
lines changed

1 file changed

+89
-5
lines changed

doctests/query_agg_test.go

Lines changed: 89 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -266,25 +266,103 @@ func ExampleClient_query_agg() {
266266
// STEP_END
267267

268268
// STEP_START agg2
269+
res2, err := rdb.FTAggregateWithArgs(ctx,
270+
"idx:bicycle", "*",
271+
&redis.FTAggregateOptions{
272+
Load: []redis.FTAggregateLoad{
273+
{Field: "price"},
274+
},
275+
Apply: []redis.FTAggregateApply{
276+
{
277+
Field: "@price<1000",
278+
As: "price_category",
279+
},
280+
},
281+
GroupBy: []redis.FTAggregateGroupBy{
282+
{
283+
Fields: []interface{}{"@condition"},
284+
Reduce: []redis.FTAggregateReducer{
285+
{
286+
Reducer: redis.SearchSum,
287+
Args: []interface{}{"@price_category"},
288+
As: "num_affordable",
289+
},
290+
},
291+
},
292+
},
293+
},
294+
).Result()
295+
296+
if err != nil {
297+
panic(err)
298+
}
269299

270-
// Not currently implemented in go-redis.
300+
fmt.Println(len(res2.Rows)) // >>> 3
301+
302+
sort.Slice(res2.Rows, func(i, j int) bool {
303+
return res2.Rows[i].Fields["condition"].(string) <
304+
res2.Rows[j].Fields["condition"].(string)
305+
})
271306

307+
for _, row := range res2.Rows {
308+
fmt.Printf(
309+
"condition=%v, num_affordable=%v\n",
310+
row.Fields["condition"],
311+
row.Fields["num_affordable"],
312+
)
313+
}
314+
// >>> condition=new, num_affordable=3
315+
// >>> condition=refurbished, num_affordable=1
316+
// >>> condition=used, num_affordable=1
272317
// STEP_END
273318

274319
// STEP_START agg3
275320

276-
// Not currently implemented in go-redis.
321+
res3, err := rdb.FTAggregateWithArgs(ctx,
322+
"idx:bicycle", "*",
323+
&redis.FTAggregateOptions{
324+
Apply: []redis.FTAggregateApply{
325+
{
326+
Field: "'bicycle'",
327+
As: "type",
328+
},
329+
},
330+
GroupBy: []redis.FTAggregateGroupBy{
331+
{
332+
Fields: []interface{}{"@type"},
333+
Reduce: []redis.FTAggregateReducer{
334+
{
335+
Reducer: redis.SearchCount,
336+
As: "num_total",
337+
},
338+
},
339+
},
340+
},
341+
},
342+
).Result()
343+
344+
if err != nil {
345+
panic(err)
346+
}
347+
348+
fmt.Println(len(res3.Rows)) // >>> 1
277349

350+
for _, row := range res3.Rows {
351+
fmt.Printf(
352+
"type=%v, num_total=%v\n",
353+
row.Fields["type"],
354+
row.Fields["num_total"],
355+
)
356+
}
357+
// type=bicycle, num_total=10
278358
// STEP_END
279359

280360
// STEP_START agg4
281361
res4, err := rdb.FTAggregateWithArgs(ctx,
282362
"idx:bicycle", "*",
283363
&redis.FTAggregateOptions{
284364
Load: []redis.FTAggregateLoad{
285-
{
286-
Field: "__key",
287-
},
365+
{Field: "__key"},
288366
},
289367
GroupBy: []redis.FTAggregateGroupBy{
290368
{
@@ -343,6 +421,12 @@ func ExampleClient_query_agg() {
343421
// __key=bicycle:7, discounted=387, price=430
344422
// __key=bicycle:8, discounted=1080, price=1200
345423
// 3
424+
// condition=new, num_affordable=3
425+
// condition=refurbished, num_affordable=1
426+
// condition=used, num_affordable=1
427+
// 1
428+
// type=bicycle, num_total=10
429+
// 3
346430
// condition=new, bicycles=[bicycle:0 bicycle:5 bicycle:6 bicycle:7 bicycle:8]
347431
// condition=refurbished, bicycles=[bicycle:9]
348432
// condition=used, bicycles=[bicycle:1 bicycle:2 bicycle:3 bicycle:4]

0 commit comments

Comments
 (0)