Quick start
This quick start will walk you through setting up Meilisearch, adding documents, performing your first search, using the search preview, adding a search bar, and securing your instance.
You will need:
- A command line for installation
- Some way to interact with Meilisearch afterwards (for example, cURL or one of our SDKs)
Let's get started!
Setup and installation
We'll start with downloading and installing Meilisearch:
# Install Meilisearch
curl -L https://install.meilisearch.com | sh
# Launch Meilisearch
./meilisearch
You have the option to install Meilisearch locally or deploy it over a cloud service. Learn more about the other installation options in our installation guide.
NOTE
If you started Meilisearch with a master key or using Meilisearch Cloud, you will need to add the -H Authorization: Bearer API_KEY
header to all your curl commands.
Running Meilisearch
On successfully running Meilisearch, you should see the following response:
888b d888 d8b 888 d8b 888
8888b d8888 Y8P 888 Y8P 888
88888b.d88888 888 888
888Y88888P888 .d88b. 888 888 888 .d8888b .d88b. 8888b. 888d888 .d8888b 88888b.
888 Y888P 888 d8P Y8b 888 888 888 88K d8P Y8b "88b 888P" d88P" 888 "88b
888 Y8P 888 88888888 888 888 888 "Y8888b. 88888888 .d888888 888 888 888 888
888 " 888 Y8b. 888 888 888 X88 Y8b. 888 888 888 Y88b. 888 888
888 888 "Y8888 888 888 888 88888P' "Y8888 "Y888888 888 "Y8888P 888 888
Database path: "./data.ms"
Server listening on: "localhost:7700"
Congratulations! You're ready to move on to the next step!
Add documents
For this quick start, we will be using a collection of movies as our dataset. To follow along, first click this link to download the file: movies.json. Then, move the downloaded file into your working directory.
Open a new terminal window and run the following command:
curl \
-X POST 'http://localhost:7700/indexes/movies/documents?primaryKey=id' \
-H 'Content-Type: application/json' \
--data-binary @movies.json
Meilisearch stores data in the form of discrete records, called documents. Documents are grouped into collections, called indexes.
NOTE
Meilisearch currently only accepts data in JSON, NDJSON, and CSV formats. You can read more about this in our documents guide.
The previous command added documents from movies.json
to a new index called movies
and set id
as the primary key. If it isn't set manually, Meilisearch infers it from your dataset.
Every index must have a primary key, an attribute shared across all documents in that index. If you try adding documents to an index and even a single one is missing the primary key, none of the documents will be stored.
Meilisearch combines consecutive document requests into a single batch and processes them together. This process is called auto-batching, and it significantly speeds up indexing. After adding documents, you should receive a response like this:
{
"taskUid": 0,
"indexUid": "movies",
"status": "enqueued",
"type": "documentAdditionOrUpdate",
"enqueuedAt": "2021-08-11T09:25:53.000000Z"
}
Most database operations in Meilisearch are asynchronous. This means that rather than being processed instantly, API requests are added to a queue and processed at a later time.
Use the returned taskUid
to check the status of your documents:
curl \
-X GET 'http://localhost:7700/tasks/0'
If the document addition is successful, the response should look like this:
{
"uid": 0,
"indexUid": "movies",
"status": "succeeded",
"type": "documentAdditionOrUpdate",
"canceledBy": null,
"details":{
"receivedDocuments": 19547,
"indexedDocuments": 19547
},
"error": null,
"duration": "PT0.030750S",
"enqueuedAt": "2021-12-20T12:39:18.349288Z",
"startedAt": "2021-12-20T12:39:18.352490Z",
"finishedAt": "2021-12-20T12:39:18.380038Z"
}
If the status
field has the value enqueued
or processing
, all you have to do is wait a short time and check again. Proceed to the next step once the task status
has changed to succeeded
.
Search
Now that you have Meilisearch set up, you can start searching!
curl \
-X POST 'http://localhost:7700/indexes/movies/search' \
-H 'Content-Type: application/json' \
--data-binary '{ "q": "botman" }'
In the above code sample, the parameter q
represents the search query. The documents you added in the previous step will be searched for text that matches botman
.
Meilisearch response:
{
"hits": [
{
"id": 29751,
"title": "Batman Unmasked: The Psychology of the Dark Knight",
"poster": "https://image.tmdb.org/t/p/w1280/jjHu128XLARc2k4cJrblAvZe0HE.jpg",
"overview": "Delve into the world of Batman and the vigilante justice tha",
"release_date": "2008-07-15"
},
{
"id": 471474,
"title": "Batman: Gotham by Gaslight",
"poster": "https://image.tmdb.org/t/p/w1280/7souLi5zqQCnpZVghaXv0Wowi0y.jpg",
"overview": "ve Victorian Age Gotham City, Batman begins his war on crime",
"release_date": "2018-01-12"
},
…
],
"estimatedTotalHits": 66,
"query": "botman",
"limit": 20,
"offset": 0,
"processingTimeMs": 12
}
By default, Meilisearch only returns the first 20 results for a search query. This can be changed using the limit
parameter.
To search on multiple indexes at the same time with a single request, use the /multi-search
endpoint.
Search preview
Meilisearch offers a browser-based search preview where you can search through a selected index. You can access it any time Meilisearch is running at http://localhost:7700
.
For security reasons, the search preview is only available in development mode.
If you have multiple indexes, you can switch between them using the indexes dropdown.
Customization
At this point, you can configure your entire Meilisearch instance, customize your indexes, and refine your results using:
Front-end integration
The only step missing now is adding a search bar to your project. The easiest way of achieving this is to use instant-meilisearch: a plugin that establishes communication between your Meilisearch instance and InstantSearch. InstantSearch, an open-source project developed by Algolia, is the tool that renders all the components needed to start searching.
The following code sample uses plain JavaScript.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@meilisearch/instant-meilisearch/templates/basic_search.css" />
</head>
<body>
<div class="wrapper">
<div id="searchbox" focus></div>
<div id="hits"></div>
</div>
</body>
<script src="https://cdn.jsdelivr.net/npm/@meilisearch/instant-meilisearch/dist/instant-meilisearch.umd.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/instantsearch.js@4"></script>
<script>
const search = instantsearch({
indexName: "movies",
searchClient: instantMeiliSearch(
"http://localhost:7700"
)
});
search.addWidgets([
instantsearch.widgets.searchBox({
container: "#searchbox"
}),
instantsearch.widgets.configure({ hitsPerPage: 8 }),
instantsearch.widgets.hits({
container: "#hits",
templates: {
item: `
<div>
<div class="hit-name">
{{#helpers.highlight}}{ "attribute": "title" }{{/helpers.highlight}}
</div>
</div>
`
}
})
]);
search.start();
</script>
</html>
Here's what's happening:
- The first four lines of the
<body>
add two container elements:#searchbox
and#hits
.instant-meilisearch
creates the search bar inside#searchbox
and lists search results in#hits
- The first two
<script src="…">
tags import libraries needed to runinstant-meilisearch
- The third and final
<script>
tag is where you customizeinstant-meilisearch
Let's try it!
- Create an empty file and name it
index.html
- Open it in a text editor like Notepad, Sublime Text, or Visual Studio Code
- Copy-paste one of the code samples above—either vanilla JavaScript, Vue 2, or React— and save the file
- Open
index.html
in your browser by double-clicking it in your folder
You should now have a working front-end search interface 🚀🔥
Securing Meilisearch
The Meilisearch API is unprotected by default, making all routes publicly accessible. You can set a master key of at least 16 bytes to protect your instance from unauthorized use:
./meilisearch --master-key="MASTER_KEY"
When you launch your Meilisearch instance with a master key, two things happen:
- Your Meilisearch instance is now protected. Aside from the get health endpoint, all subsequent API requests must include a valid API key for authorization
- Two default API keys are automatically generated
Here's how to use the master key you set to get all keys:
# replace the MASTER_KEY placeholder with your master key
curl \
-X GET 'http://localhost:7700/keys' \
-H 'Authorization: Bearer MASTER_KEY'
The master key should only be used for retrieving and managing API keys. For regular API calls, such as search, use an API key:
curl \
-X POST 'http://localhost:7700/indexes/movies/search' \
-H 'Authorization: Bearer API_KEY'
WARNING
Accessing the /keys
route without setting a master key will return an error.
To learn more about key management, refer to our dedicated guide.
What's next?
You now know all the basics: how to install Meilisearch, create an index, add documents, check the status of an asynchronous task, and perform a search.
To keep going, continue to the Meilisearch 101 for a guided overview of the main features, or check out the API references to dive right in!