Skip to main content
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.
objectIDs
string[]
required
Array of objectIDs of the items to get related products for.
limit
number
Number of recommendations to retrieve.
threshold
number
Threshold for the recommendations confidence score (between 0 and 100).
queryParameters
object
Additional search parameters to send to the Algolia API.
fallbackParameters
object
Search parameters to use when not enough recommendations are available.
escapeHTML
boolean
default:"true"
Whether to escape HTML tags from items string values.
transformItems
function
Function to transform the items before rendering.
(items: Hit[], { results: RecommendResponse }) => Hit[]
templates
object
Templates to use for the widget.
cssClasses
object
CSS classes to add to the widget elements.

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>
      `;
    }
  }
});

With header

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>';
    }
  }
});

Transform items

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.

Build docs developers (and LLMs) love