Aggregation
Aggregations are powerful tools in Elasticsearch that allow you to summarize, compute statistics, and analyze data trends within your dataset. In the Laravel-Elasticsearch integration, aggregations are simplified to align with Eloquent’s method of handling aggregate functions, making it intuitive for developers to perform complex data analyzes.
Basic Aggregations
You can use standard aggregate functions such as count()
, max()
, min()
, avg()
, and sum()
directly on your Eloquent models, just like you would with a SQL database. These functions provide quick insights into your dataset.
These aggregation functions are straightforward and mirror the typical usage in Laravel’s Eloquent ORM, providing a seamless experience for developers.
Aggregations with Conditions
As the aggregation functions are part of the Eloquent ORM, you can also use them with conditions to filter the data you want to analyze.
Average price of products excluding ‘red’ and ‘green’ colored products
Grouped Aggregations
agg()
is an optimization method that allows you to call multiple aggregation functions on a single field in one call.
This call saves you from making multiple queries to get different statistics for the same field.
Returns count, average, minimum, maximum, and sum of sales for active products
Available aggregation functions: count
, avg
, min
, max
, sum
, matrix
.
Elasticsearch Matrix Aggregations
Elasticsearch offers advanced aggregation capabilities, including matrix stats aggregations, which provide comprehensive statistics about multiple fields. The Laravel-Elasticsearch integration simplifies the usage of these advanced features.
Example result for matrix(['price', 'orders']);
:
Matrix Results Explained
The result of a matrix aggregation is a detailed statistical summary of the selected fields. Here’s a breakdown of what each statistic represents:
- doc_count: The total number of documents that matched the aggregation query.
- fields: An array containing the statistical data for each field included in the matrix aggregation.
- name: The name of the field.
- count: The number of values analyzed for this field.
- mean: The average value.
- variance: The variance indicating the data’s spread.
- skewness: A measure of the asymmetry of the data distribution.
- kurtosis: A measure of the ‘tailedness’ of the data distribution.
- covariance: The covariance between the current field and other fields in the matrix, indicating how the fields vary together.
- correlation: The correlation between the current field and other fields, showing the strength and direction of a linear relationship.