Elasticsearch aggregationlar veritabanlarındaki normal GROUP BY, AVG, SUM, MIN, COUNT gibi fonksiyonlara benzerler. Aşağıdaki örnekte bunların nasıl kullanılacağını göreceğiz. Daha fazla bilgi için Aggregations sayfasını okuyun.

Bu örnekte, elasticsearch ve veritabanının içerikleri aynı. Çalışmak için elimizde year, price, title gibi alanlar olacak.


Örnek 1


Bu örnekte year ve price alanları ile ilgili istatistikler alacağız.


Elasticsearch


curl -XGET "http://127.0.0.1:9203/_search?post_dev" -d'
{
"size": 0,
"aggregations": {
"average_year": {
"avg": {
"field": "year"
}
},
"minimum_price": {
"min": {
"field": "price"
}
},
"maximum_price": {
"max": {
"field": "price"
}
},
"total_price": {
"sum": {
"field": "price"
}
},
"total_processed_records": {
"value_count": {
"field": "price"
}
}
}
}'

Sonuç


{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 5000,
"max_score": 0,
"hits": []
},
"aggregations": {
"average_year": {
"value": 2007.398
},
"minimum_price": {
"value": 0.5
},
"total_price": {
"value": 14409.989999999974
},
"maximum_price": {
"value": 5.55
},
"total_processed_records": {
"value": 5000
}
}
}

MySQL


mysql> SELECT
-> AVG(year), MIN(price), MAX(price), SUM(price), COUNT(*)
-> FROM post;
+-----------+------------+------------+------------+----------+
| AVG(year) | MIN(price) | MAX(price) | SUM(price) | COUNT(*) |
+-----------+------------+------------+------------+----------+
| 2007.398 | 0.50 | 5.55 | 14409.99 | 5000 |
+-----------+------------+------------+------------+----------+
1 row in set (0.00 sec)