7.15.15. math_abs#

7.15.15.1. 概要#

バージョン 7.0.4 で追加.

math_abs は絶対値を返します。

この関数を有効にするには、以下のコマンドで functions/math プラグインを登録します:

plugin_register functions/math

7.15.15.2. 構文#

math_abs は引数を一つだけとります。 それは target です。

math_abs(target)

7.15.15.3. 使い方#

使い方を示すために使うスキーマ定義とサンプルデータは以下の通りです。

サンプルスキーマ:

実行例:

plugin_register functions/math
# [[0,1337566253.89858,0.000355720520019531],true]
table_create Shops TABLE_HASH_KEY ShortText
# [[0,1337566253.89858,0.000355720520019531],true]
column_create Shops from_station COLUMN_SCALAR Int32
# [[0,1337566253.89858,0.000355720520019531],true]
column_create Shops from_office COLUMN_SCALAR Int32
# [[0,1337566253.89858,0.000355720520019531],true]

サンプルデータ:

実行例:

load --table Shops
[
{"_key": "Coffee Shop",         "from_station":  50},
{"_key": "Donut & Coffee Shop", "from_station": 400},
{"_key": "Cake & Coffee Shop",  "from_station": 200}
]
# [[0,1337566253.89858,0.000355720520019531],3]

職場から最も近いお店を返す math_abs 関数の簡単な例はこちらです。

最も近いお店を探すためには距離を計算してあげる必要があります。もし職場が駅から250mのところにあるとしたら、 math_abs(250 - from_station) で距離を計算できます。

実行例:

select Shops --filter true --output_columns '_key, from_office' --scorer 'from_office = math_abs(250 - from_station)' --sort_keys from_office
# [
#   [
#     0,
#     1337566253.89858,
#     0.000355720520019531
#   ],
#   [
#     [
#       [
#         3
#       ],
#       [
#         [
#           "_key",
#           "ShortText"
#         ],
#         [
#           "from_office",
#           "Int32"
#         ]
#       ],
#       [
#         "Cake & Coffee Shop",
#         50
#       ],
#       [
#         "Donut & Coffee Shop",
#         150
#       ],
#       [
#         "Coffee Shop",
#         200
#       ]
#     ]
#   ]
# ]

--sort_keys from_office を指定することで、職場から近いお店を昇順で表示できます。

7.15.15.4. 引数#

必須の引数は1つです。

7.15.15.4.1. target#

select 対象の table に指定されたテーブルのカラムを指定します。

7.15.15.5. 戻り値#

math_abs は対象の絶対値を返します。