Просмотр исходного кода

Add ES7, ES8, and ES9 client delegators for Elasticsearch integration

Shun Miyazawa 9 месяцев назад
Родитель
Сommit
36487fa032

+ 65 - 0
apps/app/src/server/service/search-delegator/elasticsearch-client-delegator/es7-client-delegator.ts

@@ -0,0 +1,65 @@
+
+import {
+  Client,
+  type ClientOptions,
+  type ApiResponse,
+  type RequestParams,
+  type estypes,
+} from '@elastic/elasticsearch7';
+
+import type { IElasticsearchClientDelegator } from './interfaces';
+
+export class ES7ClientDelegator implements IElasticsearchClientDelegator {
+
+  private client: Client;
+
+  constructor(options: ClientOptions, rejectUnauthorized: boolean) {
+    this.client = new Client({ ...options, ssl: { rejectUnauthorized } });
+  }
+
+  async bulk(params: RequestParams.Bulk): Promise<ApiResponse<estypes.BulkResponse>> {
+    return this.client.bulk(params);
+  }
+
+  cat = {
+    aliases: (params: RequestParams.CatAliases): Promise<ApiResponse<estypes.CatAliasesResponse>> => this.client.cat.aliases(params),
+    indices: (params: RequestParams.CatIndices): Promise<ApiResponse<estypes.CatIndicesResponse>> => this.client.cat.indices(params),
+  };
+
+  cluster = {
+    health: (): Promise<ApiResponse<estypes.ClusterHealthResponse>> => this.client.cluster.health(),
+  };
+
+  indices = {
+    create: (params: RequestParams.IndicesCreate): Promise<ApiResponse<estypes.IndicesCreateResponse>> => this.client.indices.create(params),
+    delete: (params: RequestParams.IndicesDelete): Promise<ApiResponse<estypes.IndicesDeleteResponse>> => this.client.indices.delete(params),
+    exists: (params: RequestParams.IndicesExists): Promise<ApiResponse<estypes.IndicesDeleteResponse>> => this.client.indices.exists(params),
+    // eslint-disable-next-line max-len
+    existsAlias: (params: RequestParams.IndicesExistsAlias): Promise<ApiResponse<estypes.IndicesExistsAliasResponse>> => this.client.indices.existsAlias(params),
+    putAlias: (params: RequestParams.IndicesPutAlias): Promise<ApiResponse<estypes.IndicesUpdateAliasesResponse>> => this.client.indices.putAlias(params),
+    getAlias: (params: RequestParams.IndicesGetAlias): Promise<ApiResponse<estypes.IndicesGetAliasResponse>> => this.client.indices.getAlias(params),
+    // eslint-disable-next-line max-len
+    updateAliases: (params: RequestParams.IndicesUpdateAliases): Promise<ApiResponse<estypes.IndicesUpdateAliasesResponse>> => this.client.indices.updateAliases(params),
+    // eslint-disable-next-line max-len
+    validateQuery: (params:RequestParams.IndicesValidateQuery): Promise<ApiResponse<estypes.IndicesValidateQueryResponse>> => this.client.indices.validateQuery(params),
+    stats: async(params: RequestParams.IndicesStats): Promise<ApiResponse<estypes.IndicesStatsResponse>> => this.client.indices.stats(params),
+  };
+
+  nodes = {
+    info: (): Promise<ApiResponse<estypes.NodesInfoResponse>> => this.client.nodes.info(),
+  };
+
+
+  ping(): Promise<ApiResponse<estypes.PingResponse>> {
+    return this.client.ping();
+  }
+
+  reindex(indexName: string, tmpIndexName: string): Promise<ApiResponse<estypes.ReindexResponse>> {
+    return this.client.reindex({ wait_for_completion: false, body: { source: { index: indexName }, dest: { index: tmpIndexName } } });
+  }
+
+  async search(params: RequestParams.Search): Promise<ApiResponse<estypes.SearchResponse>> {
+    return this.client.search(params);
+  }
+
+}

+ 55 - 0
apps/app/src/server/service/search-delegator/elasticsearch-client-delegator/es8-client-delegator.ts

