Implement Instant Search in Your React App in 5 Minutes

The following is a guest post by Riccardo Giorato.

Introduction

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 RustAdd to a React app - 图1 (opens new window).

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 video preview of what you will be building:

Prerequisites

Before getting started, ensure that you have NodeAdd to a React app - 图2 (opens new window) already installed on your machine.

You will create the boilerplate code for your React app using the custom project we created for you : https://github.com/Giorat/meili_react_demoAdd to a React app - 图3 (opens new window)

Finally, this tutorial assumes that you are already familiar with ReactAdd to a React app - 图4 (opens new window). If that is not the case, you can check the React Documentation to learn more.

Getting Started

Clone the Repository

  1. git clone https://github.com/Giorat/meili_react_demo.git
  2. 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:

  1. npm run setup_meili

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

  1. docker run -p 7700:7700 -v $(pwd)/data.ms:/data.ms 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.

  1. {
  2. "id": 100013768717,
  3. "name": "Fitness Foldable Shoe Bag",
  4. "url": "https://www.decathlon.com/products/gym-foldable-shoe-bag",
  5. "vendor": "Domyos",
  6. "category": "Sport bag",
  7. "tags": [
  8. "Artistic Gymnastics",
  9. "Boy's",
  10. "CARDIO_FITNESS_ACCESSORIES",
  11. "CARDIO_FITNESS_BAGS",
  12. "CODE_R3: 11782"
  13. ],
  14. "images": "https://cdn.shopify.com/s/files/1/1330/6287/products/sac_20a_20chaussure_20kaki_20_7C_20001_20_7C_20PSHOT_20_490180e6-44e4-4340-8e3d-c29eb70c6ac8.jpg?v=1584683232",
  15. "creation_date": "2020-04-03T15:58:48-07:00",
  16. "price": "2.49"
  17. }

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.

  1. const MeiliSearch = require("meilisearch");
  2. (async () => {
  3. try {
  4. const config = {
  5. host: 'http://127.0.0.1:7700'
  6. };
  7. const meili = new MeiliSearch(config);
  8. await meili.createIndex({ uid: "decathlon", primaryKey: "id" });
  9. } catch (e) {
  10. console.log("Meili error: ", e.message);
  11. }
  12. })();

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:

  1. const MeiliSearch = require("meilisearch");
  2. (async () => {
  3. try {
  4. const config = {
  5. host: 'http://127.0.0.1:7700'
  6. };
  7. const meili = new MeiliSearch(config);
  8. const decathlon = require("./decathlon.json"); // path to json file
  9. const index = await meili.getIndex("decathlon");
  10. await index.addDocuments(decathlon);
  11. } catch (e) {
  12. console.log("Meili error: ", e.message);
  13. }
  14. })();

WARNING

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:

  1. git clone https://github.com/Giorat/meili_react_demo.git
  2. 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.

  1. npx create-react-app meili_react_demo
  2. cd meili_react_demo

