Skip to content

Commit dd1a63a

Browse files
committed
[add] enable LOAD parameter on FT.AGGREGATE command
1 parent 6df6eae commit dd1a63a

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

redisearch/aggregate.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,18 @@ func (a *AggregateQuery) Limit(offset int, num int) *AggregateQuery {
187187
return a
188188
}
189189

190+
//Load document fields from the document HASH objects (if they are not in the sortables)
191+
func (a *AggregateQuery) Load( Properties []string) *AggregateQuery {
192+
nproperties := len(Properties)
193+
if nproperties > 0 {
194+
a.AggregatePlan = a.AggregatePlan.Add("LOAD", nproperties)
195+
for _, property := range Properties {
196+
a.AggregatePlan = a.AggregatePlan.Add(fmt.Sprintf( "@%s", property ))
197+
}
198+
}
199+
return a
200+
}
201+
190202
//Adds a GROUPBY clause to the aggregate plan
191203
func (a *AggregateQuery) GroupBy(group GroupBy) *AggregateQuery {
192204
a.AggregatePlan = a.AggregatePlan.AddFlat(group.Serialize())

redisearch/aggregate_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,3 +523,58 @@ func TestAggregateQuery_CursorHasResults(t *testing.T) {
523523
})
524524
}
525525
}
526+
527+
func TestAggregateQuery_Load(t *testing.T) {
528+
type fields struct {
529+
Query *redisearch.Query
530+
AggregatePlan redis.Args
531+
Paging *redisearch.Paging
532+
Max int
533+
WithSchema bool
534+
Verbatim bool
535+
WithCursor bool
536+
Cursor *redisearch.Cursor
537+
}
538+
type args struct {
539+
Properties []string
540+
}
541+
tests := []struct {
542+
name string
543+
fields fields
544+
args args
545+
want redis.Args
546+
}{
547+
{"TestAggregateQuery_Load_1",
548+
fields{nil, redis.Args{}, nil, 0, false, false, false, nil},
549+
args{[]string{"field1"}},
550+
redis.Args{"*", "LOAD", 1, "@field1"},
551+
},
552+
{"TestAggregateQuery_Load_2",
553+
fields{nil, redis.Args{}, nil, 0, false, false, false, nil},
554+
args{[]string{"field1", "field2", "field3", "field4"}},
555+
redis.Args{"*", "LOAD", 4, "@field1", "@field2", "@field3", "@field4"},
556+
},
557+
{"TestAggregateQuery_Load_Empty",
558+
fields{nil, redis.Args{}, nil, 0, false, false, false, nil},
559+
args{[]string{}},
560+
redis.Args{"*"},
561+
},
562+
}
563+
for _, tt := range tests {
564+
t.Run(tt.name, func(t *testing.T) {
565+
a := &redisearch.AggregateQuery{
566+
Query: tt.fields.Query,
567+
AggregatePlan: tt.fields.AggregatePlan,
568+
Paging: tt.fields.Paging,
569+
Max: tt.fields.Max,
570+
WithSchema: tt.fields.WithSchema,
571+
Verbatim: tt.fields.Verbatim,
572+
WithCursor: tt.fields.WithCursor,
573+
Cursor: tt.fields.Cursor,
574+
}
575+
if got := a.Load(tt.args.Properties).Serialize(); !reflect.DeepEqual(got, tt.want) {
576+
t.Errorf("Load() = %v, want %v", got, tt.want)
577+
}
578+
})
579+
}
580+
}

0 commit comments

Comments
 (0)