
Security News
Meet the Socket Team at RSAC and BSidesSF 2025
Join Socket for exclusive networking events, rooftop gatherings, and one-on-one meetings during BSidesSF and RSA 2025 in San Francisco.
@codemask-labs/nestjs-elasticsearch
Advanced tools
Schema based Elasticsearch, NestJS module with utilities, type-safe queries and aggregations builders.
Welcome to Nestjs Elasticsearch module based on @nestjs/elasticsearch package.
The current version (2.x) is fully compatible with Elasticsearch 8. For projects using Elasticsearch 7, use the previous version (1.x).
This package originates from our experience with using Elasticsearch in production environments, which leaded to maintenance issues when extensively used aggregations, searches and filters (especially aggregations).
The main issues we encountered and which our package fixes are:
:rocket: Quick Setup - Get up and running in minutes using our easy-to-understand API.
:nerd_face: :computer: Developer Experience - Designed with developers in mind, package prioritizes ease of use and efficiency throughout the development process.
:white_check_mark: Full TypeScript Support - Enjoy the benefits of code autocompletion and types for both request and response objects. Unlike the original Elasticsearch library, this package provides full type definitions in order to provide better development experience and minimize runtime errors.
:hammer_and_wrench: Utility Methods - Say goodbye to repetitive boilerplate code. The package offers set of utility methods for most common Elasticsearch filtering, sorting, pagination and aggregations use cases.
:bookmark_tabs: Schema definitions - Schema definitions are integrated into the package, with each schema mapping to an Elasticsearch index to provide a clear data model. These definitions are used to register indexes in the module scope and inject them into a service, similar to the approach in the TypeORM NestJS module, ensuring that only fields available for a given index are used when building request objects.
You can install package using yarn or npm:
$ yarn add @codemask-labs/nestjs-elasticsearch
$ npm i @codemask-labs/nestjs-elasticsearch
Once the package is installed, you can start with importing the ElasticsearchModule
into the AppModule
.
import { ElasticsearchModule } from '@codemask-labs/nestjs-elasticsearch'
@Module({
imports: [
ElasticsearchModule.register({
node: 'http://localhost:9200'
})
]
})
class AppModule {}
The register()
method supports all the configuration properties available in ClientOptions
from the @elastic/elasticsearch package.
You can define index schema with @RegisterIndex()
decorator.
import { RegisterIndex } from '@codemask-labs/nestjs-elasticsearch'
@RegisterIndex('examples')
export class ExampleDocument {
readonly id: string
readonly exampleField: string
readonly exampleNumericField: number
}
The ElasticsearchModule
provides the forFeature()
method to configure the module and define which indexes should be registered in the current scope.
import { ElasticsearchModule } from '@codemask-labs/nestjs-elasticsearch'
import { ExampleDocument } from './example.document'
@Module({
imports: [ElasticsearchModule.forFeature([ExampleDocument])],
providers: [ExampleService]
})
export class ExampleModule {}
With module configuration in place, we can inject the ExampleDocument
into the ExampleService
using the @InjectIndex()
decorator:
import { Injectable } from '@nestjs/common'
import { Index } from '@codemask-labs/nestjs-elasticsearch'
import { ExampleDocument } from './example.document'
@Injectable()
export class ExampleService {
@InjectIndex(ExampleDocument)
private readonly exampleIndex: Index<ExampleDocument>
getExampleDocuments() {
return this.exampleIndex.search()
}
}
Now you can start creating request to Elasticsearch.
Once you finish the Getting Started guide, you can start building Elasticsearch request objects.
You can put request object directly in the search()
method
import { getBoolQuery, getTermQuery, Order } from '@codemask-labs/nestjs-elasticsearch'
getExampleDocuments() {
return this.exampleIndex.search({
size: 10,
query: getBoolQuery({
must: [
getTermQuery('exampleField.keyword', 'Some value'),
getRangeQuery('exampleNumericField', {
gte: 1,
lte: 10
})
]
}),
sort: {
'exampleField.keyword': {
order: Order.ASC
}
}
})
}
or use getSearchRequest()
method if you want to move request creation to some other place, but still laverage full type support and autocompletion.
import { getBoolQuery, getTermQuery, getSearchRequest, Order } from '@codemask-labs/nestjs-elasticsearch'
import { ExampleDocument } from './example.document'
const searchRequestBody = getSearchRequest(ExampleDocument, {
size: 10,
query: getBoolQuery({
must: [
getTermQuery('exampleField.keyword', 'Some value'),
getRangeQuery('exampleNumericField', {
gte: 1,
lte: 10
})
]
}),
sort: {
'exampleField.keyword': {
order: Order.ASC
}
}
})
As for now the package provides utils for the following filter queries:
Query DSL | Function Name | Documentation |
---|---|---|
Compound queries | getBoolQuery() | Boolean query |
getMustQuery() | ||
getMustNotQuery() | ||
getShouldQuery() | ||
Full text queries | getMatchQuery() | Match query |
getMatchPhrasePrefixQuery() | Match phrase prefix query | |
Term-level queries | getExistsQuery() | Exists query |
getRangeQuery() | Range query | |
getTermQuery() | Term query | |
getTermsQuery() | Terms query | |
minimum_should_match parameter | getMinimumShouldMatchParameter() | minimum_should_match parameter |
As for now the package provides utils for the following aggregation queries:
Aggregations | Function Name | Documentation |
---|---|---|
Bucket Aggregations | getCompositeAggregation() | Composite aggregation |
getDateHistogramAggregation() | Date histogram aggregation | |
getFilterAggregation() | Filter aggregation | |
getHistogramAggregation() | Histogram aggregation | |
getMissingValueAggregation() | Missing aggregation | |
getRangeAggregation() | Range aggregation | |
getTermsAggregation() | Terms aggregation | |
Metrics Aggregations | getAvgAggregation() | Avg aggregation |
getCardinalityAggregation() | Cardinality aggregation | |
getGeoCentroidAggregation() | Geo-centroid aggregation | |
getMaxAggregation() | Max aggregation | |
getMinAggregation() | Min aggregation | |
getPercentileAggregation() | Percentiles aggregation | |
getSumAggregation() | Sum aggregation | |
getTopHitsAggregation() | Top hits aggregation | |
getValueCountAggregation() | Value count aggregation | |
Pipeline Aggregations | getBucketScriptAggregation() | Bucket script aggregation |
getBucketSelectorAggregation() | Bucket selector aggregation | |
getBucketSortAggregation() | Bucket sort aggregation | |
getStatsBucketAggregation() | Stats bucket aggregation |
MIT
FAQs
Schema based Elasticsearch, NestJS module with utilities, type-safe queries and aggregations builders.
We found that @codemask-labs/nestjs-elasticsearch demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Join Socket for exclusive networking events, rooftop gatherings, and one-on-one meetings during BSidesSF and RSA 2025 in San Francisco.
Security News
Biome's v2.0 beta introduces custom plugins, domain-specific linting, and type-aware rules while laying groundwork for HTML support and embedded language features in 2025.
Security News
Next.js has patched a critical vulnerability (CVE-2025-29927) that allowed attackers to bypass middleware-based authorization checks in self-hosted apps.