For developers8. Aggregations
A common need for applications, in addition to retrieving information as a document list, like you have done with the "
FT.SEARCH" command, is to do some "aggregation".For example if we look at the movie documents, you may want to retrieve the number of movies grouped by release year starting with the most recent ones.
For this, Redis Stack provides the FT.AGGREGATE command, with aggregations described as a data processing pipeline.
Let's check out some examples.
#Group By & Sort By
Number of movies by year
Number of movies by year from the most recent to the oldest
Number of movies by genre, with the total number of votes, and average rating
Count the number of females by country sorted from the biggest to smallest number.
#Apply Functions
Number of logins per year and month
The
idx:user index contains the last_login field. This field stores the last login time as an EPOC timestamp.Redis Stack search aggregation allows you to apply transformations to each record. This is done using the APPLY parameter.
For this example you have to use a date/time function to extract the month and year from the timestamp.
Number of logins per weekday
Using the date/time Apply functions it is possible to extract the day of the week from the timestamp, so let's see how the logins are distributed over the week.
#Filter
In the previous example you used the
query string parameter to select all documents ("*") or a subset of the documents ("@gender:{female}")It is also possible to filter the results using a predicate expression relating to values in each result. This is applied post-query and relates to the current state of the pipeline. This is done using the FILTER parameter.
Count the number of females by country, except China, with more than 100 users, and sorted from the biggest to lowest number
Number of login per month, for year 2020
This is similar to the previous query with the addition of a filter on the year.