Meilisearch vs. Elasticsearch

Meilisearch vs. Elasticsearch

Hello everybody! Get in touch with Gevorg, the frontend technician of Work Solutions. Today I will talk about the integration of Meilisearch and Elasticsearch search services in Strapi.

Strapi – is a popular Headless CMS for server development, as well as creating flexible APIs. However, built-in search tools may be limited. Developers can look for ways to integrate with external search engines. Two such systems Mailisearch and Elasticsearch. They provide an opportunity to implement a full-fledged search in projects.

In the article, we will consider how to integrate these systems, compare their features, determine which tasks are suitable. Examples are considered for strapi v3. However, the information is also valid for v4 with some minor differences in the interaction interfaces.

Built-in search engines

Let’s start with the built-in search tools. For this purpose, Strapi provides a Query Engine API that allows you to create queries with various filters. The request is organized as follows:

Here param is the name of the property and filter is one of the key comparison operators.

The query method also supports AND and OR operators. The AND operator works implicitly, requiring the transfer of an array with the necessary conditions:

The _or keyword is used to use the OR operator:

An example of implementation

To search for articles in the corporate blog, we will add search conditions by author, title and text of the announcement. In addition, we will also apply filtering by categories. This is implemented without much difficulty, since the developers have taken into account such usage scenarios.

The search is carried out according to one of the following criteria:

  • The title contains a keyword. The article category corresponds to the selected filter.

  • The text of the announcement contains a keyword. The article category corresponds to the selected filter.

  • The author’s name or surname contains a keyword. The article category corresponds to the selected filter.

Let’s describe the query parameters that meet these conditions:

Note that the request interface allows you to work with nested properties, such as the name and surname of the author object.

We got acquainted with the built-in search capabilities. If we talk about the usual functions of search services, such as: correcting typos, selecting matches, searching by synonyms, etc., then the built-in functionality of strapi is not enough. To implement a full-fledged search functionality, you can use third-party services such as Meilisearch and Elasticsearch, we will consider them in more detail.

Mailisearch

Meilisearch helps you quickly add powerful search to your project. Has a high level of customization out of the box.

Entity processing

Meilisearch matches all fields by default. Includes both public and private attributes. But there is an opportunity to customize the indexing process. To do this, you need to use the transformEntry method, which allows you to customize the entity. For example, you can exclude private attributes from the indexing process using the sanitizeEntity built-in function:

Search customization

After indexing the collection, you need to configure the file search process /api/COLLECTION/models/COLLECTION.js. Settings include the following properties:

  1. searchableAttributes list of fields to search.

  2. synonyms – synonyms for search values.

  3. stopWords – a list of words to ignore when searching.

  4. typoTolerance – Error rules.

  5. rankingRules – Ranking rules.

  6. displayedAttributes – fields that will be enabled in response to a request.

These options allow you to configure how the system will search and return results.

Integration with the client

An npm package can be used to integrate Meilisearch with a client application meilisearch. Here’s how to initialize the client and search:

To search for articles related to strapi, with the possibility of selecting matches and ordering by date of publication, you can use the following query:

Meilisearch is a great solution for quick search in small volumes of data. However, if a distributed search engine is required, Meilisearch may not be the best choice. In addition, the service does not provide tools for static analysis and visualization of data.

Elasticsearch

Elasticsearch is a powerful search and data analysis engine that can be integrated into Strapi. Provides many features, but requires complex configuration.

Let’s consider how the integration of Elasticsearch is carried out:

Indexing

Integrating with Elasticsearch requires indexing data for search. For this, we will prepare two scripts. The first script:

The second script:

Creating an API

To create an API in Strapi, you need to add a new route, as well as a controller for processing requests. First, let’s add a new route:

The next step is to create a controller to handle requests. To do this, let’s expand the information in the controllers/articles.js file:

We implement the search logic in services/articles.js:

Integration with the client

To integrate with the client application, we turn to the search control point that was created earlier. For example, using the Axios library:

Comparison table of solutions

Consider a comparison table between Strapi, Meilisearch and Elasticsearch, which describes their capabilities and limitations:

Opportunities

Strapi

Mailisearch

Elasticsearch

Distributed work

No

No

So

Ease of integration with Strapi

So

No

Simple query syntax

So

So

No

Restrictions on search queries

Hard

No more than 10 words in the request

No

Adding synonyms

No

So

So

Highlighting matches

No

So

So

Finding Inexact Matches

No

So

So

Limitation of Indexing

The maximum is 200 indices. No more than 100 words per field

No

Choosing the optimal solution

The choice between Strapi Query Engine API, Meilisearch and Elasticsearch depends on your specific tasks and requirements:

– Query Engine API: Suitable for data filtering or simple searches. But if we are talking about a search service, then it is better to choose ready-made solutions.

— Meilisearch: Great for quickly searching small amounts of data. Easy to learn, provides many options for customization.

– Elasticsearch: Ideal for processing large volumes of data, complex queries, distributed systems. However, it requires significantly more resources and a more complex configuration.

It’s worth noting that using Elasticsearch may require an expert to deploy and configure it. This can be expensive and difficult for small projects.

Conclusion

In this article, we looked at different ways to integrate search engines such as Meilisearch and Elasticsearch with Headless CMS Strapi. We studied the processes of data preprocessing, setting search parameters, integration with clients, compared the functionality and limitations of these solutions. This information will help developers choose the best tool for their projects and improve the user experience when searching for information in their applications.

Related posts