Skip to content

Commit

Permalink
fix: rearange args for ft.aggregate
Browse files Browse the repository at this point in the history
apply should be before any groupby or sortby
  • Loading branch information
ndyakov committed Feb 6, 2025
1 parent 0b34b19 commit 42dc7c5
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 17 deletions.
41 changes: 25 additions & 16 deletions search_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,12 @@ func FTAggregateQuery(query string, options *FTAggregateOptions) AggregateQuery
if options.Timeout > 0 {
queryArgs = append(queryArgs, "TIMEOUT", options.Timeout)
}
for _, apply := range options.Apply {
queryArgs = append(queryArgs, "APPLY", apply.Field)
if apply.As != "" {
queryArgs = append(queryArgs, "AS", apply.As)
}
}
if options.GroupBy != nil {
for _, groupBy := range options.GroupBy {
queryArgs = append(queryArgs, "GROUPBY", len(groupBy.Fields))
Expand Down Expand Up @@ -549,12 +555,6 @@ func FTAggregateQuery(query string, options *FTAggregateOptions) AggregateQuery
if options.SortByMax > 0 {
queryArgs = append(queryArgs, "MAX", options.SortByMax)
}
for _, apply := range options.Apply {
queryArgs = append(queryArgs, "APPLY", apply.Field)
if apply.As != "" {
queryArgs = append(queryArgs, "AS", apply.As)
}
}
if options.LimitOffset > 0 {
queryArgs = append(queryArgs, "LIMIT", options.LimitOffset)
}
Expand Down Expand Up @@ -661,11 +661,12 @@ func (cmd *AggregateCmd) readReply(rd *proto.Reader) (err error) {
data, err := rd.ReadSlice()
if err != nil {
cmd.err = err
return nil
return err
}
cmd.val, err = ProcessAggregateResult(data)
if err != nil {
cmd.err = err
return err
}
return nil
}
Expand Down Expand Up @@ -699,6 +700,12 @@ func (c cmdable) FTAggregateWithArgs(ctx context.Context, index string, query st
if options.Timeout > 0 {
args = append(args, "TIMEOUT", options.Timeout)
}
for _, apply := range options.Apply {
args = append(args, "APPLY", apply.Field)
if apply.As != "" {
args = append(args, "AS", apply.As)
}
}
if options.GroupBy != nil {
for _, groupBy := range options.GroupBy {
args = append(args, "GROUPBY", len(groupBy.Fields))
Expand Down Expand Up @@ -740,12 +747,6 @@ func (c cmdable) FTAggregateWithArgs(ctx context.Context, index string, query st
if options.SortByMax > 0 {
args = append(args, "MAX", options.SortByMax)
}
for _, apply := range options.Apply {
args = append(args, "APPLY", apply.Field)
if apply.As != "" {
args = append(args, "AS", apply.As)
}
}
if options.LimitOffset > 0 {
args = append(args, "LIMIT", options.LimitOffset)
}
Expand Down Expand Up @@ -1693,7 +1694,8 @@ func (cmd *FTSearchCmd) readReply(rd *proto.Reader) (err error) {

// FTSearch - Executes a search query on an index.
// The 'index' parameter specifies the index to search, and the 'query' parameter specifies the search query.
// For more information, please refer to the Redis documentation:
// For more information, please refer to the Redis documentation about [FT.SEARCH].
//
// [FT.SEARCH]: (https://redis.io/commands/ft.search/)
func (c cmdable) FTSearch(ctx context.Context, index string, query string) *FTSearchCmd {
args := []interface{}{"FT.SEARCH", index, query}
Expand All @@ -1704,6 +1706,12 @@ func (c cmdable) FTSearch(ctx context.Context, index string, query string) *FTSe

type SearchQuery []interface{}

// FTSearchQuery - Executes a search query on an index with additional options.
// The 'index' parameter specifies the index to search, the 'query' parameter specifies the search query,
// and the 'options' parameter specifies additional options for the search.
// For more information, please refer to the Redis documentation about [FT.SEARCH].
//
// [FT.SEARCH]: (https://redis.io/commands/ft.search/)
func FTSearchQuery(query string, options *FTSearchOptions) SearchQuery {
queryArgs := []interface{}{query}
if options != nil {
Expand Down Expand Up @@ -1816,7 +1824,8 @@ func FTSearchQuery(query string, options *FTSearchOptions) SearchQuery {
// FTSearchWithArgs - Executes a search query on an index with additional options.
// The 'index' parameter specifies the index to search, the 'query' parameter specifies the search query,
// and the 'options' parameter specifies additional options for the search.
// For more information, please refer to the Redis documentation:
// For more information, please refer to the Redis documentation about [FT.SEARCH].
//
// [FT.SEARCH]: (https://redis.io/commands/ft.search/)
func (c cmdable) FTSearchWithArgs(ctx context.Context, index string, query string, options *FTSearchOptions) *FTSearchCmd {
args := []interface{}{"FT.SEARCH", index, query}
Expand Down Expand Up @@ -1908,7 +1917,7 @@ func (c cmdable) FTSearchWithArgs(ctx context.Context, index string, query strin
}
}
if options.SortByWithCount {
args = append(args, "WITHCOUT")
args = append(args, "WITHCOUNT")
}
}
if options.LimitOffset >= 0 && options.Limit > 0 {
Expand Down
27 changes: 26 additions & 1 deletion search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ var _ = Describe("RediSearch commands Resp 2", Label("search"), func() {
Expect(res.Rows[0].Fields["t1"]).To(BeEquivalentTo("b"))
})

It("should FTAggregate load ", Label("search", "ftaggregate"), func() {
FIt("should FTAggregate load ", Label("search", "ftaggregate"), func() {
text1 := &redis.FieldSchema{FieldName: "t1", FieldType: redis.SearchFieldTypeText}
text2 := &redis.FieldSchema{FieldName: "t2", FieldType: redis.SearchFieldTypeText}
val, err := client.FTCreate(ctx, "idx1", &redis.FTCreateOptions{}, text1, text2).Result()
Expand All @@ -640,6 +640,31 @@ var _ = Describe("RediSearch commands Resp 2", Label("search"), func() {
Expect(res.Rows[0].Fields["t2"]).To(BeEquivalentTo("world"))
})

FIt("should FTAggregate apply and groupby", Label("search", "ftaggregate"), func() {
text1 := &redis.FieldSchema{FieldName: "PrimaryKey", FieldType: redis.SearchFieldTypeText, Sortable: true}
num1 := &redis.FieldSchema{FieldName: "CreatedDateTimeUTC", FieldType: redis.SearchFieldTypeNumeric, Sortable: true}
val, err := client.FTCreate(ctx, "idx1", &redis.FTCreateOptions{}, text1, num1).Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(BeEquivalentTo("OK"))
WaitForIndexing(client, "idx1")

// 6 feb
client.HSet(ctx, "doc1", "PrimaryKey", "9::362330", "CreatedDateTimeUTC", "1738823999")

// 12 feb
client.HSet(ctx, "doc2", "PrimaryKey", "9::362329", "CreatedDateTimeUTC", "1739342399")
client.HSet(ctx, "doc3", "PrimaryKey", "9::362329", "CreatedDateTimeUTC", "1739353199")

reducer := redis.FTAggregateReducer{Reducer: redis.SearchCount}

options := &redis.FTAggregateOptions{
Apply: []redis.FTAggregateApply{{Field: "@CreatedDateTimeUTC /(60*60*24)", As: "TimestampAsDay"}},
GroupBy: []redis.FTAggregateGroupBy{{Fields: []interface{}{"@TimestampAsDay"}, Reduce: []redis.FTAggregateReducer{reducer}}},
}
res, err := client.FTAggregateWithArgs(ctx, "idx1", "*", options).Result()

Check failure on line 664 in search_test.go

View workflow job for this annotation

GitHub Actions / build (1.23.x, 7.4.2-54)

declared and not used: res

Check failure on line 664 in search_test.go

View workflow job for this annotation

GitHub Actions / test-redis-ce (8.0-M03, 1.22.x)

res declared and not used

Check failure on line 664 in search_test.go

View workflow job for this annotation

GitHub Actions / build (1.21.x)

res declared and not used

Check failure on line 664 in search_test.go

View workflow job for this annotation

GitHub Actions / test-redis-ce (8.0-M03, 1.23.x)

declared and not used: res

Check failure on line 664 in search_test.go

View workflow job for this annotation

GitHub Actions / build (1.22.x)

res declared and not used

Check failure on line 664 in search_test.go

View workflow job for this annotation

GitHub Actions / test-redis-ce (7.4.2, 1.22.x)

res declared and not used

Check failure on line 664 in search_test.go

View workflow job for this annotation

GitHub Actions / build (1.23.x)

declared and not used: res

Check failure on line 664 in search_test.go

View workflow job for this annotation

GitHub Actions / test-redis-ce (7.4.2, 1.23.x)

declared and not used: res

Check failure on line 664 in search_test.go

View workflow job for this annotation

GitHub Actions / test-redis-ce (7.2.7, 1.22.x)

res declared and not used

Check failure on line 664 in search_test.go

View workflow job for this annotation

GitHub Actions / test-redis-ce (7.2.7, 1.23.x)

declared and not used: res
Expect(err).NotTo(HaveOccurred())
})

It("should FTAggregate apply", Label("search", "ftaggregate"), func() {
text1 := &redis.FieldSchema{FieldName: "PrimaryKey", FieldType: redis.SearchFieldTypeText, Sortable: true}
num1 := &redis.FieldSchema{FieldName: "CreatedDateTimeUTC", FieldType: redis.SearchFieldTypeNumeric, Sortable: true}
Expand Down

0 comments on commit 42dc7c5

Please sign in to comment.