@@ -0,0 +1,55 @@
+
+import { Client, type ClientOptions, type estypes } from '@elastic/elasticsearch8';
+
+import type { IElasticsearchClientDelegator } from './interfaces';
+
+export class ES8ClientDelegator implements IElasticsearchClientDelegator {
+
+  private client: Client;
+
+  constructor(options: ClientOptions, rejectUnauthorized: boolean) {
+    this.client = new Client({ ...options, tls: { rejectUnauthorized } });
+  }
+
+  async bulk(params: estypes.BulkRequest): Promise<estypes.BulkResponse> {
+    return this.client.bulk(params);
+  }
+
+  cat = {
+    aliases: (params: estypes.CatAliasesRequest): Promise<estypes.CatAliasesResponse> => this.client.cat.aliases(params),
+    indices: (params: estypes.CatIndicesRequest): Promise<estypes.CatAliasesResponse> => this.client.cat.indices(params),
+  };
+
+  cluster = {
+    health: (): Promise<estypes.ClusterHealthResponse> => this.client.cluster.health(),
+  };
+
+  indices = {
+    create: (params: estypes.IndicesCreateRequest): Promise<estypes.IndicesCreateResponse> => this.client.indices.create(params),
+    delete: (params: estypes.IndicesDeleteRequest): Promise<estypes.IndicesDeleteResponse> => this.client.indices.delete(params),
+    exists: (params: estypes.IndicesExistsRequest): Promise<estypes.IndicesExistsResponse> => this.client.indices.exists(params),
+    existsAlias: (params: estypes.IndicesExistsAliasRequest): Promise<estypes.IndicesExistsAliasResponse> => this.client.indices.existsAlias(params),
+    putAlias: (params: estypes.IndicesPutAliasRequest): Promise<estypes.IndicesPutAliasResponse> => this.client.indices.putAlias(params),
+    getAlias: (params: estypes.IndicesGetAliasRequest): Promise<estypes.IndicesGetAliasResponse> => this.client.indices.getAlias(params),
+    updateAliases: (params: estypes.IndicesUpdateAliasesRequest): Promise<estypes.IndicesUpdateAliasesResponse> => this.client.indices.updateAliases(params),
+    validateQuery: (params: estypes.IndicesValidateQueryRequest): Promise<estypes.IndicesValidateQueryResponse> => this.client.indices.validateQuery(params),
+    stats: (params: estypes.IndicesStatsRequest): Promise<estypes.IndicesStatsResponse> => this.client.indices.stats(params),
+  };
+
+  nodes = {
+    info: (): Promise<estypes.NodesInfoResponse> => this.client.nodes.info(),
+  };
+
+  ping(): Promise<estypes.PingResponse> {
+    return this.client.ping();
+  }
+
+  reindex(indexName: string, tmpIndexName: string): Promise<estypes.ReindexResponse> {
+    return this.client.reindex({ wait_for_completion: false, source: { index: indexName }, dest: { index: tmpIndexName } });
+  }
+
+  async search(params: estypes.SearchRequest): Promise<estypes.SearchResponse> {
+    return this.client.search(params);
+  }
+
+}

+ 54 - 0
apps/app/src/server/service/search-delegator/elasticsearch-client-delegator/es9-client-delegator.ts

@@ -0,0 +1,54 @@
+import { Client, type ClientOptions, type estypes } from '@elastic/elasticsearch9';
+
+import type { IElasticsearchClientDelegator } from './interfaces';
+
+export class ES9ClientDelegator implements IElasticsearchClientDelegator {
+
+  private client: Client;
+
+  constructor(options: ClientOptions, rejectUnauthorized: boolean) {
+    this.client = new Client({ ...options, tls: { rejectUnauthorized } });
+  }
+
+  async bulk(params: estypes.BulkRequest): Promise<estypes.BulkResponse> {
+    return this.client.bulk(params);
+  }
+
+  cat = {
+    aliases: (params: estypes.CatAliasesRequest): Promise<estypes.CatAliasesResponse> => this.client.cat.aliases(params),
+    indices: (params: estypes.CatIndicesRequest): Promise<estypes.CatAliasesResponse> => this.client.cat.indices(params),
+  };
+
+  cluster = {
+    health: (): Promise<estypes.ClusterHealthResponse> => this.client.cluster.health(),
+  };
+
+  indices = {
+    create: (params: estypes.IndicesCreateRequest): Promise<estypes.IndicesCreateResponse> => this.client.indices.create(params),
+    delete: (params: estypes.IndicesDeleteRequest): Promise<estypes.IndicesDeleteResponse> => this.client.indices.delete(params),
+    exists: (params: estypes.IndicesExistsRequest): Promise<estypes.IndicesExistsResponse> => this.client.indices.exists(params),
+    existsAlias: (params: estypes.IndicesExistsAliasRequest): Promise<estypes.IndicesExistsAliasResponse> => this.client.indices.existsAlias(params),
+    putAlias: (params: estypes.IndicesPutAliasRequest): Promise<estypes.IndicesPutAliasResponse> => this.client.indices.putAlias(params),
+    getAlias: (params: estypes.IndicesGetAliasRequest): Promise<estypes.IndicesGetAliasResponse> => this.client.indices.getAlias(params),
+    updateAliases: (params: estypes.IndicesUpdateAliasesRequest): Promise<estypes.IndicesUpdateAliasesResponse> => this.client.indices.updateAliases(params),
+    validateQuery: (params: estypes.IndicesValidateQueryRequest): Promise<estypes.IndicesValidateQueryResponse> => this.client.indices.validateQuery(params),
+    stats: (params: estypes.IndicesStatsRequest): Promise<estypes.IndicesStatsResponse> => this.client.indices.stats(params),
+  };
+
+  nodes = {
+    info: (): Promise<estypes.NodesInfoResponse> => this.client.nodes.info(),
+  };
+
+  ping(): Promise<estypes.PingResponse> {
+    return this.client.ping();
+  }
+
+  reindex(indexName: string, tmpIndexName: string): Promise<estypes.ReindexResponse> {
+    return this.client.reindex({ wait_for_completion: false, source: { index: indexName }, dest: { index: tmpIndexName } });
+  }
+
+  async search(params: estypes.SearchRequest): Promise<estypes.SearchResponse> {
+    return this.client.search(params);
+  }
+
+}