Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mobizerg/nest-elasticsearch
Elasticsearch integration module for nestjs framework
https://github.com/mobizerg/nest-elasticsearch
elasticsearch nestjs typescript
Last synced: about 1 month ago
JSON representation
Elasticsearch integration module for nestjs framework
- Host: GitHub
- URL: https://github.com/mobizerg/nest-elasticsearch
- Owner: mobizerg
- License: mit
- Created: 2019-05-24T11:53:52.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-06-19T15:06:44.000Z (over 5 years ago)
- Last Synced: 2024-11-16T12:51:30.498Z (about 2 months ago)
- Topics: elasticsearch, nestjs, typescript
- Language: TypeScript
- Size: 13.7 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
A Elasticsearch integration module for Nest.js framework.### Installation
**Yarn**
```bash
yarn add @mobizerg/nest-elasticsearch @elastic/elasticsearch@7
```**NPM**
```bash
npm install @mobizerg/nest-elasticsearch @elastic/elasticsearch@7 --save
```### Description
Elasticsearch integration module for [Nest.js](https://github.com/nestjs/nest) based on the [Elasticsearch](https://github.com/elastic/elasticsearch-js) package.### Usage
Import the **ElasticsearchModule** in `app.module.ts`
```typescript
import { Module } from '@nestjs/common';
import { ElasticsearchModule } from '@mobizerg/nest-elasticsearch';@Module({
imports: [
ElasticsearchModule.register(options)
],
})
export class AppModule {}
```
With Async
```typescript
import { Module } from '@nestjs/common';
import { ElasticsearchModule } from '@mobizerg/nest-elasticsearch';@Module({
imports: [
ElasticsearchModule.registerAsync({
imports: [ConfigModule],
useExisting: ElasticsearchConfigService,
}),
],
})
export class AppModule {}
```Example config file (async)
```typescript
import { Injectable } from '@nestjs/common';
import { ConfigService } from './config.service';
import { ElasticsearchModuleOptions, ElasticsearchOptionsFactory } from '@mobizerg/nest-elasticsearch';@Injectable()
export class ElasticsearchConfigService implements ElasticsearchOptionsFactory {constructor(private readonly config: ConfigService) {}
createElasticsearchOptions(name?: string): ElasticsearchModuleOptions {
return {
name,
node: 'http://localhost:9200',
maxRetries: 3,
requestTimeout: 60000,
};
}
}
```Importing inside services
```typescript
import { Injectable } from '@nestjs/common';
import { ElasticsearchService, InjectClient } from '@mobizerg/nest-elasticsearch';
import { Client } from '@elastic/elasticsearch';@Injectable()
export class SearchService extends ElasticsearchService {constructor(@InjectClient()
readonly client: Client) {
super(client, {
index: 'tag',
settings: {
number_of_shards: 1,
number_of_replicas: 0,
refresh_interval: '2s',
analysis: {
analyzer: {
autocomplete: {
tokenizer: 'autocomplete',
filter: ['trim', 'lowercase'],
},
autocomplete_search: {
tokenizer: 'lowercase',
filter: ['trim'],
},
},
tokenizer: {
autocomplete: {
type: 'edge_ngram',
min_gram: 2,
max_gram: 12,
token_chars: ['letter', 'digit'],
},
},
},
},
properties: {
id: { type: 'integer' },
tag: { type: 'text', analyzer: 'autocomplete', search_analyzer: 'autocomplete_search' },
status: { type: 'byte' },
},
});
}async find(query: string): Promise {
try {
const { body } = await this.client.search({
index: this.config.index,
body: {
_source: ['id'],
from: 1,
size: 15,
query: {
bool: {
must: {
match: {
tag: {
query,
minimum_should_match: '80%',
},
},
},
},
},
},
});
return body;
} catch (error) {
return false;
}
}
}
```### License
MIT