Skip to content

Instantly share code, notes, and snippets.

@paztek
Created July 20, 2020 07:27
Show Gist options
  • Save paztek/742dbaa362254054e650bca43eea04ed to your computer and use it in GitHub Desktop.
Save paztek/742dbaa362254054e650bca43eea04ed to your computer and use it in GitHub Desktop.
nestjs-elasticsearch-example-2
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
@Controller()
export class AppController {
constructor(
private readonly appService: AppService,
) {}
@Get('/companies')
getCompanies(): Promise<any[]> {
return this.appService.getCompanies();
}
}
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ElasticsearchModule } from './elasticsearch/elasticsearch.module';
@Module({
imports: [
ElasticsearchModule,
],
controllers: [
AppController,
],
providers: [
AppService,
],
})
export class AppModule {}
import { Injectable } from '@nestjs/common';
import { ElasticsearchService } from '@nestjs/elasticsearch';
@Injectable()
export class AppService {
constructor(
private readonly client: ElasticsearchService,
) {}
async getCompanies(): Promise<any[]> {
const results = await this.client.search({ index: 'companydatabase' });
return results.body.hits.hits.map((hit) => hit._source);
}
}
import { Module } from '@nestjs/common';
import { ElasticsearchModule as BaseElasticsearchModule } from '@nestjs/elasticsearch';
@Module({
imports: [
BaseElasticsearchModule.register({
node: 'http://localhost:9200',
}),
],
exports: [
BaseElasticsearchModule,
],
})
export class ElasticsearchModule {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment