InstantSearch Adapter
This guide shows you how to use the @elasticpath/catalog-search-instantsearch-adapter package to build search experiences in your storefront using the popular InstantSearch.js library.
The adapter translates InstantSearch.js requests into Catalog Search API requests and transforms the responses back into InstantSearch format, giving you access to a rich ecosystem of pre-built search widgets.
Before using the adapter, ensure you have Catalog Search enabled for your store. See Catalog Search in Commerce Manager to get started.
Features
- Seamless integration with InstantSearch.js widgets
- Full-text search with highlighting
- Faceted search and filtering
- Pagination and result sorting
- Support for both vanilla JavaScript and React InstantSearch
Installation
Install the adapter along with the Elastic Path SDK and InstantSearch.js:
npm install @elasticpath/catalog-search-instantsearch-adapter @epcc-sdk/sdks-shopper instantsearch.js
For React applications:
npm install @elasticpath/catalog-search-instantsearch-adapter @epcc-sdk/sdks-shopper react-instantsearch instantsearch.js
Basic usage
Configure the client
First, configure the Elastic Path shopper client with your credentials:
import { configureClient, client } from '@epcc-sdk/sdks-shopper';
configureClient(
{
baseUrl: 'https://euwest.api.elasticpath.com',
},
{
clientId: 'YOUR_CLIENT_ID',
storage: 'localStorage', // Optional: persist tokens
}
);
Create the adapter
Create an instance of the adapter with your configured client:
import CatalogSearchInstantSearchAdapter from '@elasticpath/catalog-search-instantsearch-adapter';
const catalogSearchAdapter = new CatalogSearchInstantSearchAdapter({
client: client,
});
Initialize InstantSearch
Use the adapter's searchClient property when initializing InstantSearch:
import instantsearch from 'instantsearch.js';
const search = instantsearch({
indexName: 'search',
searchClient: catalogSearchAdapter.searchClient,
});
// Add widgets
search.addWidgets([
instantsearch.widgets.searchBox({
container: '#searchbox',
}),
instantsearch.widgets.hits({
container: '#hits',
}),
instantsearch.widgets.refinementList({
container: '#brand-list',
attribute: 'extensions.products(product_brands).name',
}),
]);
search.start();
React integration
For React applications, use the react-instantsearch package:
import React from 'react';
import {
InstantSearch,
SearchBox,
Hits,
RefinementList,
Pagination,
} from 'react-instantsearch';
import CatalogSearchInstantSearchAdapter from '@elasticpath/catalog-search-instantsearch-adapter';
import { configureClient, client } from '@epcc-sdk/sdks-shopper';
// Configure authentication (typically done once in your app initialization)
configureClient(
{
baseUrl: 'https://euwest.api.elasticpath.com',
},
{
clientId: 'YOUR_CLIENT_ID',
storage: 'localStorage',
}
);
// Create the adapter
const catalogSearchAdapter = new CatalogSearchInstantSearchAdapter({
client: client,
});
function Search() {
return (
<InstantSearch
indexName="search"
searchClient={catalogSearchAdapter.searchClient}
>
<SearchBox />
<div className="search-panel">
<div className="search-panel__filters">
<RefinementList attribute="extensions.products(product_brands).name" />
</div>
<div className="search-panel__results">
<Hits />
<Pagination />
</div>
</div>
</InstantSearch>
);
}
Index name configuration
The indexName parameter determines which Elastic Path search endpoint to use:
| Index Name | Usage |
|---|---|
search | Regular product search (required) |
autocomplete | Autocomplete and suggestions |
These values map directly to the type parameter in Elastic Path's Catalog Search API.
Supported widgets
The adapter supports most InstantSearch widgets out of the box.
Search widgets
searchBox- Basic search inputautocomplete- Search with suggestions (use withindexName: 'autocomplete')voiceSearch- Voice input support
Results widgets
hits- Display search resultsinfiniteHits- Infinite scrolling resultshighlight- Highlight matching termssnippet- Show text snippets
Filtering widgets
refinementList- Filter by facet valueshierarchicalMenu- Category navigationrangeSlider/rangeInput- Numeric range filterstoggleRefinement- Boolean filtersclearRefinements- Clear all filterscurrentRefinements- Show active filters
Pagination widgets
pagination- Page navigationhitsPerPage- Results per page selector
Metadata widgets
stats- Search statisticsbreadcrumb- Navigation breadcrumb
Widgets with limitations
geoSearch- Not supported (Catalog Search does not support geo-search)queryRuleCustomData- Limited supportsortBy- Useconfigurewidget withadditionalSearchParameters.sort_by
Widget examples
Hierarchical categories
instantsearch.widgets.hierarchicalMenu({
container: '#categories',
attributes: [
'categories.lvl0',
'categories.lvl1',
'categories.lvl2',
],
});
Price range filter
instantsearch.widgets.rangeSlider({
container: '#price-range',
attribute: 'price.float_price',
});
Searchable refinement list
instantsearch.widgets.refinementList({
container: '#brands',
attribute: 'extensions.products(product_brands).name',
limit: 10,
showMore: true,
showMoreLimit: 50,
searchable: true,
searchablePlaceholder: 'Search brands',
});
Union search
The adapter supports union search to query multiple collections simultaneously:
const adapter = new CatalogSearchInstantSearchAdapter({
client: client,
union: true,
});
Migrating from Algolia
If you're migrating from Algolia to Elastic Path Catalog Search, most of your InstantSearch implementation will work without changes:
- Replace the Algolia search client with this adapter
- Update attribute names to match your Elastic Path catalog structure
- Configure authentication using the Elastic Path SDK instead of Algolia API keys
Related guides
- Catalog Search Overview - Enable and configure Catalog Search
- Indexing - Configure searchable fields
- Profiles - Set up search profiles
- Setting Up Catalog Search (API) - API-based configuration
See the complete InstantSearch example on GitHub for a full implementation.