A minimal, copy-pasteable integration for a host-local NestJS app talking to
the Dockerized Elasticsearch from this repo at http://localhost:9200.
These files are a reference to drop into your existing NestJS project — this repo only ships the Elasticsearch infrastructure, not a full Nest app.
npm install @nestjs/elasticsearch @elastic/elasticsearch @nestjs/configUse the
@elastic/elasticsearchmajor that matches the server, e.g.^8.12.0.
In your NestJS project's .env:
ELASTICSEARCH_NODE=http://localhost:9200Why localhost? NestJS runs as a normal host process, outside Docker, so it
reaches the published port 9200. The hostname elasticsearch only resolves
inside the Docker network and must not be used here.
| File | Role |
|---|---|
search.module.ts |
Registers ElasticsearchModule from env config |
search.service.ts |
Thin wrapper: health, index, search, delete |
search.controller.ts |
Demo REST endpoints + health check |
Import SearchModule in your AppModule:
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { SearchModule } from './search/search.module';
@Module({
imports: [
ConfigModule.forRoot({ isGlobal: true }),
SearchModule,
],
})
export class AppModule {}# App health → also pings Elasticsearch
curl localhost:3000/search/health
# Index a document
curl -XPOST localhost:3000/search/articles \
-H 'Content-Type: application/json' \
-d '{"id":"1","title":"Hello ES","body":"first doc"}'
# Search
curl 'localhost:3000/search/articles?q=hello'