Implement Elastic Search Using NestJS
I have a question about connecting Elasticsearch with Nest.js. Obviously, our factory behaves like every other one (might be async and is able to inject dependencies through inject). You can check out the elasticsearch Query DSL to construct nestjs developer more complex queries.
📖 How to Use This Guide
Above construction will instantiate ElasticsearchConfigService inside ElasticsearchModule and will leverage it to create options object. Quite often you might want to asynchronously pass your module options instead of passing them beforehand. In such case, use registerAsync() method, that provides a couple of various ways to deal with async data.
Running the app
The next block of code pings our cluster to make sure we are in connection and the ping function returns a promise, which is been handled by our catch block. In the case of a failed connection a HttpException is thrown with the message ‘Unable to reach Elasticsearch cluster’. I’m quite excited about this post, i have been thinking about the perfect topic to launch my blog with, after scribbling lots of topics i decided to write about Elasticsearch. The reason is because i recently implemented it in a side project, and it was quite interesting setting it up, and magical how fast the queries are. Let’s go through it together.If you are not familiar with elastic search don’t get intimidated by the name.
Search code, repositories, users, issues, pull requests…
NestJS is a progressive Node.js web framework built with TypeScript and uses ExpressJS under the hood. The ElasticsearchService wraps the Client from the official @elastic/elasticsearch methods. The ElasticsearchModule.register() takes options object as an argument, read more. After executing the above command successfully, we can now connect to our elasticsearch cluster and perform CRUD operations. This guide documents my implementation journey, sharing real solutions and approaches I’m currently using in production.
- A progressive Node.js framework for building efficient and scalable server-side applications.
- When a HttpException is caught by the built in handler, the custom object is transformed to JSON and sent to the user.
- NestJS is a progressive Node.js web framework built with TypeScript and uses ExpressJS under the hood.
- I’m quite excited about this post, i have been thinking about the perfect topic to launch my blog with, after scribbling lots of topics i decided to write about Elasticsearch.
- We read every piece of feedback, and take your input very seriously.
We read every piece of feedback, and take your input very seriously. I’m @themuku, and this guide was born Software quality assurance out of necessity during my own journey implementing Elasticsearch in a NestJS project. When I started, I found myself piecing together information from various sources – documentation, videos, and forums. While there were resources about Elasticsearch and NestJS separately, I couldn’t find a comprehensive guide that brought them together effectively. If you have the above tools ready, go ahead and run the following commands to install the NestJS CLI and create a new nest project.
Implement Elastic Search Using NestJS
It’s growing alongside my project, which means you’ll get practical, tested solutions rather than theoretical examples. Elasticsearch module for Nest based on the official @elastic/elasticsearch package. A progressive Node.js framework for building efficient and scalable server-side applications.
Packages
Firstly we imported Injectable and HttpException from @nestjs/common package, Injectable is a decorator that allows us to create providers in nestjs by simply annotating a class with the Injectable decorator. While the HttpException is a class that accepts two parameters, the custom response object and the appropriate HttpStatus. When a HttpException is caught by the built in handler, the custom object is transformed to JSON and sent to the user. The subsequent import is the elasticsearch library which we would need to interface with our elasticsearch cluster.Next, we have our provider class AppService which is been exported. What we first did was to declare a variable esclient which would be our Elasticsearch client. Within the constructor block we then initialized our Elasticsearch client by passing in the host address.
Connecting to our cluster
We’ll be needing a few dependencies, so let’s go ahead and install them. If you studied the pokemon abilities data we copied you would notice that each object has a url property. The above request would search for any the documents and return any whose url matches https. Now lets create our backend that would communicate with our elastic search engine.
Package Sidebar
Demo project showcasing Elasticsearch integration with NestJS using Docker Compose. Includes examples for setting up Elasticsearch, indexing, searching data, and managing Elasticsearch operations within a NestJS service. Ideal for adding full-text search capabilities to NestJS projects in a containerized environment. It can grow thanks to the sponsors and support by the amazing backers. It works the same as useClass with one critical difference – ElasticsearchModule will lookup imported modules to reuse already created ConfigService, instead of instantiating it on its own.