# External data

External data lets you add dynamic data to your ranking expressions. External data is provided when reading the feed, so you can provide user-specific data here.

Every `external.*` field that the score expression references needs a default in `ranking.defaults.external`, and creating or updating a feed group or feed view without one fails with `expression references fields without defaults: external.<field>`. The default is used when a read doesn't provide that field. Custom activity fields used in the expression, such as `stock` in the examples below, need a default too.

Fields in `external_ranking` replace the default with the same top-level key. A nested object you provide replaces the whole default object for that key; it is not merged key by key.

## Accessing external data in ranking expressions

| name       | description                                                                                                                                |
| ---------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
| `external` | The `external_ranking` object provided when reading the feed. Nested fields can be accessed with `.` notation, for example: `external.foo` |

```java label="Java"
feeds.createFeedGroup(CreateFeedGroupRequest.builder()
    .id("myid")
    .activitySelectors(List.of(
        ActivitySelectorConfig.builder().type("following").build()))
    .ranking(RankingConfig.builder()
        .type("expression")
        .score("popularity * external.popularity_multiplier + share_count * external.share_multiplier")
        // Required: used when a read doesn't provide these fields
        .defaults(Map.of("external", Map.of(
            "popularity_multiplier", 1,
            "share_multiplier", 1)))
        .build())
    .build()).execute();
```

## Providing external data when reading the feed

```java label="Java"
Feed timeline = new Feed("timeline", "john", feeds);

timeline.getOrCreate(GetOrCreateFeedRequest.builder()
    .userID("john")
    .externalRanking(Map.of(
        "popularity_multiplier", 1, // normal weight for popularity
        "share_multiplier", 100)) // very high boost for share_count
    .build());
```

## Arrays and Maps

Stream provides three powerful functions for working with arrays and maps in ranking expressions. These functions are particularly useful for creating dynamic, personalized ranking algorithms that can respond to external data.

**map_lookup(key, map)**

Looks up a single value from a map by key. If the key doesn't exist, it returns `0.0`. `key` must be a string and `map` an object, otherwise evaluating the expression fails.

```java label="Java"
feeds.createFeedGroup(CreateFeedGroupRequest.builder()
    .id("myid")
    .activitySelectors(List.of(
        ActivitySelectorConfig.builder().type("following").build()))
    .ranking(RankingConfig.builder()
        .type("expression")
        .score("popularity + map_lookup(stock, external.stock_boosts)")
        .defaults(Map.of("stock", "", "external", Map.of("stock_boosts", Map.of())))
        .build())
    .build()).execute();
```

Providing external data when reading the feed

```java label="Java"
Feed timeline = new Feed("timeline", "john", feeds);

timeline.getOrCreate(GetOrCreateFeedRequest.builder()
    .userID("john")
    .externalRanking(Map.of("stock_boosts", Map.of(
        "apple", 0.9,
        "tesla", 0.5,
        "microsoft", 0.3)))
    .build());
```

**sum_map_lookup(keys, map)**

Sums values from a map for multiple keys. Keys must be strings; missing keys and non-numeric values are skipped. This is useful when an activity has multiple tags or categories that should all contribute to the score.

```java label="Java"
feeds.createFeedGroup(CreateFeedGroupRequest.builder()
    .id("myid")
    .activitySelectors(List.of(
        ActivitySelectorConfig.builder().type("following").build()))
    .ranking(RankingConfig.builder()
        .type("expression")
        .score("popularity + sum_map_lookup(stocks, external.stock_boosts)")
        .defaults(Map.of("stocks", List.of(), "external", Map.of("stock_boosts", Map.of())))
        .build())
    .build()).execute();
```

Providing external data when reading the feed

```java label="Java"
Feed timeline = new Feed("timeline", "john", feeds);

timeline.getOrCreate(GetOrCreateFeedRequest.builder()
    .userID("john")
    .externalRanking(Map.of("stock_boosts", Map.of(
        "apple", 10.0,
        "tesla", 5.0,
        "microsoft", 2.0)))
    .build());

// score = popularity + 10.0 + 5.0 = popularity + 15.0 (for activity with stocks: ['apple', 'tesla'])
```

**in_array(needle, haystack)**

Checks if a value exists in an array. Returns `1.0` if found, `0.0` if not found. The comparison is exact, so the string `"1"` doesn't match the number `1`. Because the result is a number, compare it explicitly in conditions, for example `in_array(stock, external.watchlist) == 1`.

```java label="Java"
feeds.createFeedGroup(CreateFeedGroupRequest.builder()
    .id("myid")
    .activitySelectors(List.of(
        ActivitySelectorConfig.builder().type("following").build()))
    .ranking(RankingConfig.builder()
        .type("expression")
        .score("popularity + in_array(stock, external.watchlist) * 10")
        .defaults(Map.of("stock", "", "external", Map.of("watchlist", List.of())))
        .build())
    .build()).execute();
```

Providing external data when reading the feed

```java label="Java"
Feed timeline = new Feed("timeline", "john", feeds);

timeline.getOrCreate(GetOrCreateFeedRequest.builder()
    .userID("john")
    .externalRanking(Map.of("watchlist", List.of("apple", "tesla", "microsoft")))
    .build());

// activities with stocks in the watchlist get a +10 boost, others get no boost.
```

### Practical Examples

**Stock Trading Feed:**

```js label="JavaScript"
score: "popularity + sum_map_lookup(stocks, external.stock_boosts) + in_array(market_sector, external.favorite_sectors) * 5";
```

**Content Categorization:**

```js label="JavaScript"
score: "popularity + in_array(category, external.preferred_categories) * 3 + map_lookup(category, external.category_weights)";
```

**Multi-tag Content:**

```js label="JavaScript"
score: "popularity + sum_map_lookup(tags, external.tag_boosts) + in_array(priority, external.high_priority_tags) * 10";
```

---

For the most recent version of this documentation, visit [https://getstream.io/activity-feeds/docs/java/external-ranking-data/](https://getstream.io/activity-feeds/docs/java/external-ranking-data/).