すべてのプロダクト
Search
ドキュメントセンター

OpenSearch:UDAF の使用方法

最終更新日:Jun 19, 2026

OpenSearch には、合計、平均、最大値、最小値、カウントなどの一般的な集計操作向けに、組み込みのユーザー定義集計関数 (UDAF) が用意されています。

UDAF リスト

  • sum:集計後の値の合計を計算します。

  • avg:集計後の値の平均を計算します。

  • max:集計後の最大値を計算します。

  • min:集計後の最小値を計算します。

  • count:集計後の項目数を数えます。

  • MAXLABEL:集計後の最大値に対応するラベルの値を取得します。

使用例

テストデータ

次の例では、主要ブランドの携帯電話情報を含むステージング環境の phone テーブルを使用します。

nid

title

price

brand

size

color

1

Huawei Mate 9 Kirin 960 chip Leica dual lens

3599

Huawei

5.9

レッド

2

Huawei/Huawei P10 Plus full Netcom phone

4388

Huawei

5.5

ブルー

3

Xiaomi/Xiaomi Redmi 4X 32G full Netcom 4G smartphone

899

Xiaomi

5.0

ブラック

4

OPPO R11 full Netcom front and rear 20-megapixel fingerprint recognition camera phone r11r9s

2999

OPPO

5.5

レッド

5

Meizu/Meizu Meilan E2 full Netcom front fingerprint fast-charge 4G smartphone

1299

Meizu

5.5

シルバーホワイト

6

Nokia/Nokia 105 Mobile loud senior phone candy bar keypad student elderly small phone long standby

169

Nokia

1.4

ブルー

7

Apple/Apple iPhone 6s 32G original sealed domestic version in stock fast shipping

3599

Apple

4.7

シルバーホワイト

8

Apple/Apple iPhone 7 Plus 128G full Netcom 4G phone

5998

Apple

5.5

ジェットブラック

9

Apple/Apple iPhone 7 32G full Netcom 4G smartphone

4298

Apple

4.7

ブラック

10

Samsung/Samsung GALAXY S8 SM-G9500 full Netcom 4G phone

5688

Samsung

5.6

ミスティブルー

クエリの例

● テーブルのすべての内容を取得します。

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 |

注:title フィールドと color フィールドはサマリーフィールドのため、null として表示されます。

  • sum 関数を使用して、ブランドごとの製品価格の合計を計算します。

SELECT brand, sum(price) FROM phone GROUP BY (brand) ORDER BY brand LIMIT 1000
USE_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 |
  • max 関数を使用して、ブランドごとの最高価格の携帯電話を検索し、価格の降順でソートします。

SELECT brand, max(price) AS price FROM phone GROUP BY (brand) ORDER BY price DESC LIMIT 1000
USE_TIME: 0.053, ROW_COUNT: 7

------------------------------- TABLE INFO ---------------------------
               brand |               price |
               Apple |                5998 |
             Samsung |                5688 |
              Huawei |                4388 |
                OPPO |                2999 |
               Meizu |                1299 |
              Xiaomi |                 899 |
               Nokia |                 169 |
  • MAXLABEL 関数を使用して、ブランドごとに最高価格の携帯電話の画面サイズを取得します。

SELECT brand, MAXLABEL(size, price) AS size FROM phone GROUP BY brand