설치
npm i --save @nestjs/elasticsearch @elastic/elasticsearch
사용하기
- useFactory, configService 로 비동기로 설정
- client configuration 은 link 참조
ElasticsearchModule.registerAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
node: configService.get('ELASTICSEARCH_NODE'),
}),
inject: [ConfigService],
}),
@Injectable()
export class SearchService {
constructor(private readonly elasticsearchService: ElasticsearchService) {}
}
@nestjs/elasticsearch
의 ElasticsearchService
는 @elastic/elasticsearch
의 Client
를 래핑하는 형태이다.
await client.index({
index: 'game-of-thrones',
document: {
character: 'Ned Stark',
quote: 'Winter is coming.'
}
})
const result= await client.search({
index: 'game-of-thrones',
query: {
match: { quote: 'winter' }
}
})
reference