Displays product recommendations related to specific items using Algolia’s Recommend API.
Usage
import instantsearch from 'instantsearch.js';
import { relatedProducts } from 'instantsearch.js/es/widgets';
const search = instantsearch({
indexName: 'instant_search',
searchClient
});
search.addWidgets([
relatedProducts({
container: '#related-products',
objectIDs: ['product-1']
})
]);
search.start();
Parameters
container
string | HTMLElement
required
CSS selector or HTMLElement to insert the widget.
Array of objectIDs of the items to get related products for.
Number of recommendations to retrieve.
Threshold for the recommendations confidence score (between 0 and 100).
Additional search parameters to send to the Algolia API.
Search parameters to use when not enough recommendations are available.
Whether to escape HTML tags from items string values.
Function to transform the items before rendering.(items: Hit[], { results: RecommendResponse }) => Hit[]
Templates to use for the widget.
Template for each recommendation item. Receives the hit data.
Template to display when there are no results.
Template for the widget header. Receives { items, cssClasses }.
Template to wrap all items. Receives { items, templates, cssClasses }.
CSS classes to add to the widget elements.
CSS class for the root element.
CSS class for the root when there are no results.
CSS class for the list element.
Examples
Basic usage
relatedProducts({
container: '#related-products',
objectIDs: ['product-123'],
limit: 4,
templates: {
item(hit, { html, components }) {
return html`
<div>
<img src="${hit.image}" alt="${hit.name}" />
<h3>${components.Highlight({ hit, attribute: 'name' })}</h3>
<p>$${hit.price}</p>
</div>
`;
}
}
});
relatedProducts({
container: '#related-products',
objectIDs: ['product-123'],
templates: {
header({ items }) {
return `<h2>Related Products (${items.length})</h2>`;
},
item(hit) {
return `
<article>
<img src="${hit.image}" />
<h4>${hit.name}</h4>
<span>$${hit.price}</span>
</article>
`;
}
}
});
With empty state
relatedProducts({
container: '#related-products',
objectIDs: ['product-123'],
templates: {
item(hit) {
return `<div class="product">${hit.name}</div>`;
},
empty() {
return '<p>No related products found.</p>';
}
}
});
relatedProducts({
container: '#related-products',
objectIDs: ['product-123'],
transformItems(items) {
return items.map(item => ({
...item,
priceFormatted: `$${item.price.toFixed(2)}`
}));
},
templates: {
item(hit) {
return `<div>${hit.name} - ${hit.priceFormatted}</div>`;
}
}
});
With threshold
relatedProducts({
container: '#related-products',
objectIDs: ['product-123'],
limit: 6,
threshold: 70, // Only show recommendations with 70%+ confidence
templates: {
item(hit) {
return `<div class="related-item">${hit.name}</div>`;
}
}
});
Multiple source products
relatedProducts({
container: '#related-products',
objectIDs: ['product-123', 'product-456'],
limit: 8,
templates: {
item(hit) {
return `<div class="product">${hit.name}</div>`;
}
}
});
HTML output
<div class="ais-RelatedProducts">
<ol class="ais-RelatedProducts-list">
<li class="ais-RelatedProducts-item">
<!-- Item template content -->
</li>
<!-- More items -->
</ol>
</div>
Notes
- Requires Algolia Recommend to be enabled on your account.
- The widget uses the Related Products model.
- Results are based on product attributes and user behavior.
- Use
threshold to control recommendation quality.