How to model user data and calculate aggregations using reducers such as averages?
Last updated 15, Apr 2024
Question
How to model user data and calculate aggregations with reducers such as averages?
Answer
To model users' data in a way that allows querying for the average or any other supported reducers (count, min, max, etc.) of the users, you can follow the example. Let's store users' data, including the age of the users: you will learn how to calculate the average age.
- Insert data. For example, you may create a Redis hash for each user with a unique key. For example, you can use the key format "
user:{user_id}
". - Store the user attributes in the hash, including the user's age. For example, you can use the field "
age
" in the hash to store the age value for each user. - Create a search index on the "
age
" field of the user's hashes. You can use the FT.CREATE command to create the index. Set the field type for the "age
" field as NUMERIC. - To query for the average age of the users, you can use the FT.AGGREGATE command with the REDUCE function to calculate the average of the "
age
" field across all user hashes. You can specify the index to search in and the aggregation function to calculate the average.
Here is an example of how you can use Redis commands to accomplish this:
# Store user data in Redis hashes
HSET user:1 age 38
HSET user:2 age 42
HSET user:3 age 33
# Create an index on the 'age' field
FT.CREATE users_idx ON HASH PREFIX 1 user: SCHEMA age NUMERIC
# Query for the average age of the users
FT.AGGREGATE users_idx "*" GROUPBY 0 REDUCE AVG 1 @age AS avg_age
The FT.AGGREGATE command will calculate the average over all results and return:
1) (integer) 12) 1) "avg_age"
2) "37.6666666667"
Using the APPLY argument, it is possible to compute a mathematical transformation and approximate the result, thus stripping the decimal part.
FT.AGGREGATE users_idx "*" GROUPBY 0 REDUCE AVG 1 @age AS avg_age APPLY ceil(@avg_age) AS ceil_avg_age
1) (integer) 1
2) 1) "avg_age"
2) "37.6666666667"
3) "ceil_avg_age"
4) "38"
Redis provides powerful querying capabilities and aggregation functions to perform various calculations on indexed fields. Refer to the documentation for more information on the available features and query syntax.
References
Groupings, projections, and aggregation functions FT.AGGREGATE