The following is a guest post by Riccardo Giorato.

The content of this post is outdated. Please refer to the meilisearch-react repository for updated information on how to integrate search capabilities into your React app.


In this quick tutorial, you'll learn how to easily create a search page with instant and reliable results thanks to the power of MeiliSearch.

MeiliSearch is an open source, high-relevancy search engine, built in Rust.

We will cover the basic steps to get the search running and move on to more advanced topics at the end.

For the example, let's recreate a fast and beautiful search experience for a Sport brand. Here is a preview of what you will be building:


Before getting started, ensure that you have Node already installed on your machine.

You will create the boilerplate code for your React app using the custom project we created for you :

Finally, this tutorial assumes that you are already familiar with React. If that is not the case, you can check the React Documentation to learn more.

Getting Started

Clone the Repository

git clone
cd meili_react_demo

Run a new Docker image

If you cloned the repository, to set up the MeiliSearch instance just execute inside the main folder:

npm run setup_meili

If you didn't clone the repo and you want to create directly the Docker instance execute this command:

docker run -p 7700:7700 -v $(pwd)/ getmeili/meilisearch

You will be able to check that MeiliSearch is running by visiting the following URL:

Create an Index in MeiliSearch

An index is an entity in which documents are stored, like an array of objects with some specific settings attached to it and a unique primary key.

You can read more about the properties of indexes in the the MeiliSearch documentation.