Including Tailwind CSS

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

  1. <link href="https://unpkg.com/tailwindcss@^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.

  1. import React, { useState, useEffect } from "react";
  2. // TODO configure the MeiliSearch Client
  3. function App() {
  4. const [searchedWord, setSearch] = useState("dumbell");
  5. const [resultSearch, setResults] = useState([]);
  6. const [resultCards, setCards] = useState([]);
  7. // TODO add function to send searchedWord to MeiliSearch
  8. // TODO add function to parse the JSON object
  9. return (
  10. <div className="mx-auto">
  11. <div class="header font-sans text-white items-center justify-center">
  12. <header class="py-12">
  13. <img
  14. class="h-20 w-auto items-center justify-center p-2 mx-auto"
  15. src="/wide_logo.png"
  16. style={{ filter: "invert(0%)" }}
  17. alt=""
  18. />
  19. <h1 class="flex flex-wrap flex-grow text-3xl w-full justify-center p-4">
  20. Stop looking for an item find it and work hard!
  21. </h1>
  22. <div class="border rounded overflow-hidden w-full flex justify-center mx-auto searchBox mt-6">
  23. <button class="flex items-center justify-center px-4 shadow-md bg-white text-black">
  24. <svg
  25. class="h-4 w-4 text-grey-dark"
  26. fill="currentColor"
  27. xmlns="http://www.w3.org/2000/svg"
  28. viewBox="0 0 24 24"
  29. >
  30. <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" />
  31. </svg>
  32. </button>
  33. <input
  34. type="text"
  35. value={searchedWord}
  36. onChange={(event) => setSearch(event.target.value)}
  37. class="px-6 py-4 w-full text-black"
  38. placeholder="Product, sport, color, …"
  39. />
  40. </div>
  41. </header>
  42. </div>
  43. <div>
  44. <div class="flex flex-wrap searchResults">{resultCards}</div>
  45. </div>
  46. </div>
  47. );
  48. }
  49. 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:

  1. # if you use npm
  2. npm install meilisearch
  1. # if you use yarn
  2. 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

  1. import MeiliSearch from "meilisearch";
  2. const client = new MeiliSearch({
  3. host: "http://127.0.0.1:7700/",
  4. });
  5. 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

  1. useEffect(() => {
  2. // Create an scoped async function in the hook
  3. async function searchWithMeili() {
  4. const search = await index.search(searchedWord);
  5. setResults(search.hits);
  6. }
  7. // Execute the created function directly
  8. searchWithMeili();
  9. }, [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

  1. useEffect(() => {
  2. let arrayItems = [];
  3. for (let i = 0; i < resultSearch.length; i++) {
  4. const product = resultSearch[i];
  5. arrayItems.push(
  6. <div class="flex w-full sm:w-1/2 md:w-1/3 lg:w-1/4 xl:w-1/6 p-3">
  7. <a
  8. href={product.url}
  9. class="flex-1 rounded overflow-hidden shadow-lg"
  10. >
  11. <img
  12. class="w-full h-48 object-cover"
  13. src={product.images}
  14. alt={product.name}
  15. onError={(e)=>{e.target.onerror = null; e.target.src="/wide_logo.png"}}
  16. />
  17. <div class="px-6 py-3">
  18. <div class="font-bold text-sm mb-1 text-gray-600 capitalize">
  19. {product.category}
  20. </div>
  21. <div class="font-bold text-xl mb-2 text-gray-800">
  22. {product.vendor} - {product.name.substr(0, 20)}
  23. </div>
  24. <p class="text-black text-xl font-bold text-base py-2">
  25. $ {product.price}
  26. </p>
  27. </div>
  28. </a>
  29. </div>
  30. );
  31. }
  32. setCards(arrayItems);
  33. }, [resultSearch]);

You can have a look at the full App.jsAdd to a React app - 图7 (opens new window) code here:

You can visit the live application here: https://meili-react-demo.netlify.app/Add to a React app - 图9 (opens new window)

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 displayedAttributesAdd to a React app - 图10 (opens new window) object.

Upload the new Settings to MeiliSearch

  1. const MeiliSearch = require("meilisearch");
  2. (async () => {
  3. try {
  4. const config = {
  5. host: 'http://127.0.0.1:7700'
  6. };
  7. const meili = new MeiliSearch(config);
  8. const index = await meili.getIndex("decathlon");
  9. const newSettings = {
  10. rankingRules: [
  11. "typo",
  12. "words",
  13. "proximity",
  14. "attribute",
  15. "wordsPosition",
  16. "exactness",
  17. "desc(creation_date)"
  18. ],
  19. searchableAttributes: ["name", "vendor", "category", "tags"],
  20. displayedAttributes: [
  21. "name",
  22. "vendor",
  23. "category",
  24. "tags",
  25. "images",
  26. "url"
  27. ]
  28. };
  29. await index.updateSettings(newSettings);
  30. } catch (e) {
  31. console.log("Meili error: ", e.message);
  32. }
  33. })();

Conclusion

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 hereAdd to a React app - 图15 (opens new window).