Aggregations are a method of grouping documents together and run processing on them on the server to transform them into data that you need in your application, without having to perform the computation client-side.
Aggregations in Redis are build around an aggregation pipeline, you will start off with a
RedisAggregationSet<T> of objects that you have indexed in Redis. From there you can
- Query to filter down the results you want
- Apply functions to them to combine functions to them
- Group like features together
- Run reductions on groups
- Sort records
- Further filter down records
Redis OM .NET provides an
RedisAggregationSet<T> class that will let you perform aggregations on employees, let's start off with a trivial aggregation. Let's start off by defining a model:
We'll then create the index for that model, pull out a
RedisAggregationSet<T> from our provider, and initialize the index, and seed some data into our database
The Aggregations pipeline is all built around the
RedisAggregationSet<T> this Set is generic, so you can provide the model that you want to build your aggregations around (an Indexed type), but you will notice that the return type from queries to the
RedisAggregationSet is the generic type passed into it. Rather it is an
T is the generic type you passed into it. This is a really important concept, when results are returned from aggregations, they are not hydrated into an object like they are with queries. That's because Aggregations aren't meant to pull out your model data from the database, rather they are meant to pull out aggregated results. The AggregationResult has a
RecordShell field, which is ALWAYS null outside of the pipeline. It can be used to build expressions for querying objects in Redis, but when the AggregationResult lands, it will not contain a hydrated record, rather it will contain a dictionary of Aggregations built by the Aggregation pipeline. This means that you can access the results of your aggregations by indexing into the AggregationResult.
Let's try running an aggregation where we find the Sum of the sales for all our employees in EMEA. So the Aggregations Pipeline will use the
RecordShell object, which is a reference to the generic type of the aggregation set, for something as simple as a group-less SUM, you will simply get back a numeric type from the aggregation.
Where expression tells the aggregation pipeline which records to consider, and subsequently the
SUM expression indicates which field to sum. Aggregations are a rich feature and this only scratches the surface of it, these pipelines are remarkably flexible and provide you the ability to do all sorts of neat operations on your Data in Redis.