Personalization / AI Personalization (beta) / Implement personalization / Guides

Applying contextual affinities

AI Personalization is a beta feature according to Algolia’s Terms of Service (“Beta Services”).

Contextual affinities are attributes that are relevant to your users only in specific contexts. They ensure that certain preferences are applied only where they make sense, enhancing the relevance and accuracy of personalized search results.

For example, if a user buys white furniture, personalized search results could guide users to other white furniture items. However, personalized search results for unrelated categories shouldn’t be based on the user’s color preferences. Preferring white furniture doesn’t mean they prefer white hardware tools.

By applying contextual affinities, personalization becomes more precise and effective, providing your users with search results that truly match their interests and needs in the right context.

Preparing your index

To enable contextual affinities, you can add a new attribute to your index manually or automate the process with a script. This attribute stores context-specific details for each record, allowing for more relevant search results based on the user’s situation. The following example adds hierarchicalAffinities as a contextual affinity attribute:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[
  {
    "objectID": "0000001",
    "name": "Chair",
    "categories": {
      "lvl0": "Hard Goods",
      "lvl1": "Hard Goods > Desk Chairs",
    },
    "brand": "Nolan",
    "price": 10.99,
    "color": "White",
    "hierarchicalAffinities": ["Hard Goods > Desk Chairs > White"],
  },
  {
    "objectID": "0000002",
    "name": "iPhone 15",
    "categories": {
      "lvl0": "Hard Goods",
      "lvl1": "Hard Goods > Cell Phones",
      "lvl2": "Hard Goods > Cell Phones > Phones & Tablets",
    },
    "brand": "Apple",
    "price": 699.99,
    "color": "Gold",
    "hierarchicalAffinities": ["Hard Goods > Cell Phones > Gold", "Apple > Gold"],
  }
]

The first record is tagged with Hard Goods > Desk Chairs > White. This approach ensures that user preferences, such as a liking for white chairs, are taken into account only when recommending desk chairs.

In the second record, the hierarchicalAffinities attribute is more flexible. It categorizes the iPhone 15 under “Hard Goods” and “Cell Phones” specifying its color as “Gold” with Hard Goods > Cell Phones > Gold and also includes a brand-specific categorization under Apple > Gold. This approach lets you capture detailed preferences like color and brand affinity within specific contexts.

Adding a contextual affinity to your configuration

Once the contextual affinity has been added to all records in your index, the next step is to select it as an affinity in the AI Personalization dashboard Configuration page.

Here, you can streamline your configuration by consolidating attributes. For example, instead of having a separate color or brand attribute in your setup, you can opt for a hierarchicalAffinities attribute which contains context-specific information about color and brand.

Did you find this page helpful?