File

src/provider/elastic/elastic.service.ts

Index

Methods

Constructor

constructor(elasticsearchService: ElasticsearchService)
Parameters :
Name Type Optional
elasticsearchService ElasticsearchService No

Methods

Public Async deleteDocument
deleteDocument(indexData: any)
Parameters :
Name Type Optional
indexData any No
Returns : Promise<any>
Public Async deleteIndex
deleteIndex(indexData: any)
Parameters :
Name Type Optional
indexData any No
Returns : Promise<any>
Public Async get
get(searchData: any)
Parameters :
Name Type Optional
searchData any No
Returns : Promise<any>
Public Async insertIndex
insertIndex(buldData: any)
Parameters :
Name Type Optional
buldData any No
Returns : Promise<any>
Public Async searchIndex
searchIndex(searchData: any)
Parameters :
Name Type Optional
searchData any No
Returns : Promise<any>
Public Async updateByQuery
updateByQuery(index: any, query: any, update: any)
Parameters :
Name Type Optional
index any No
query any No
update any No
Returns : Promise<any>
Public Async updateIndex
updateIndex(updateData: any)
Parameters :
Name Type Optional
updateData any No
Returns : Promise<any>
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
import { ElasticsearchService } from '@nestjs/elasticsearch';
@Injectable()
export class ElasticService {

  constructor(private readonly elasticsearchService: ElasticsearchService) {}

  public async insertIndex(buldData: any): Promise<any> {
    try {

      return await this.elasticsearchService.bulk(buldData);
    } catch (error) {
      throw new Error('Erro bulk');
    }
  }

  public async updateIndex(updateData: any): Promise<any> {
    return await this.elasticsearchService
      .update(updateData)
      .then((res) => res)
      .catch((err) => {
        throw new HttpException(err, HttpStatus.INTERNAL_SERVER_ERROR);
      });
  }

  public async updateByQuery(index: any, query: any, update: any): Promise<any> {
    return await this.elasticsearchService
      .updateByQuery({
        index,
        body: {
          query,
          script: {
            source: update
          },
        },
      })
      .then((res) => res)
      .catch((err) => {
        throw new HttpException(err, HttpStatus.INTERNAL_SERVER_ERROR);
      });
  }

  public async searchIndex(searchData: any): Promise<any> {
    return await this.elasticsearchService
      .search(searchData)
      // .then((res) => {
      //   return res.body.hits.hits.map((hit) => {
      //     hit._source.id = hit._id;
      //     return hit._source
      //   });
      // })
      .catch((err) => {
        throw new HttpException(err, HttpStatus.INTERNAL_SERVER_ERROR);
      });
  }

  public async get(searchData: any): Promise<any> {
    return await this.elasticsearchService.get(searchData)
      // .then((res) => {
      //   return res.body.hits.hits.map((hit) => {
      //     hit._source.id = hit._id;
      //     return hit._source
      //   });
      // })
      .catch((err) => {
        throw new HttpException(err, HttpStatus.INTERNAL_SERVER_ERROR);
      });
  }

  public async deleteIndex(indexData: any): Promise<any> {
    return await this.elasticsearchService.indices
      .delete(indexData)
      .then((res) => res)
      .catch((err) => {
        throw new HttpException(err, HttpStatus.INTERNAL_SERVER_ERROR);
      });
  }

  public async deleteDocument(indexData: any): Promise<any> {
    return await this.elasticsearchService
      .delete(indexData)
      .then((res) => res)
      .catch((err) => {
        throw new HttpException(err, HttpStatus.INTERNAL_SERVER_ERROR);
      });
  }
  
}

results matching ""

    No results matching ""