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 how to hire a software developer 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.
- The ElasticsearchService wraps the Client from the official @elastic/elasticsearch methods.
- After executing the above command successfully, we can now connect to our elasticsearch cluster and perform CRUD operations.
- Now lets create our backend that would communicate with our elastic search engine.
- It can grow thanks to the sponsors and support by the amazing backers.
- In such case, use registerAsync() method, that provides a couple of various ways to deal with async data.
Package Sidebar
We read every piece of feedback, and take your input very seriously. I’m @themuku, and this guide was born 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.
Connecting to our cluster
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 nestjs developer production.
Setting up elasticsearch cluster
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.
- Obviously, our factory behaves like every other one (might be async and is able to inject dependencies through inject).
- 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.
- You can check out the elasticsearch Query DSL to construct more complex queries.
- We read every piece of feedback, and take your input very seriously.
- When a HttpException is caught by the built in handler, the custom object is transformed to JSON and sent to the user.
- Ideal for adding full-text search capabilities to NestJS projects in a containerized environment.
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 more complex queries.
codebyomar
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.