Utilize the power of Meilisearch to Power your Web-based application (r)

Jan 12, 2024

-sidebar-toc>

From ecommerce platforms From e-commerce platforms Content Management Systems (CMSs) Web applications create and manage massive amounts of information. Making sense of the information efficiently is essential to ensure a smooth user experience. So, traditional search functionality that is based on literal word-for match-ups is not enough. It's essential to utilize a full-text search.

A full-text search analyzes every word of databases or documents. This allows you to find relevant data from massive data sets based on certain keywords or phrases. It accounts for factors like the frequency of usage and the multilingual content. This results in more accurate and more comprehensive results for searches.

Meilisearch is the leader within the category of search engines, harnessing the power of full-text search to provide users with a flexible and efficient device that was designed with designers and consumers in mind.

This tutorial video demonstrates how you can incorporate Meilisearch within the Node.js web application.

What exactly is Meilisearch?

Meilisearch also has a wide variety of options for customization, with many options to adjust the relevancy of the results. One of these features most prominent feature is the ranking rule that you are able to modify to fit your needs.

The requirements

To follow along to follow along, you'll need:

How To Set Up Meilisearch

  1. Log on to Meilisearch Cloud and either create an account or log in. Ensure you confirm your email address.
  2. After that, click to make a new project or server, which is a server operating an instance of Meilisearch -- in which you'll include your website's information.
  3. Add a name for the project (for example the word book-app) (or book-app) and then select the desired area. Then, click Create . After creating your project, you'll be able to click Settings for more information about the project you created, such as your URL to access the data sources, your API keys for safeguarding your Meilisearch server as well as additional details.
The book-app project overview page
The book-app overview website for the project.

There are three API keys accessible Each key represents a specific access level:

  • Master Key Master Keys unlock all routes and is the only key having access to endpoints that allow for generating and editing API keys. Only use the master key to manage API keys in a secure environment.
  • It is the standard Search API key -It only gives access to the search route. It can be used to access the client-side code.
  • Key for an default Admin API This key grants access to all API route, with the exception of keys that is utilized to make and remove API keys. The key is only used key in a secure setting.

How do I Index Data using Meilisearch

Indexes are the main components that store and organize searchable information. They serve as containers to store documentswhich are objects that contain one or more fields.

Every index available on Meilisearch is completely customizable and independent that allows you to design your own filters and rules to rank. choices.

What Do I Need to Know To Make an Index as well as add documents

  1. Inside the navigation bar, click to open to open the Indexestab in your project.
  2. Click Create an Index. Then, type in the name of your index (for for example book, a title)) then click Create index.
  3. Choose the best way to transfer your files. This guide will help you upload an JSON file. It has 13 entries about books using the Google Book API. Google Book API.
  4. Select to upload your file and then add the JSON file. Then, click Import Documents.

How Do I Update or Delete Documents

Meilisearch Cloud currently doesn't include the capability to modify or delete files, but you could use the REST API route or the SDK. This program demonstrates how you can edit or delete documents making use of documents via the REST API ends. This tutorial makes use of cURL for working with routes, but you can use an API platform, like Postman.

  1. If you are interested in updating documents , send an PUT The following routes can be used to submit your request:
/indexes/index_uid/documents

Index_uid above is the index name of your project. index_uid above is the index name of your project.

Highlighted index name
Index name highlighted.
  1. By using this method it is possible to add or edit a collection of documents, even though they aren't there. To update documents you'd like to upgrade, add its principal key. Documents that are older receive an insufficient update which relies on information included in the current document. The document updated preserves the fields which were not included in the latest document. This allows the fields to remain unaltered.Below are a few examples of ways you can update the name of a document inside the index of a book. JavaScript for kids up to JavaScript Coding kids and also add a publisher field:
curl \ -X PUT '/indexes/books/documents' \ -H 'Content-Type: application/json' \ -H 'Authorization: Bearer ' \ --data-binary '[ "id": "71nDBQAAQBAJ", "title": "JavaScript Coding for Kids", "publisher": "No Starch Press" ]'
  1. Replace and Replace and with the appropriate value on the Summary of the project page:
"taskUid": 26, "indexUid": "books", "status": "enqueued", "type": "documentAdditionOrUpdate", "equeuedAt": "2023-05-26T07:52:24.127920065Z"
  1. To delete documents, Meilisearch includes three routes (endpoints):
