Master MeiliSearch: The Ultimate Guide to Fast and Relevant Search

Unlock the Power of MeiliSearch: A Blazing-Fast Search Engine

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.

curl -o blogs.json https://raw.githubusercontent.com/meilisearch/internet-news-data-reader-engagement/master/data/blogs.json

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.

const { MeiliSearch } = require('meilisearch');

async function main() {
  const client = new MeiliSearch({
    host: 'http://localhost:7700',
  });

  const index = await client.index('blogs');
}

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.

curl -X POST 'http://localhost:7700/indexes/blogs/documents' -H 'Content-Type: application/json' --data-binary @blogs.json

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.

const newBlog = {
  id: 1234,
  title: 'New Blog Post',
  author: 'ABC News',
  content: 'This is a new blog post',
};

index.addDocuments([newBlog]).then(response => {
  console.log(response.updateId);
});

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.

index.search('Gregorian').then(response => {
  console.log(response.hits);
});

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.

const updatedBlog = {
  id: 1234,
  title: 'Updated Blog Post',
  author: 'Gregorian',
  content: 'This is an updated blog post',
};

index.addDocuments([updatedBlog]).then(response => {
  console.log(response.updateId);
});

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.

index.updateSettings({
  rankingRules: ['wordsPosition', 'exactness'],
}).then(response => {
  console.log(response);
});

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.

  • Defining stop words: exclude common words like “the” or “a” from your search results
  • Setting synonyms: configure words with similar meanings to return more relevant results
  • Adding facet filters: narrow down search results based on specific attributes like categories or tags

Leave a Reply