# ReQL Functions

> Source: https://docs.recombee.com/reql/functions

> For the complete documentation index, see [llms.txt](/llms.txt).

# ReQL Functions

Functions may be used to enhance your [ReQL](/reql) queries – for example[up-sell](/reql/functions/context#reql-func-context-item) or [getting nearby items](/reql/functions/geographical#reql-func-earth-distance) can be achieved easily by functions.

There are various functions like [math functions](/reql/functions/math) (rounding, computing square root …), [string functions](/reql/functions/string) (converting case), function for [getting size of set or string](/reql/functions/miscellaneous#reql-func-size), [getting current timestamp](/reql/functions/type-conversion#reql-func-timestamp), functions for [conversions between types](/reql/functions/type-conversion) (for example conversion between date in a text representation and timestamp) or even [lambda functions](/reql/functions/lambda). If you are missing some function that you would like to use, please let us know.

ReQL function can take arguments and returns a single value. Arguments are placed between parentheses and are separated by commas – for example computing base 2 logarithm of 8 looks like this: `log(8,2)`. If the function takes no arguments the parentheses may be omitted (for e.g. `now`). If the function is given wrong number of arguments, or the arguments have unsupported type, an error is produced. If any of the arguments is null, then result is also a null value.

## Quick Overview

### [Context](/reql/functions/context)

| Name                                                                  | Description                                                                        | Example                         |
| --------------------------------------------------------------------- | ---------------------------------------------------------------------------------- | ------------------------------- |
| [context\_item](/reql/functions/context#reql-func-context-item)       | Retrieves property value of the item, that is currently viewed by the user.        | context\_item\["price"\]        |
| [context\_user](/reql/functions/context#reql-func-context-user)       | Retrieves property value of the user, that is about to obtain the recommendations. | context\_user\["country"\]      |
| [context\_segment](/reql/functions/context#reql-func-context-segment) | Retrieves the Segment on which the recommendations are based                       | context\_segment\["segmentId"\] |

### [Miscellaneous](/reql/functions/miscellaneous)

| Name                                                                | Description                                                  | Example                                   |
| ------------------------------------------------------------------- | ------------------------------------------------------------ | ----------------------------------------- |
| [now](/reql/functions/miscellaneous#reql-func-now)                  | Returns current timestamp                                    | now() > timestamp("2015-06-24T17:35:50Z") |
| [random](/reql/functions/miscellaneous#reql-func-random)            | Returns a random number between 0 and 1                      | 1\>=random()>=0                           |
| [size](/reql/functions/miscellaneous#reql-func-size)                | Returns the length of a string or number of objects in a set | size("Recombee") == 8                     |
| [item\_values](/reql/functions/miscellaneous#reql-func-item-values) | Returns property values of a particular item as a dictionary | item\_values("item-42")\["description"\]  |
| [reduce](/reql/functions/miscellaneous#reql-func-reduce)            | Reduces a set into a single value using an operator          | reduce("+", {1, 2, 3}) == 6               |

### [Lambda](/reql/functions/lambda)

| Name                                              | Description                                                                                | Example                                                  |
| ------------------------------------------------- | ------------------------------------------------------------------------------------------ | -------------------------------------------------------- |
| [map](/reql/functions/lambda#reql-func-map)       | Applies the lambda expression to every member of a set                                     | map(lambda 'x': 2\*'x', {1, 2, 3}) == {2, 4, 6}          |
| [select](/reql/functions/lambda#reql-func-select) | Returns only those values of input set for which the lambda expression returns true        | select(lambda 'x': 'x' \> 5, {10, 2, 13, 1}) == {10, 13} |
| [exists](/reql/functions/lambda#reql-func-exists) | Returns _true_ if lambda expression is satisfied for at least one element of the input set | exists(lambda 'x': 'x' \> 5, {2, 13, 4})                 |

### [Type Conversion](/reql/functions/type-conversion)

| Name                                                             | Description                        | Example                                                    |
| ---------------------------------------------------------------- | ---------------------------------- | ---------------------------------------------------------- |
| [boolean](/reql/functions/type-conversion#reql-func-boolean)     | Converts the argument to boolean   | boolean("") == False                                       |
| [number](/reql/functions/type-conversion#reql-func-number)       | Converts the argument to number    | number("456") == 456                                       |
| [string](/reql/functions/type-conversion#reql-func-string)       | Converts the argument to string    | string(123) == "123"                                       |
| [timestamp](/reql/functions/type-conversion#reql-func-timestamp) | Converts the argument to timestamp | timestamp("2015-06-25T11:08:44Z") == timestamp(1435230524) |

### [Math](/reql/functions/math)

| Name                                          | Description                                                           | Example                 |
| --------------------------------------------- | --------------------------------------------------------------------- | ----------------------- |
| [max](/reql/functions/math#reql-func-max)     | Returns the maximum of _n_ arguments                                  | max(147,42,81.9) == 147 |
| [min](/reql/functions/math#reql-func-min)     | Returns the minimum of _n_ arguments                                  | min(147,42,81.9) == 42  |
| [avg](/reql/functions/math#reql-func-avg)     | Returns the average of _n_ numbers                                    | avg(2, 4) == 3          |
| [round](/reql/functions/math#reql-func-round) | Returns closest integer                                               | round(4.5) == 5         |
| [floor](/reql/functions/math#reql-func-floor) | Returns largest preceding integer                                     | floor(4.3) == 4         |
| [ceil](/reql/functions/math#reql-func-ceil)   | Returns smallest following integer                                    | ceil(4.3) == 5          |
| [abs](/reql/functions/math#reql-func-abs)     | Returns non-negative value of the argument without regard to its sign | abs(-4.3) == 4.3        |
| [sqrt](/reql/functions/math#reql-func-sqrt)   | Returns the square root of the argument                               | sqrt(4) == 2            |
| [pow](/reql/functions/math#reql-func-pow)     | Returns 𝑓𝑖𝑟𝑠𝑡\_𝑎𝑟𝑔𝑢𝑚𝑒𝑛𝑡𝑠𝑒𝑐𝑜𝑛𝑑\_𝑎𝑟𝑔𝑢𝑚𝑒𝑛𝑡    | pow(10,3) == 1000       |
| [log](/reql/functions/math#reql-func-log)     | Returns the logarithm of the argument                                 | log(1000) == 3          |

### [String](/reql/functions/string)

| Name                                            | Description                                        | Example                                              |
| ----------------------------------------------- | -------------------------------------------------- | ---------------------------------------------------- |
| [upper](/reql/functions/string#reql-func-upper) | Converts all letters to the capital letters        | upper("AbCdefG") == "ABCDEFG"                        |
| [lower](/reql/functions/string#reql-func-lower) | Converts all letters to the small letters          | lower("AbCdefG") == "abcdefg"                        |
| [split](/reql/functions/string#reql-func-split) | Splits a string into sub-strings using a separator | split("abc,def,ghi", ",") == \["abc", "def", "ghi"\] |

### [Interactions Listing](/reql/functions/interactions-listing)

| Name                                                                                   | Description                                       | Example                                                      |
| -------------------------------------------------------------------------------------- | ------------------------------------------------- | ------------------------------------------------------------ |
| [user\_interactions](/reql/functions/interactions-listing#reql-func-user-interactions) | Returns list of interactions by a particular user | user\_interactions(context\_user\["userId"\], {"purchases"}) |

### [Interactions Statistics](/reql/functions/interactions-statistics)

| Name                                                                                                | Description                                   | Example                                       |
| --------------------------------------------------------------------------------------------------- | --------------------------------------------- | --------------------------------------------- |
| [num\_item\_bookmarks](/reql/functions/interactions-statistics#reql-func-num-item-bookmarks)        | Returns the number of bookmarks of an item    | num\_item\_bookmarks('itemId') < 10           |
| [num\_item\_detail\_views](/reql/functions/interactions-statistics#reql-func-num-item-detail-views) | Returns the number of detail views of an item | num\_item\_detail\_views('itemId') < 10       |
| [num\_item\_purchases](/reql/functions/interactions-statistics#reql-func-num-item-purchases)        | Returns the number of purchases of an item    | num\_item\_purchases('itemId') < 10           |
| [num\_item\_ratings](/reql/functions/interactions-statistics#reql-func-num-item-ratings)            | Returns the number of ratings of an item      | num\_item\_ratings('itemId', "negative") < 10 |

### [Item Segments Handling](/reql/functions/item-segments)

| Name                                                                    | Description                                 | Example                                          |
| ----------------------------------------------------------------------- | ------------------------------------------- | ------------------------------------------------ |
| [segment\_items](/reql/functions/item-segments#reql-func-segment-items) | Get items in a Segment                      | 'itemId' in segment\_items("genres", "thriller") |
| [item\_segments](/reql/functions/item-segments#reql-func-item-segments) | Get Item Segments to which the item belongs | "thriller" in item\_segments("genres", 'itemId') |

### [Geographical](/reql/functions/geographical)

| Name                                                                     | Description                                                                                                      | Example                                                                                              |
| ------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- |
| [earth\_distance](/reql/functions/geographical#reql-func-earth-distance) | Returns the orthodromic distance between two points (given as their latitude and longitude in degrees) in meters | earth\_distance(50.075538,14.437800,52.520007,13.404954) < 282000                                    |
| [geo\_polygon](/reql/functions/geographical#reql-func-geo-polygon)       | Creates a spherical polygon represented by points with geographical coordinates                                  | geo\_polygon(\[\[40.800, -73.957\], \[40.767, -73.981\], \[40.764, -73.972\], \[40.796, -73.949\]\]) |
| [geo\_radius](/reql/functions/geographical#reql-func-geo-radius)         | Creates a spherical cap represented by a center with geographical coordinates and a radius in km                 | geo\_radius(\[40.689167, -74.044444\], 10)                                                           |