Search code examples
goredisgo-redis

Add filter to aggregation function in go redis


I'm trying to use go-redis to calculate the sum of the field duration across entries in a redis database. I'm able to sum the entries if I use all the entries, but I want to filter out entries that has a timestamp within a certain range and can't find any good examples of how to do it.

How do I filter out what entries to aggregate in go-redis based on a timestamp field and a timerange?

    aggOptions := redis.FTAggregateOptions{
        GroupBy: []redis.FTAggregateGroupBy{
            {
                Filter: "", <---- I guess a filter should be inserted here, but how? 
                Fields: []interface{}{"@ID"},
                Reduce: []redis.FTAggregateReducer{
                    {
                        Reducer: redis.SearchSum,
                        Args:    []interface{}{"@duration"},
                        As:      "sum",
                    },
                },
            },
        },
    }
    aggResult, err := store.rdb.FTAggregateWithArgs(
        ctx,
        "*", 
        &aggOptions,
    ).Result()

Solution

  • You can specify a filter as follows.

    options := &redis.FTAggregateOptions{Filter: "@name=='foo' && @age < 20", DialectVersion: dlc}
    res, err := client.FTAggregateWithArgs(ctx, "idx1", "*", options).Result()
    

    Find more examples here.

    Is this what you are looking for?