-
Notifications
You must be signed in to change notification settings - Fork 64
Description
Context
instant-meilisearch
is an adapter between instantsearch
and algoliasearch.js
.
The way it works is that instantsearch
provides search requests compatible
with the search method of algoliasearch.js. It then expects a search response in the format that is returned by the search method of algoliasearch.js.
The role of instant-meilisearch
is to transform the Algolia search requests into meilisearch search requests and then transform the meilisearch search response into an algoliasearch response format.
Problem
Since instant-meilisearch
is the adapter of algolia, instant-meilisearch
is used by autocomplete-client
for the same purpose as described above: adapting the search request and response. This constitute the first problem, as instant-meilisearch also provides the possibility to send configurations specifically related to instant-meilisearch which are not relevant to autocomplete-client and autocomplete-client user agent always contains instant-meilisearch
in its agents.
The second issue lies when the user wants to use instantsearch
in a SSR environment. Typically, a user might want to create a first back-end search and build the website with the returned hits. By doing so, the first hits are referenced on the different search engines (SEO) and the initial load of the hits is way faster. This is a common implementation on e-commerce websites.
Currently, this is only possible if the user creates an algolia search request. Which means, using the search request parameters of algolia.
For example:
The user wants to build its website with an initial search. He needs to do the following:
const instantMeilisearchClient = instantMeilisearch('host', 'apiKey');
const algoliaSearchParams = {
"indexName": "games",
"params": {
"facets": [
"genres"
],
"hitsPerPage": 6,
"page": 0,
"query": "",
}
}
const response = instantMeilisearchClient.search([algoliaSearchParams]);
// use response as initial search in the instantSearch instance
As you can see, the search request was made using the algoliasearch
format and not the meilisearch
format. It would have been more intuitive if the user could make a request with Meilisearch search parameters:
const searchParams = {
indexUid: "games"
facets: [
"genres"
],
q: "",
// etc...
}
Solution
To be able to provide this intuitive SSR experience, we need to extract two parts of instant-meilisearch
. The algolia-search-request
adapter and the algolia-search-response
adapter.
By doing so, the user can now create a SSR initial search with Meilisearch search parameters:
import { algoliaSearchResponseClient} from '@meilisearch/algolia-search-response'
const searchClient = algoliaSearchResponseClient('host', 'apiKey')
const searchParams = {
indexUid: "games"
facets: [
"genres"
],
q: "",
// etc...
}
const algoliaSearchResponse = await searchClient.search([searchParams]); // search response in an algoliasearch.js format
// use response as initial search in the instantSearch instance
By using the searchRequest
adapter, autocomplete can also remove instant-meilisearch
from its user-agents.
// in autocomplete sources
import { algoliaSearchRequestClient } from '@meilisearch/algolia-search-request'
const searchClient = algoliaSearchRequestClient('host', 'apiKey', { userAgent: ['autocomplete (vx.x.x)'] })
const algoliasearchParams = {
indexName: "games",
params: { query: "test" }
}
const meilisearchResponse = await searchClient.search([algoliaSearchParams])
In the above example, the user agent sends to Meilisearch the engine are now
meilisearch js (vx.x.x); autocomplete-client (v.x.x.x)
instead of
meilisearch js (vx.x.x); instant-meilisearch (vx.x.x) autocomplete-client (v.x.x.x)
Design choices.
How should we name both packages?
@meilisearch/algolia-search-request-adapter
and@meilisearch/algolia-search-response-adapter
?
How should we name the methods that are served by these packages?
algoliaSearchRequestClient
andalgoliaSearchResponseClient
?
Todo
- extract [search-request-adapter](https://github.com/meilisearch/meilisearch-js-plugins/tree/main/packages/instant-meilisearch/src/adapter/search-request-adapter] to its own
@meilisearch/algolia-search-request-adapter
package - extract search-response-adapter to its own
@meilisearch/algolia-search-response-adapter
package. - Handle pagination
- Handle sorting adapter