Unlock the Power of MeiliSearch: A Blazing-Fast Search Engine
MeiliSearch is more than just a search engine – it’s a highly customizable search engine API that offers unparalleled flexibility. With MeiliSearch, you can modify ranking rules, add custom ranking rules, configure synonyms, filter out stop words, and more. In this tutorial, we’ll explore how to interact with a MeiliSearch instance using the MeiliSearch JS client, perfect for any frontend project that requires fast searching capabilities.
Getting Started with MeiliSearch
To begin, make sure you have an active Node.js installation and a tool like cURL to send API requests. You can run a MeiliSearch instance using the MeiliSearch sandbox, deploy an instance using a Digital Ocean droplet, run MeiliSearch using Docker, or install it on Debian, Ubuntu, Linux, or Mac OS. Don’t forget to set a master key to secure your instance’s API endpoints.
MeiliSearch Project Setup
Create a new project using npm and add the meilisearch-js dependency. Then, create an index.js file that contains all your code. For this tutorial, we’ll use a modified dataset titled “Internet news data with readers engagement” from Kaggle. You can download the dataset on GitHub or use the command below to download it directly.
Creating the Blogs Index
To create a blogs index, upload your blogs.json data to the index so you can later search or modify the data. Use the meilisearch package at the top of your index.js file and create a main function that allows you to use the async/await syntax. Establish a connection with your MeiliSearch instance and create an index. The client object exposes all methods for interacting with the API of your MeiliSearch instance.
Uploading a Dataset to MeiliSearch
The quickest way to upload a large dataset to your MeiliSearch instance is by using a tool like cURL. Execute the command in the directory that contains the blogs.json dataset, paying attention to upload the data to the right index: /indexes/blogs/. Verify whether your data has been uploaded successfully by listing your indexes again.
Adding Documents to MeiliSearch
Add a new document to your blogs.json dataset using the MeiliSearch JavaScript client. Define a new fictive blog created by ABC News and add it to your MeiliSearch instance using the addDocuments method. The addDocuments function returns a JSON object with the updateId property when the documents have been added successfully.
Searching for Documents in MeiliSearch
Use the search method to retrieve your newly added document. The returned result contains several properties: hits, nbHits, processingTimeMs, and query. Try querying for the document with author Gregorian to see the search results.
Modifying Documents with MeiliSearch
Update the blog created by author Gregorian by changing some of its values. Since MeiliSearch has automatically assigned the primary key to the id field, you need to send the id field with your request. Use the same addDocuments function for updating documents.
Modifying MeiliSearch Ranking Rules
MeiliSearch applies ranking rules in a default order, which can be changed in the settings. You can add or remove rules and change their order of importance. Let’s take a look at wordsPosition and exactness ranking rules. Change the ranking rules order to wordsPosition and then to exactness to see the differences in search results.
Explore More with MeiliSearch
In this tutorial, we’ve demonstrated how to add, update, and query for documents using MeiliSearch. We’ve also introduced you to the concept of ranking rules. There’s so much more to explore, such as defining stop words, setting synonyms, or adding facet filters to index your data. Get started with LogRocket’s modern error tracking in minutes and take your application to the next level.