Item variations
In our example, we sell a t-shirt or sweatshirt in many colours. The simplest solution is to have one entry per item and a list of possible colour variants. This will, however, cause a problem when a customer searches for a "red t-shirt" - Algolia will display all collours of our t-shirt (that have a red variation).
To avoid this confusion, make sure that searches for "red t-shirt" only display the red version of any t-shirt (while still finding a way to show all variants).
Dataset example
In our inventory, we have two t-shirt (A and B) and two sweatshirts (C and D). Each SKU comes in several variants.

In our dataset, we can represent them by creating one record for each colour variant of each item. Each record specifies the type, the sku (base), the colour, and the associated thumbnail. Here’s what our records look like:
[
{
"type": "t-shirt",
"sku": "B",
"colour": "blue",
"thumbnail_url": "tshirt-B-blue.png"
},
{
"type": "sweatshirt",
"sku": "C",
"colour": "red",
"thumbnail_url": "sweatshirt-C-red.png"
},
...
]
Going further, we can add all the possible colour variations for each record. This way, we can display all the variants for a single product (for example, colour swatches under the thumbnail), allowing the customer to discover them.
[
{
"type": "t-shirt",
"sku": "B",
"colour": "blue",
"thumbnail_url": "tshirt-B-blue.png",
"color_variants": ["orange", "teal", "yellow", "red", "green"]
},
{
"type": "t-shirt",
"sku": "B",
"colour": "orange",
"thumbnail_url": "tshirt-B-orange.png",
"color_variants": ["blue", "teal", "yellow", "red", "green"]
},
...
]
With this approach, every record represents a single variation, which ensures always displaying consistent data. One record per variation lets us add granular custom ranking attributes, like number_of_sales to rank variants accordingly.
Note we can use Algolia’s distinct feature to deduplicate skus (base). This way, when someone searches for “jersey”, they only get one from each sku (base).
Using the API
At indexing time
Before deduplicating items, restrict what attributes are searchable. We don’t want to search thumbnail_url, which may be irrelevant and add noise, nor into colour_variants, because it could lead to false positives. Therefore, we can set sku, type, and colour as searchableAttributes.
To use distinct, we first need to set base_sku as attributeForDistinct during indexing time. Only then can we set distinct to true to deduplicate results. Note that setting distinct at indexing time is optional. If we want to, we can set it at query time instead.
At query time
Once attributeForDistinct is set, we can enable distinct by setting it to true.
Using the dashboard
We can also set our attribute for distinct and enable distinct in the Algolia dashboard.
- Go to your dashboard, then go to the Search product and select your index.
- Click on the Configuration tab.
- In the Searchable Attributes section, click the “Add a searchable attribute” button.
- Select the
sku,type, andcolourattributes in the drop-down menu one after another. - Click the Deduplication and Grouping tab, which you can find under Search behavior.
- Set the Distinct option to true.
- Set the Attribute for Distinct option to design.
- Save your changes.
When distinct is true, you get one color for each design. To control which one, you can set a new attribute with business metrics, for example, number_of_sales, and set it up for custom ranking.
