Created
July 20, 2020 07:27
-
-
Save paztek/742dbaa362254054e650bca43eea04ed to your computer and use it in GitHub Desktop.
nestjs-elasticsearch-example-2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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