To know more about What is aggregations in Elasticsearch?
- Metrics aggregations
- Avg aggregation
- Cardinality aggregation
- Extended stats aggregation
- Min & Max aggregation
- Sum aggregation
- Bucket aggregations
- Nested Bucket aggregations
2. Kloojj.com
Suyog Dilip Kale
Technology Evangelist
Chief Architect
www.kloojj.com
Organiser
Pune Developer’s Community
www.meetup.com/Pune-Developers-Community
http://www.punedevscommunity.in/
3. Kloojj.com
● Aggregations
○ What is aggregations in Elasticsearch?
○ Metrics aggregations
■ Avg aggregation
■ Cardinality aggregation
■ Extended stats aggregation
■ Min & Max aggregation
■ Sum aggregation
○ Bucket aggregations
○ Nested Bucket aggregations
4. Kloojj.com
● Read-What is aggregations in Elasticsearch
○ Aggregations helps provide aggregated data based on a search query.
○ It can be composed in order to build complex summaries of the data.
○ There are many different types of aggregations, each with its own
purpose and output.
○ Elasticsearch aggregations let you zoom out to explore trends and
patterns in your data.
○ What if your ecommerce portal has billions of user visits and you
want drill down it by country, states and then cities? What if you want
to see avg user age groups OR gender wise product interests ? What if
you want calculate daily, weekly or monthly sales ?
○ Everything is much easy with Elasticsearch aggregations
5. Kloojj.com
● Read-Metrics aggregations
○ Metrics Aggregations are used to compute metrics over set of
documents. Numeric matrices are either single-valued like average
aggregation or multi-valued like stats.
○ Avg aggregations: to get the average of any numeric field present in
the aggregated documents, see below example:
GET /website/user/_search
{
"aggs": {
"avg_score": {
"avg": {
"field": "score"
}
}
}
}
6. Kloojj.com
● Read-Metrics aggregations
○ Cardinality aggregations: to get the count of distinct values of a
particular field, see below example:
GET /website/user/_search
{
"aggs": {
"cardinality_cname": {
"cardinality": {
"field": "cname"
}
}
}
}
○ This will give you distinct country name count.
7. Kloojj.com
● Read-Metrics aggregations
○ Extended stats aggregations: it generates all the statistics about a
specific numerical field in aggregated documents, for example:
GET /website/user/_search
{
"aggs": {
"stats_score": {
"stats": {
"field": "score"
}
}
}
}
○ It will return all statistics on score field like total records,minimum,
maximum, average and total sum value.
8. Kloojj.com
● Read-Metrics aggregations
○ Min & Max aggregations: These aggregations can be used to find the
max or min value of a specific numeric field in aggregated documents.
For example below one returns max value from score field:
GET /website/user/_search
{
"aggs": {
"max_score": {
"max": {
"field": "score"
}
}
}
}
9. Kloojj.com
● Read-Metrics aggregations
○ Sum aggregation: This aggregation calculates the sum of a specific
numeric field in aggregated documents. For example:
GET /website/user/_search
{
"aggs": {
"total_score": {
"sum": {
"field": "score"
}
}
}
}
○ This returns the total of score value
10. Kloojj.com
● Read-Bucket aggregations
○ These aggregations contain many buckets for different types of
aggregations having a criterion, which determines whether a
document belongs to that bucket or not.
○ There are many other special bucket aggregations, those are −
● Date Histogram Aggregation
● Date Range Aggregation
● Filter Aggregation
● Filters Aggregation
● Geo Distance Aggregation
● GeoHash grid Aggregation
● Global Aggregation
● Histogram Aggregation
● IPv4 Range Aggregation
● Missing Aggregation
● Nested Aggregation
● Range Aggregation
● Reverse nested Aggregation
● Sampler Aggregation
● Significant Terms Aggregation
● Terms Aggregation
11. Kloojj.com
● Read-Bucket aggregations
○ Let’s try term bucket aggregation
GET /website/user/_search
{
"aggs": {
"by_cname": {
"terms": {
"field": "cname"
}
}
}
}
○ If you see the result it will return the document counts for each
country found in aggregated documents
12. Kloojj.com
● Read-Nested Bucket aggregations
○ To drill down the aggregation results, it is best way to nest them, for
example, if you want to get counts against each action by country try
below query:
GET /website/user/_search
{
"aggs": {
"by_cname": {
"terms": {
"field": "cname"
},
"aggs": {
"by_action": {
"terms": {
"field": "action"
}
}
}
}
}
}