API with NestJS #12. Introduction to Elasticsearch

JavaScript NestJS TypeScript

This entry is part 12 of 19 in the API with NestJS

We can find some searching functionalities in a lot of web applications. While we might be fine when iterating through a small data set, the performance for more extensive databases can become an issue. Relational databases might prove to be relatively slow when searching through a lot of data.

A solution to the above problem might be Elasticsearch. It is a search engine that highly focuses on performance. When using it, we maintain a separate document-oriented database.

If you are familiar with MongoDB, document-oriented databases will ring a bell for you. In theory, we might use Elasticsearch as a general-purpose database. It wasn’t designed for this purpose, though. If you would like to read more about it, check out this question on Stackoverflow.

Running Elasticsearch

Running Elasticsearch includes maintaining a separate, search-optimized database. Because of that, we need to choose one of the ways to fire it up.

In the second part of this series, we’ve started using Docker Compose. Therefore, a fitting way to start using Elasticsearch would be to do so through Docker. When we go to the official Elasticsearch documentation, we can see an example using Docker Compose. It includes three nodes.

An Elasticsearch cluster is a group of one or more Elasticsearch nodes connected. Each node is an instance of Elasticsearch.

Let’s add the above official configuration to our existing file.

docker-compose.yml

You might run into an issue when doing the above: . There is a high chance that increasing the   will help, as described here.

By default, the password for Elasticsearch is  . To set up a password, we can add it to our   file:

docker-compose.yml

The default username is “elastic

Connecting to Elasticsearch in NestJS

To use Elasticsearch within our NestJS project, we can use the official @nestjs/elasticsearch library.

It wraps the @elastic/elasticsearch client. Since it is a peer dependency of @nestjs/elasticsearch, we need to install it.

Don’t confuse it with the “elasticsearch” client that will soon be deprecated.

Due to how we did set up Elesticsearch, our cluster is available at  . Our username is  , and the password is  . We need to add all of the above to our environment variables.

.env

Now we can create our module that uses the above configuration.

/src/search/search.module.ts

We export the   above so that we are able to use some of its features when importing   as suggested here

Populating Elasticsearch with data

The first thing to consider when populating Elasticsearch with data is the concept of the index. In the context of Elasticsearch, we group similar documents by assigning them with the same index.

In the previous versions of Elasticsearch we also used types to group documents, but this concept is being abandoned

When populating the Elasticsearch database with data, we throw in only the parts that we later use when searching. Let’s create an interface for that purpose.

/src/posts/types/postSearchBody.interface.ts

The TypeScript support with Elasticsearch is not that good, unfortunately. Following the official documentation, we can create a search response type for our posts.

/src/posts/types/postSearchBody.interface.ts

When we’re done with the above, we can create a service that takes care of interacting with our Elasticsearch cluster.

/src/posts/postsSearch.service.ts

Above we use   becase we want to search both through the title and the content of the posts

The crucial thing to acknowledge about   is that it returns just the properties that we’ve put into the Elasticsearch database. Since we save the ids of the posts, we can now get the whole documents from our Postgres database. Let’s put this logic into .

/src/posts/posts.service.ts

The last thing to do is to modify the controller so that it accepts a query parameter.

/src/posts/posts.controller.ts

Don’t forget to import the  in the  .

Keeping Elasticsearch consistent with our database

Through our API, we can also edit and delete posts. Therefore, we need to put some effort into keeping the Elasticsearch database consistent with our Postgres instance.

Deleting documents

Since we save the id of the post in our Elasticsearch database, we can use it to find it and delete it. To do so, we can use the   function.

/src/posts/postsSearch.service.ts

Let’s call the above method in   every time we delete a post.

/src/posts/posts.service.ts

Modifying documents

The other thing to make sure that the Elasticsearch database is consistent with our main database is to modify existing documents. To do that, we can use the  function.

Unfortunately, we need to write a script that updates all of the necessary fields. For example, to update the title and the content, we need:

We can create the above script dynamically.

/src/posts/postsSearch.service.ts

Now we need to use the above method whenever we modify existing posts.

/src/posts/posts.service.ts

The Elasticsearch documents also have ids. An alternative to the above deletes and updates would be to store the Elasticsearch id in our Postgres database and use it when deleting and updating.

Summary

Today we’ve learned the very basics of Elasticsearch. When doing so, we’ve added it to our NestJS API. We’ve also created our documents and searched through them. All of that is the tip of the Elasticsearch iceberg. There is a lot more to learn here, so stay tuned!

Series Navigation<< API with NestJS #11. Managing private files with Amazon S3API with NestJS #13. Implementing refresh tokens using JWT >>
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments