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.
Before you start
Section titled “Before you start”- 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).
1. Install the adapter
Section titled “1. Install the adapter”Install it next to your InstantSearch flavour.
npm i @typemo-org/instantsearch-adapter instantsearch.jspnpm add @typemo-org/instantsearch-adapter instantsearch.jsyarn add @typemo-org/instantsearch-adapter instantsearch.jsbun add @typemo-org/instantsearch-adapter instantsearch.jsnpm i @typemo-org/instantsearch-adapter react-instantsearchpnpm add @typemo-org/instantsearch-adapter react-instantsearchyarn add @typemo-org/instantsearch-adapter react-instantsearchbun add @typemo-org/instantsearch-adapter react-instantsearchnpm i @typemo-org/instantsearch-adapter vue-instantsearchpnpm add @typemo-org/instantsearch-adapter vue-instantsearchyarn add @typemo-org/instantsearch-adapter vue-instantsearchbun add @typemo-org/instantsearch-adapter vue-instantsearchnpm i @typemo-org/instantsearch-adapter @typemo-org/svelte-instantsearch instantsearch.jspnpm add @typemo-org/instantsearch-adapter @typemo-org/svelte-instantsearch instantsearch.jsyarn add @typemo-org/instantsearch-adapter @typemo-org/svelte-instantsearch instantsearch.jsbun add @typemo-org/instantsearch-adapter @typemo-org/svelte-instantsearch instantsearch.js@typemo-org/svelte-instantsearch needs Svelte 5.25 and SvelteKit 2.12 or later.
2. Create the search client
Section titled “2. Create the search client”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.
3. Build the page
Section titled “3. Build the page”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();import { Highlight, Hits, InstantSearch, Pagination, RefinementList, SearchBox} from 'react-instantsearch';
function Hit({ hit, sendEvent }) { return ( <a href={hit.url} onClick={() => sendEvent('click', hit, 'Result clicked')}> <Highlight hit={hit} attribute="title" /> </a> );}
export function Search() { return ( <InstantSearch indexName="your-collection-id" searchClient={searchClient} insights={insights}> <SearchBox /> <RefinementList attribute="brand" /> <Hits hitComponent={Hit} /> <Pagination /> </InstantSearch> );}<script setup>import { AisHighlight, AisHits, AisInstantSearch, AisPagination, AisRefinementList, AisSearchBox} from 'vue-instantsearch/vue3/es';</script>
<template> <AisInstantSearch index-name="your-collection-id" :search-client="searchClient" :insights="insights" > <AisSearchBox /> <AisRefinementList attribute="brand" /> <AisHits> <template #item="{ item }"> <a :href="item.url"><AisHighlight :hit="item" attribute="title" /></a> </template> </AisHits> <AisPagination /> </AisInstantSearch></template><script> import { Highlight, Hits, InstantSearch, Pagination, RefinementList, SearchBox } from '@typemo-org/svelte-instantsearch';</script>
<InstantSearch indexName="your-collection-id" {searchClient} {insights}> <SearchBox /> <RefinementList attribute="brand" /> <Hits> {#snippet hit({ hit, sendEvent })} <a href={hit.url} onclick={() => sendEvent('click', hit, 'Result clicked')}> <Highlight attribute="title" {hit} /> </a> {/snippet} </Hits> <Pagination /></InstantSearch>To render the first page of results on the server, add a +page.server.js next to this page:
import { getServerState } from '@typemo-org/svelte-instantsearch';
import Page from './+page.svelte';
export const load = ({ url }) => getServerState(Page, url);The page that holds <InstantSearch> must not read data for anything else; put other data in a
layout. The other components and connect() for your own UI are in the
package README.
4. Report purchases
Section titled “4. Report purchases”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'});Check it worked
Section titled “Check it worked”- 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.