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

- add and adjust elasricsearch-client.ts based on latest crowi function
- add elasticsearch-client-types.ts and adjust the type
- adjust initClient() methods to use ElasticsearchClient object
- add getType() instead of add conditions each query

LuqmanHakim-Grune 4 лет назад
Родитель
Сommit
f5cde4955b

+ 54 - 0
packages/app/src/server/service/search-delegator/elasticsearch-client-types.ts

@@ -0,0 +1,54 @@
+export type NodesInfoResponse = {
+  nodes: Record<
+    string,
+    {
+      version: string
+      plugins: Plugin[]
+    }
+  >
+}
+
+export type CatIndicesResponse = {
+  index: string
+}[]
+
+export type IndicesExistsAliasResponse = boolean
+
+export type CatAliasesResponse = {
+  alias: string
+  index: string
+  filter: string
+}[]
+
+export type BulkResponse = {
+  took: number
+  errors: boolean
+  items: Record<string, any>[]
+}
+
+export type SearchResponse = {
+  took: number
+  // eslint-disable-next-line camelcase
+  timed_out: boolean
+  _shards: {
+    total: number
+    successful: number
+    skipped: number
+    failed: number
+  }
+  hits: {
+    total: number | {
+      value: number
+      relation: string
+    } // 6.x.x | 7.x.x
+    // eslint-disable-next-line camelcase
+    max_score: number | null
+    hits: Record<string, {
+      _index: string
+      _type: string
+      _id: string
+      _score: number
+      _source: any
+    }>[]
+  }
+}

+ 61 - 0
packages/app/src/server/service/search-delegator/elasticsearch-client.ts

@@ -0,0 +1,61 @@
+/* eslint-disable implicit-arrow-linebreak */
+/* eslint-disable no-confusing-arrow */
+import { Client as ES6Client, ApiResponse as ES6ApiResponse, RequestParams as ES6RequestParams } from '@elastic/elasticsearch6';
+import { Client as ES7Client, ApiResponse as ES7ApiResponse, RequestParams as ES7RequestParams } from '@elastic/elasticsearch7';
+import {
+  BulkResponse,
+  CatAliasesResponse,
+  CatIndicesResponse,
+  IndicesExistsAliasResponse,
+  NodesInfoResponse,
+  SearchResponse,
+} from './elasticsearch-client-types';
+
+type ApiResponse<T = any, C = any> = ES6ApiResponse<T, C> | ES7ApiResponse<T, C>
+
+export default class ElasticsearchClient {
+
+  client: ES6Client | ES7Client
+
+  constructor(client: ES6Client | ES7Client) {
+    this.client = client;
+  }
+
+  bulk(params: ES6RequestParams.Bulk & ES7RequestParams.Bulk): Promise<ApiResponse<BulkResponse>> {
+    return this.client instanceof ES6Client ? this.client.bulk(params) : this.client.bulk(params);
+  }
+
+  // cat is not used in current Implementation
+  cat = {
+    aliases: (params: ES6RequestParams.CatAliases & ES7RequestParams.CatAliases): Promise<ApiResponse<CatAliasesResponse>> =>
+      this.client instanceof ES6Client ? this.client.cat.aliases(params) : this.client.cat.aliases(params),
+    indices: (params: ES6RequestParams.CatIndices & ES7RequestParams.CatIndices): Promise<ApiResponse<CatIndicesResponse>> =>
+      this.client instanceof ES6Client ? this.client.cat.indices(params) : this.client.cat.indices(params),
+  }
+
+  indices = {
+    create: (params: ES6RequestParams.IndicesCreate & ES7RequestParams.IndicesCreate) =>
+      this.client instanceof ES6Client ? this.client.indices.create(params) : this.client.indices.create(params),
+    delete: (params: ES6RequestParams.IndicesDelete & ES7RequestParams.IndicesDelete) =>
+      this.client instanceof ES6Client ? this.client.indices.delete(params) : this.client.indices.delete(params),
+    existsAlias: (params: ES6RequestParams.IndicesExistsAlias & ES7RequestParams.IndicesExistsAlias): Promise<ApiResponse<IndicesExistsAliasResponse>> =>
+      this.client instanceof ES6Client ? this.client.indices.existsAlias(params) : this.client.indices.existsAlias(params),
+    putAlias: (params: ES6RequestParams.IndicesPutAlias & ES7RequestParams.IndicesPutAlias) =>
+      this.client instanceof ES6Client ? this.client.indices.putAlias(params) : this.client.indices.putAlias(params),
+    updateAliases: (params: ES6RequestParams.IndicesUpdateAliases & ES7RequestParams.IndicesUpdateAliases) =>
+      this.client instanceof ES6Client ? this.client.indices.updateAliases(params) : this.client.indices.updateAliases(params),
+  }
+
+  nodes = {
+    info: (): Promise<ApiResponse<NodesInfoResponse>> => (this.client instanceof ES6Client ? this.client.nodes.info() : this.client.nodes.info()),
+  }
+
+  ping() {
+    return this.client instanceof ES6Client ? this.client.ping() : this.client.ping();
+  }
+
+  search(params: ES6RequestParams.Search & ES7RequestParams.Search): Promise<ApiResponse<SearchResponse>> {
+    return this.client instanceof ES6Client ? this.client.search(params) : this.client.search(params);
+  }
+
+}

+ 9 - 5
packages/app/src/server/service/search-delegator/elasticsearch.ts

@@ -14,6 +14,7 @@ import { SearchDelegatorName } from '~/interfaces/named-query';
 import {
   MetaData, SearchDelegator, Result, SearchableData, QueryTerms,
 } from '../../interfaces/search';
+import ElasticsearchClient from './elasticsearch-client';
 
 const logger = loggerFactory('growi:service:search-delegator:elasticsearch');
 
@@ -100,16 +101,19 @@ class ElasticsearchDelegator implements SearchDelegator<Data> {
   initClient() {
     const { host, auth, indexName } = this.getConnectionInfo();
 
-    this.client = new this.elasticsearch.Client({
+    this.client = new ElasticsearchClient(new this.elasticsearch.Client({
       node: host,
       ssl: { rejectUnauthorized: this.configManager.getConfig('crowi', 'app:elasticsearchRejectUnauthorized') },
       auth,
       requestTimeout: this.configManager.getConfig('crowi', 'app:elasticsearchRequestTimeout'),
-      // log: 'debug',
-    });
+    }));
     this.indexName = indexName;
   }
 
+  getType() {
+    return this.isElasticsearchV6 ? 'pages' : '_doc';
+  }
+
   /**
    * return information object to connect to ES
    * @return {object} { host, auth, indexName}
@@ -348,7 +352,7 @@ class ElasticsearchDelegator implements SearchDelegator<Data> {
     const command = {
       index: {
         _index: this.indexName,
-        _type: this.isElasticsearchV6 ? 'pages' : '_doc',
+        _type: this.getType(),
         _id: page._id.toString(),
       },
     };
@@ -384,7 +388,7 @@ class ElasticsearchDelegator implements SearchDelegator<Data> {
     const command = {
       delete: {
         _index: this.indexName,
-        _type: this.isElasticsearchV6 ? 'pages' : '_doc',
+        _type: this.getType(),
         _id: page._id.toString(),
       },
     };