Skip to content

InstantSearch

To build your own search UI from InstantSearch widgets, give InstantSearch Typemo’s search client. The adapter @typemo-org/instantsearch-adapter turns InstantSearch’s requests into Typemo searches and its click and purchase events into Typemo analytics. If your UI already runs on InstantSearch, the search client is the only thing that changes.

  • A collection with documents, and its id.
  • A Read only key from API keys. The key rides in the request URL, so the adapter refuses a Write & Read key.
  • Your site listed under Security: a Read only key answers only there.
  • Every attribute you filter by set as a filter on Search → Setup (Filters).

Install it next to your InstantSearch flavour.

Terminal window
npm i @typemo-org/instantsearch-adapter instantsearch.js

Create it once, outside your components.

import { instantTypemo } from '@typemo-org/instantsearch-adapter';
const { searchClient, insights } = instantTypemo({
apiKey: 'sq_your_read_only_key',
highlightFields: ['title']
});

highlightFields names the text fields that come back highlighted. Every option is in the adapter reference.

Pass searchClient and insights to InstantSearch, with your collection id as the index name.

import instantsearch from 'instantsearch.js';
import { hits, pagination, refinementList, searchBox } from 'instantsearch.js/es/widgets';
const search = instantsearch({ indexName: 'your-collection-id', searchClient, insights });
search.addWidgets([
searchBox({ container: '#searchbox' }),
refinementList({ container: '#brand', attribute: 'brand' }),
hits({
container: '#hits',
templates: {
item: (hit, { html, components }) =>
html`<a href="${hit.url}">${components.Highlight({ hit, attribute: 'title' })}</a>`
}
}),
pagination({ container: '#pagination' })
]);
search.start();

Clicks on results are sent on their own once insights is passed. Send a purchase as a conversion with its prices and currency, so revenue shows in analytics:

sendEvent('conversion', hit, 'Product purchased', {
eventSubtype: 'purchase',
objectData: [{ price: 29, quantity: 1 }],
currency: 'EUR'
});
  • Typing in the search box shows your documents, with the typed words highlighted.
  • A filter lists its values with counts, and picking one narrows the results.
  • The searches you ran appear in Search performance within a few minutes.

Next: sorting, Typemo-only parameters and what the adapter refuses are in the adapter reference.