In order to create your index, you need to find out what your primary key is. Below is a sample document to add to MeiliSearch.

  "id": 100013768717,
  "name": "Fitness Foldable Shoe Bag",
  "url": "",
  "vendor": "Domyos",
  "category": "Sport bag",
  "tags": [
    "Artistic Gymnastics",
    "CODE_R3: 11782"
  "images": "",
  "creation_date": "2020-04-03T15:58:48-07:00",
  "price": "2.49"

In this document, the field that holds the unique value of the document is the id field. This attribute is called the primary key in MeiliSearch.

You can easily create this Index with a Rest client like Postman but, in this tutorial, you will use the MeiliSearch Javascript SDK to do it directly from node.js.

const MeiliSearch = require("meilisearch");

(async () => {
  try {
    const config = {
      host: ''

    const meili = new MeiliSearch(config);

    await meili.createIndex({ uid: "decathlon", primaryKey: "id" });

  } catch (e) {
    console.log("Meili error: ", e.message);

Index documents

MeiliSearch receives documents in JSON format and stores them for searching purposes. These documents are composed of fields that can hold any type of data.

For this tutorial, you can download this dataset full of sportswear items:

To upload all the objects from this JSON file, use the following script:

const MeiliSearch = require("meilisearch");

(async () => {
  try {
    const config = {
      host: ''

    const meili = new MeiliSearch(config);

    const decathlon = require("./decathlon.json"); // path to json file

    const index = await meili.getIndex("decathlon");

    await index.addDocuments(decathlon);

  } catch (e) {
    console.log("Meili error: ", e.message);

Remember to change the path to your JSON file before running this script!

Prepare the React App

You will use a standard React App that you can create using CRA or simply by cloning this repository:

git clone
cd meili_react_demo

If you prefer to start from an empty app you can create your own using the following command. You can name the application however you desire.

npx create-react-app meili_react_demo
cd meili_react_demo

Including Tailwind CSS

To speed up the styling process, add Tailwind CSS style directly to index.html:

<link href="^1.0/dist/tailwind.min.css" rel="stylesheet">

Configure App.js State

Then, modify the App.js file using this code to set up a simple Search form and a few State variables to handle every aspect of the search.

import React, { useState, useEffect } from "react";

// TODO configure the MeiliSearch Client 

function App() {
  const [searchedWord, setSearch] = useState("dumbell");
  const [resultSearch, setResults] = useState([]);
  const [resultCards, setCards] = useState([]);

  // TODO add function to send searchedWord to MeiliSearch

  // TODO add function to parse the JSON object

  return (
    <div className="mx-auto">
      <div class="header font-sans text-white items-center justify-center">
        <header class="py-12">
            class="h-20 w-auto items-center justify-center p-2 mx-auto"
            style={{ filter: "invert(0%)" }}
          <h1 class="flex flex-wrap flex-grow text-3xl w-full justify-center p-4">
            Stop looking for an item — find it and work hard!
          <div class="border rounded overflow-hidden w-full flex justify-center mx-auto searchBox mt-6">
            <button class="flex items-center justify-center px-4 shadow-md bg-white text-black">
                class="h-4 w-4 text-grey-dark"
                viewBox="0 0 24 24"
                <path d="M16.32 14.9l5.39 5.4a1 1 0 0 1-1.42 1.4l-5.38-5.38a8 8 0 1 1 1.41-1.41zM10 16a6 6 0 1 0 0-12 6 6 0 0 0 0 12z" />
              onChange={(event) => setSearch(}
              class="px-6 py-4 w-full text-black"
              placeholder="Product, sport, color, …"
        <div class="flex flex-wrap searchResults">{resultCards}</div>

export default App;

This code should output this beautiful header with a search form.

Search Results in React

Connecting to MeiliSearch from React using the MeiliSearch Javascript SDK is a simple operation that can be done in just a few steps.

MeiliSearch Client

Install the MeiliSearch SDK:

// if you use npm
npm install meilisearch
// if you use yarn
yarn add meilisearch

Set up the MeiliSearch Client with the server URL. In our case, it was the localhost Docker machine. Finally, load the right Index from the backend.

Replace this comment in App.js by the code snippet below:
"// TODO configure the MeiliSearch Client"

import MeiliSearch from "meilisearch";

const client = new MeiliSearch({
  host: "",

const index = client.getIndex("decathlon");

Send the Search Query

Use an “useEffect” to execute the search of the typed words into MeiliSearch. All the results hits will be set to a simple state variable called “resultsSearch”.

Replace this comment in App.js by the code snippet below:
"// TODO add function to send searchedWord to MeiliSearch"

  useEffect(() => {
    // Create an scoped async function in the hook
    async function searchWithMeili() {
      const search = await;
    // Execute the created function directly
  }, [searchedWord]);

Showcase the Results

Inside a second "useEffect", you will search through the JSON objects returned by MeiliSearch. They will have the same structure than the uploaded JSON objects.

Then, it's time to create a list of cards linking to the products pages.

Replace this comment in App.js by the code snippet below:
"// TODO add function to parse the JSON object "

  useEffect(() => {
    let arrayItems = [];
    for (let i = 0; i < resultSearch.length; i++) {
      const product = resultSearch[i];
        <div class="flex w-full sm:w-1/2 md:w-1/3 lg:w-1/4 xl:w-1/6 p-3">
            class="flex-1 rounded overflow-hidden shadow-lg"
              class="w-full h-48 object-cover"
              onError={(e)=>{ = null;"/wide_logo.png"}}
            <div class="px-6 py-3">
              <div class="font-bold text-sm mb-1 text-gray-600 capitalize">
              <div class="font-bold text-xl mb-2 text-gray-800">
                {product.vendor} - {, 20)}
              <p class="text-black text-xl font-bold text-base py-2">
                $ {product.price}
  }, [resultSearch]);

You can have a look at the full App.js code here:

You can visit the live application here:

Configure the Search even more!

With MeiliSearch, you get a ton of other small options you can fine-tune to improve your Search experience. For advanced exploration, you will need to do a few extra configuration steps.

Search Ranking

Start by changing the search rankings, or more simply, the way MeiliSearch looks through the documents you uploaded to find the references to your search terms inside the rankingRules object. In that case, set the following ranking:

  • "typo"
  • "words"
  • "proximity"
  • "attribute"
  • "wordsPosition"
  • "exactness"
  • "desc(creation_date)"

This configuration is the default one except for the last field which is a custom rule "desc(creation_date)". The latter ranks items by their creation date if all previous values are identical.

Searchable Attributes

Secondly, you have to specify the attributes that MeiliSearch can search from in each document, inside a searchableAttributes object. Here, the configuration is done to search only on name, vendor, category and tags leaving out images or URL.

Displayed Attributes

Lastly, you have to specify the attributes that MeiliSearch can return to the user by the Frontend application with the displayedAttributes object.

Upload the new Settings to MeiliSearch

const MeiliSearch = require("meilisearch");

(async () => {
  try {
    const config = {
      host: ''

    const meili = new MeiliSearch(config);

    const index = await meili.getIndex("decathlon");

    const newSettings = {
      rankingRules: [
      searchableAttributes: ["name", "vendor", "category", "tags"],
      displayedAttributes: [

    await index.updateSettings(newSettings);

  } catch (e) {
    console.log("Meili error: ", e.message);


This quick search wouldn’t be possible without an incredible team that is working on this great project night and day! If you might enjoy contributing to the MeiliSearch family you can jump on these repositories to bring some help, issues or tips and tricks:

General discussion is very welcome on the forum or chat:

And also don’t forget to leave a Star on the main project on Github here.