/indexes/index_uid/documents // Deleting all documents /indexes/index_uid/documents/document_id // Deleting a single document /indexes/index_uid/documents/delete-batch // Deleting a selection of documents

Document ID and document_id through analyzing the information from books.json. books.json file after downloading the file via MeiliSearch Cloud or your database.

Below is an example of how you could remove the book that has been updated:

curl \ -H 'Authorization: Bearer ' \ -X DELETE '/indexes/books/documents/71nDBQAAQBAJ'

When you've sent your inquiry the reply should look something like:

"taskUid": 10, "indexUid": "books", "status": "enqueued", "type": "documentDeletion", "equeuedAt": "2023-05-26T07:20:11.1291066"

How to Add MeiliSearch into an already existing Web Service

  1. Make a copy of the project that you began from GitHub with the following commands in your terminal
git clone https://github.com/Tammibriggs/meilisearch-app.git cd meilisearch -app npm install

If you open your package.json file, there should be a start command. Use NPM start to start the Node.js project locally using the port 3000 on localhost. When you enter http://localhost:3000/ in your browser, should see the following:

Meilisearch demo starter app
Meilisearch demo starter app.
  1. When the application has been running and functioning, you'll be able to include Meilisearch in it. This will mean that it will be able to start the search form with the search results returned by Meilisearch after you submit it. In order to do this you must install Meilisearch by executing this command from the terminal:
Meilisearch is a program that NPM can install
  1. It is also necessary to download dotenv, the dotenv NPM software that reads sensitive credentials from an .env file. Enter the following command into the terminal's window
npm install dotenv
  1. Make a .env file in the root of your project folder. add the following:
YOUR_PROJECT_URL= '' YOUR_SEARCH_API_KEY= ''

It is essential to alter your-project-url and your admin-api-key to the values they correspond to.

  1. Then, import meilisearch along with Dotenv. dotenv Dotenv package is transferred to server.js file and configure Dotenv :
import MeiliSearch from 'meilisearch dotenv', import it from 'dotenv';dotenv.config();
  1. When you're done then, you are able to then you can start Meilisearch and you're now able to start working on your book-app project. You can click on server.js in the file and add this code following the searchValue variables definitions:
const client = new MeiliSearch( host: process.env.YOUR_PROJECT_URL, apiKey: process.env.YOUR_SEARCH_API_KEY )
  1. A key feature is the capability to browse through the library's book index on Meilisearch with the search parameter that is included in the URL as you fill out the application form. To enable this feature, add the code below before the "Customer" variable definition:
const index = client.index('books') const searchResults = ! !searchValue && await index.search(searchValue)

The code generates an index reference of the book. After that, it employs"search"() to search for documents. "search"() method to locate documents that match the search value in the book's index when you specify searchValue.

  1. For the final step, to display the search results, modify your rendering() method according to the following:
res.render('index', books: searchResults ? searchResults.hits [], searchValue)

Now you're ready to begin looking through the index of the book:

Searching through the 'books' index with the search form
The search for books index using the search form
  1. Once you've added the codes after which you can apply them later, server.js Files are as follows:
import express from 'express'; import MeiliSearch from 'meilisearch'; import dotenv from 'dotenv'; dotenv.config(); const app = express(); const PORT = process.env.PORT || 3000; app.set('view engine', 'ejs'); app.use(express.static('public')); app.get('/', async (req, res) => const searchValue = req.query.search; const client = new MeiliSearch( host: process.env.YOUR_PROJECT_URL, apiKey: process.env.YOUR_SEARCH_API_KEY, ); const index = client.index('books'); const searchResults = ! !searchValue && (await index.search(searchValue)); res.render('index', books: searchResults ? searchResults.hits : [], searchValue, ); ); app.listen(PORT, () => console.log(`listening at http://localhost:$PORT`); );

It is possible to access the full code for this guide on GitHub..

Summary

Meilisearch can be a fantastic alternative to a search engine that increases the functionality of a site's search engine as well as user experience. Its fast performance, focused search algorithm seamless integration makes it a valuable option when you're looking to improve your search capabilities on your website.

     Which one do you rely on to complete your work? Comment below in the comment section!

Jeremy Holcombe

Content & Marketing Editor , WordPress Web Developer, and Content Writer. Alongside everything else related to WordPress I like playing golf, going to the beach, and watching films. Additionally, I suffer from difficulties with height ;).

Article was posted on here