OpenSearch menyediakan fungsi agregat yang didefinisikan pengguna (UDAF) bawaan untuk operasi agregasi umum, seperti sum, average, maximum, minimum, dan count.
Daftar UDAF
-
sum: Menghitung jumlah nilai setelah agregasi.
-
avg: Menghitung rata-rata nilai setelah agregasi.
-
max: Menghitung nilai maksimum setelah agregasi.
-
min: Menghitung nilai minimum setelah agregasi.
-
count: Menghitung jumlah item setelah agregasi.
-
MAXLABEL: Mengambil nilai label yang sesuai dengan nilai maksimum setelah agregasi.
Contoh penggunaan
Data uji
Contoh berikut menggunakan tabel phone di lingkungan staging, yang berisi informasi ponsel dari berbagai merek utama.
|
nid |
title |
price |
brand |
size |
color |
|
1 |
Huawei Mate 9 Kirin 960 chip Leica dual lens |
3599 |
Huawei |
5.9 |
Red |
|
2 |
Huawei/Huawei P10 Plus full Netcom phone |
4388 |
Huawei |
5.5 |
Blue |
|
3 |
Xiaomi/Xiaomi Redmi 4X 32G full Netcom 4G smartphone |
899 |
Xiaomi |
5.0 |
Black |
|
4 |
OPPO R11 full Netcom front and rear 20-megapixel fingerprint recognition camera phone r11r9s |
2999 |
OPPO |
5.5 |
Red |
|
5 |
Meizu/Meizu Meilan E2 full Netcom front fingerprint fast-charge 4G smartphone |
1299 |
Meizu |
5.5 |
Silver white |
|
6 |
Nokia/Nokia 105 Mobile loud senior phone candy bar keypad student elderly small phone long standby |
169 |
Nokia |
1.4 |
Blue |
|
7 |
Apple/Apple iPhone 6s 32G original sealed domestic version in stock fast shipping |
3599 |
Apple |
4.7 |
Silver white |
|
8 |
Apple/Apple iPhone 7 Plus 128G full Netcom 4G phone |
5998 |
Apple |
5.5 |
Jet black |
|
9 |
Apple/Apple iPhone 7 32G full Netcom 4G smartphone |
4298 |
Apple |
4.7 |
Black |
|
10 |
Samsung/Samsung GALAXY S8 SM-G9500 full Netcom 4G phone |
5688 |
Samsung |
5.6 |
Misty blue |
Contoh kueri
● Ambil semua konten dari tabel.
SELECT * FROM phone ORDER BY nid LIMIT 1000 USE_TIME: 0.036, ROW_COUNT: 10
------------------------------- TABLE INFO ---------------------------
nid | title | price | brand | size | color |
1 | null | 3599 | Huawei | 5.9 | null |
2 | null | 4388 | Huawei | 5.5 | null |
3 | null | 899 | Xiaomi | 5 | null |
4 | null | 2999 | OPPO | 5.5 | null |
5 | null | 1299 | Meizu | 5.5 | null |
6 | null | 169 | Nokia | 1.4 | null |
7 | null | 3599 | Apple | 4.7 | null |
8 | null | 5998 | Apple | 5.5 | null |
9 | null | 4298 | Apple | 4.7 | null |
10 | null | 5688 | Samsung | 5.6 | null |
Catatan: Bidang title dan color merupakan bidang ringkasan dan ditampilkan sebagai null.
-
Hitung total harga produk untuk setiap merek menggunakan fungsi sum.
SELECT brand, sum(price) FROM phone GROUP BY (brand) ORDER BY brand LIMIT 1000USE_TIME: 0.152, ROW_COUNT: 7
------------------------------- TABLE INFO ---------------------------
brand | SUM(price) |
Apple | 13895 |
Huawei | 7987 |
Meizu | 1299 |
Nokia | 169 |
OPPO | 2999 |
Samsung | 5688 |
Xiaomi | 899 |
-
Temukan ponsel dengan harga tertinggi untuk setiap merek menggunakan fungsi max, diurutkan berdasarkan harga secara menurun.
SELECT brand, max(price) AS price FROM phone GROUP BY (brand) ORDER BY price DESC LIMIT 1000USE_TIME: 0.053, ROW_COUNT: 7
------------------------------- TABLE INFO ---------------------------
brand | price |
Apple | 5998 |
Samsung | 5688 |
Huawei | 4388 |
OPPO | 2999 |
Meizu | 1299 |
Xiaomi | 899 |
Nokia | 169 |
-
Temukan ukuran layar ponsel dengan harga tertinggi untuk setiap merek menggunakan fungsi MAXLABEL.
SELECT brand, MAXLABEL(size, price) AS size FROM phone GROUP BY brand