What is multitenancy?
In Meilisearch, you might have one index containing data belonging to many distinct tenants. In such cases, your tenants must only be able to search through their own documents. You can implement this using tenant tokens.Requirements
- Node.js and a package manager like
npm
,yarn
, orpnpm
- Meilisearch JavaScript SDK
- A Meilisearch server running — see our quick start
- A search API key — available in your Meilisearch dashboard
- A search API key UID — retrieve it using the keys endpoints
Prefer self-hosting? Read our installation guide.
Data models
This guide uses a simple data model to represent medical appointments. The documents in the Meilisearch index will look like this:appointments
index.
Creating a tenant token
The first step is generating a tenant token that will allow a given patient to search only for their appointments. To achieve this, you must first create a tenant token that filters results based on the patient’s ID. Create asearch.js
file and use the following code to generate a tenant token:
patient
field. This means that a patient can only search for their own appointments.
Using the tenant token
Now that you have a tenant token, use it to perform searches. To achieve this, you will need to:- On the server: create an endpoint to send the token to your front-end
- On the client: retrieve the token and use it to perform searches
Serving the tenant token
This guide uses Express.js to create the server. You can installexpress
by running:
server.js
file:
http://localhost:3000/token
that accepts an id
query parameter and returns a tenant token.
Making a search
Now that we have an endpoint, you will use it to retrieve the tenant token in your front-end application. This guide uses InstantSearch.js to create a search interface. You will use CDN links to include InstantSearch.js and the Meilisearch InstantSearch.js connector in your HTML file. Createclient.html
file and insert this code: