Socket
Socket
Sign inDemoInstall

@mobizerg/nest-elasticsearch

Package Overview
Dependencies
31
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @mobizerg/nest-elasticsearch

Elasticsearch integration module for nestjs framework


Version published
Weekly downloads
22
increased by144.44%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

Nest Logo

A Elasticsearch integration module for Nest.js framework.

Installation

Yarn

yarn add @mobizerg/nest-elasticsearch @elastic/elasticsearch@7

NPM

npm install @mobizerg/nest-elasticsearch @elastic/elasticsearch@7 --save

Description

Elasticsearch integration module for Nest.js based on the Elasticsearch package.

Usage

Import the ElasticsearchModule in app.module.ts

import { Module } from '@nestjs/common';
import { ElasticsearchModule } from '@mobizerg/nest-elasticsearch';

@Module({
    imports: [
        ElasticsearchModule.register(options)
    ],
})
export class AppModule {}

With Async

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)

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

import { Injectable } from '@nestjs/common';
import { ElasticsearchService, InjectClient } from '@mobizerg/nest-elasticsearch';
import { Client } from '@elastic/elasticsearch';

@Injectable()
export class SearchService extends ElasticsearchService<T> {

  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<any> {
    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

Keywords

FAQs

Last updated on 19 Jun 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc