import { Client } from '@elastic/elasticsearch' import { ElasticQuery } from './query'; import { Service } from '../../constants'; export class Elastic extends ElasticQuery { client: Client; constructor() { super(); this.client = new Client({ node: Service.ElasticSearch.Url, auth: { username: Service.ElasticSearch.UserName, password: Service.ElasticSearch.Password, }, // ssl: { // rejectUnauthorized: false // }, // protocol: 'http' }); } async addDocument({ index, id, body }: { index: string, id: string, body: any }) { try { const response = await this.client.index({ index, id, body, }); return response; } catch (error) { console.log(error); throw error; } } async updateDocument({ index, id, body }: { index: string, id: string, body: any }) { try { const response = await this.client.update({ index, id, body: { doc: body, }, }); return response; } catch (error) { console.log(error); throw error; } } async deleteDocument({ index, id }: { index: string, id: string }) { try { const response = await this.client.delete({ index, id, }); return response; } catch (error) { console.log(error); throw error; } } async searchQuery(data: any) { try { const response: any = await this.client.search(data); return response.hits; } catch (error) { console.log(error); throw error; } } async count({ index, body }: { index: string, body: any }) { try { const response = await this.client.count({ index, body, }); return response; } catch (error) { console.log(error); throw error; } } async aggregate({ index, body }: { index: string, body: any }) { try { const response = await this.client.search({ index, body, }); return response; } catch (error) { console.log(error); throw error; } } }