# Context

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

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

# Context

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

## Context Item Function

Context item function is used in [Recommend Items to Item](/api/recommendations#request-recommend-items-to-item) and [Recommend Item Segments to Item](/api/recommendations#request-recommend-item-segments-to-item)for retrieving property values of the item, that is currently viewed by the user.

### Definition

```
context_item[property_name]
```

| Key            | Type   | Meaning                                                                                          |
| -------------- | ------ | ------------------------------------------------------------------------------------------------ |
| property\_name | string | Name of property to be retrieved. If property of this name does not exist, an error is produced. |

_(Note that Context item function is a bit special function – it takes no arguments and returns a map `name of property -> value` representing the context item. `propertyname` placed in square brackets is the key to this map.)_

### Examples

Consider following sample items.

| name String         | price Number | category String |
| ------------------- | ------------ | --------------- |
| "television-42"     | 369          | "television"    |
| "television-49"     | 449          | "television"    |
| "remote-control-13" | 25           | remote-control  |

#### Example 1

Suppose that the user is currently viewing `television-42`. Then the following expression returns `369`, because that is the price of the context item.

```
context_item["price"]
```

#### Example 2 - Up-Sell

Suppose that the user is currently viewing `television-42`. You can recommend him products from the same category with higher price. The _filter_ would look like this:

```
'price' > context_item["price"] and 'category' == context_item["category"]
```

Considering the sample items above, only `television-49` will pass the filter.

If you don’t want to be so restrictive about the cheaper products, you shall use _booster_ instead of _filter_ and boost the products with higher price. The _booster_ could look like this:

```
if 'category' != context_item["category"] then 0.1 else if  'price' > context_item["price"] then 1 else 0.5
```

## Context User Function

Context user function is used for retrieving property values of the user that the recommendations are for.

### Definition

```
context_user[property_name]
```

| Key            | Type   | Meaning                                                                                          |
| -------------- | ------ | ------------------------------------------------------------------------------------------------ |
| property\_name | string | Name of property to be retrieved. If property of this name does not exist, an error is produced. |

_(Note that Context user function is a bit special function – it takes no arguments and returns a map `name of property -> value` representing the context user. `propertyname` placed in square brackets is the key to this map.)_

### Example

Consider following sample users, which have specified languages they understand:

| userId String | languages Set  |
| ------------- | -------------- |
| "user-27"     | \["EN"\]       |
| "user-29"     | \["EN", "FR"\] |

And sample items, which are some movies:

| itemId String                         | language String |
| ------------------------------------- | --------------- |
| "Pulp Fiction"                        | "EN"            |
| "Le fabuleux destin d Amelie Poulain" | "FR"            |
| "Fight Club"                          | "EN"            |
| "Kolja"                               | "CS"            |

Suppose that I want to recommend movies to users, but only movies that the users can understand. I can use the following filter:

```
'language' in context_user["languages"]
```

For _user-27_ (which can speak only english) _Pulp Fiction_ or _Fight Club_ can be recommended.

For _user-29_ (which can speak english and french) _Pulp Fiction, Fight Club_ or _Le fabuleux destin d Amelie Poulain_ can be recommended.

## Context Segment Function

Context Segment function is used in [Recommend Item Segments to Item Segment](/api/recommendations#request-recommend-item-segments-to-item-segment)for retrieving the [Item Segment](/segmentations) to which is being recommended.

### Definition

```
context_segment[property_name]
```

| Key            | Type   | Meaning                                                                  |
| -------------- | ------ | ------------------------------------------------------------------------ |
| property\_name | string | Name of property to be retrieved. Currently only segmentId is supported. |

### Examples

```
// If the context Segment is "fairy tales"
// then explicitly disallow "tabloid news" Segment to be ever recommended

if context_segment["segmentId"] == "fairy tales"
	then 'segmentId' != "tabloid news"
	else true
```