7.16.18. math_abs — Groonga v16.0.8 documentation

Skip to main content
`Ctrl`+`K`

# 7.16.18. `math_abs`

## 7.16.18.1. Summary

Added in version 7.0.4.

`math_abs` returns the absolute value of value.

To enable this function, register `functions/math` plugin by following the command:

    plugin_register functions/math

## 7.16.18.2. Syntax

`math_abs` requires one argument - `target`.

    math_abs(target)

## 7.16.18.3. Usage

Here is a schema definition and sample data.

Sample schema:

Execution example:

    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]

Sample data:

Execution example:

    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]

Here is the simple usage of `math_abs` function which returns nearest shops from office.

To detect nearest shops, we need to calculate distance. If the distance of your office from station is 250 meters, you can calculate it by `math_abs(250 - from_station)`.

Execution example:

    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
    #       ]
    #     ]
    #   ]
    # ]

By specifying `--sort_keys from_office`, you can show nearest shops by ascending order.

## 7.16.18.4. Parameters

There is only one required parameter.

### 7.16.18.4.1. `target`

Specifies a column of table that is specified by `table` parameter in `select`.

## 7.16.18.5. Return value

`math_abs` returns the absolute value of target column value.