Explorar o código

Omit es6 code

Shun Miyazawa %!s(int64=3) %!d(string=hai) anos
pai
achega
56b847955b

+ 0 - 120
apps/app/resource/search/mappings-es6.json

@@ -1,120 +0,0 @@
-{
-  "settings": {
-    "analysis": {
-      "filter": {
-        "english_stop": {
-          "type":       "stop",
-          "stopwords":  "_english_"
-        }
-      },
-      "tokenizer": {
-        "edge_ngram_tokenizer": {
-          "type": "edge_ngram",
-          "min_gram": 2,
-          "max_gram": 20,
-          "token_chars": ["letter", "digit"]
-        }
-      },
-      "analyzer": {
-        "japanese": {
-          "tokenizer": "kuromoji_tokenizer",
-          "char_filter" : ["icu_normalizer"]
-        },
-        "english_edge_ngram": {
-          "tokenizer": "edge_ngram_tokenizer",
-          "filter": [
-            "lowercase",
-            "english_stop"
-          ]
-        }
-      }
-    }
-  },
-  "mappings": {
-    "pages": {
-      "properties" : {
-        "path": {
-          "type": "text",
-          "fields": {
-            "raw": {
-              "type": "text",
-              "analyzer": "keyword"
-            },
-            "ja": {
-              "type": "text",
-              "analyzer": "japanese"
-            },
-            "en": {
-              "type": "text",
-              "analyzer": "english_edge_ngram",
-              "search_analyzer": "standard"
-            }
-          }
-        },
-        "body": {
-          "type": "text",
-          "fields": {
-            "ja": {
-              "type": "text",
-              "analyzer": "japanese"
-            },
-            "en": {
-              "type": "text",
-              "analyzer": "english_edge_ngram",
-              "search_analyzer": "standard"
-            }
-          }
-        },
-        "comments": {
-          "type": "text",
-          "fields": {
-            "ja": {
-              "type": "text",
-              "analyzer": "japanese"
-            },
-            "en": {
-              "type": "text",
-              "analyzer": "english_edge_ngram",
-              "search_analyzer": "standard"
-            }
-          }
-        },
-        "username": {
-          "type": "keyword"
-        },
-        "comment_count": {
-          "type": "integer"
-        },
-        "bookmark_count": {
-          "type": "integer"
-        },
-        "seenUsers_count":{
-          "type": "integer"
-        },
-        "like_count": {
-          "type": "integer"
-        },
-        "grant": {
-          "type": "integer"
-        },
-        "granted_users": {
-          "type": "keyword"
-        },
-        "granted_group": {
-          "type": "keyword"
-        },
-        "created_at": {
-          "type": "date",
-          "format": "dateOptionalTime"
-        },
-        "updated_at": {
-          "type": "date",
-          "format": "dateOptionalTime"
-        },
-        "tag_names": {
-          "type": "keyword"
-        }
-      }
-    }
-  }
-}

+ 102 - 36
apps/app/src/server/service/search-delegator/elasticsearch-client.ts

@@ -1,7 +1,7 @@
 /* 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,
@@ -16,68 +16,134 @@ import {
   ReindexResponse,
 } from './elasticsearch-client-types';
 
-type ApiResponse<T = any, C = any> = ES6ApiResponse<T, C> | ES7ApiResponse<T, C>
+// 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);
+//   }
+
+//   // TODO: cat is not used in current Implementation, remove cat?
+//   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),
+//   };
+
+//   cluster = {
+//     health: (params: ES6RequestParams.ClusterHealth & ES7RequestParams.ClusterHealth): Promise<ApiResponse<ClusterHealthResponse>> =>
+//       this.client instanceof ES6Client ? this.client.cluster.health(params) : this.client.cluster.health(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),
+//     exists: (params: ES6RequestParams.IndicesExists & ES7RequestParams.IndicesExists): Promise<ApiResponse<IndicesExistsResponse>> =>
+//       this.client instanceof ES6Client ? this.client.indices.exists(params) : this.client.indices.exists(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),
+//     getAlias: (params: ES6RequestParams.IndicesGetAlias & ES7RequestParams.IndicesGetAlias) =>
+//       this.client instanceof ES6Client ? this.client.indices.getAlias(params) : this.client.indices.getAlias(params),
+//     updateAliases: (params: ES6RequestParams.IndicesUpdateAliases & ES7RequestParams.IndicesUpdateAliases) =>
+//       this.client instanceof ES6Client ? this.client.indices.updateAliases(params) : this.client.indices.updateAliases(params),
+//     validateQuery: (params: ES6RequestParams.IndicesValidateQuery & ES7RequestParams.IndicesValidateQuery): Promise<ApiResponse<ValidateQueryResponse>> =>
+//       this.client instanceof ES6Client ? this.client.indices.validateQuery(params) : this.client.indices.validateQuery(params),
+//     stats: (params: ES6RequestParams.IndicesStats & ES7RequestParams.IndicesStats): Promise<ApiResponse<IndicesStatsResponse>> =>
+//       this.client instanceof ES6Client ? this.client.indices.stats(params) : this.client.indices.stats(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();
+//   }
+
+//   reindex(params: ES6RequestParams.Reindex & ES7RequestParams.Reindex): Promise<ApiResponse<ReindexResponse>> {
+//     return this.client instanceof ES6Client ? this.client.reindex(params) : this.client.reindex(params);
+//   }
+
+//   search(params: ES6RequestParams.Search & ES7RequestParams.Search): Promise<ApiResponse<SearchResponse>> {
+//     return this.client instanceof ES6Client ? this.client.search(params) : this.client.search(params);
+//   }
+
+// }
+
 
+type ApiResponse<T = any, C = any> = ES7ApiResponse<T, C>
 export default class ElasticsearchClient {
 
-  client: ES6Client | ES7Client;
+  client: ES7Client;
 
-  constructor(client: ES6Client | ES7Client) {
+  constructor(client: 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);
+  bulk(params: ES7RequestParams.Bulk): Promise<ApiResponse<BulkResponse>> {
+    return this.client.bulk(params);
   }
 
   // TODO: cat is not used in current Implementation, remove cat?
   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),
+    aliases: (params: ES7RequestParams.CatAliases): Promise<ApiResponse<CatAliasesResponse>> =>
+      this.client.cat.aliases(params),
+    indices: (params: ES7RequestParams.CatIndices): Promise<ApiResponse<CatIndicesResponse>> =>
+      this.client.cat.indices(params),
   };
 
   cluster = {
-    health: (params: ES6RequestParams.ClusterHealth & ES7RequestParams.ClusterHealth): Promise<ApiResponse<ClusterHealthResponse>> =>
-      this.client instanceof ES6Client ? this.client.cluster.health(params) : this.client.cluster.health(params),
+    health: (params: ES7RequestParams.ClusterHealth): Promise<ApiResponse<ClusterHealthResponse>> =>
+      this.client.cluster.health(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),
-    exists: (params: ES6RequestParams.IndicesExists & ES7RequestParams.IndicesExists): Promise<ApiResponse<IndicesExistsResponse>> =>
-      this.client instanceof ES6Client ? this.client.indices.exists(params) : this.client.indices.exists(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),
-    getAlias: (params: ES6RequestParams.IndicesGetAlias & ES7RequestParams.IndicesGetAlias) =>
-      this.client instanceof ES6Client ? this.client.indices.getAlias(params) : this.client.indices.getAlias(params),
-    updateAliases: (params: ES6RequestParams.IndicesUpdateAliases & ES7RequestParams.IndicesUpdateAliases) =>
-      this.client instanceof ES6Client ? this.client.indices.updateAliases(params) : this.client.indices.updateAliases(params),
-    validateQuery: (params: ES6RequestParams.IndicesValidateQuery & ES7RequestParams.IndicesValidateQuery): Promise<ApiResponse<ValidateQueryResponse>> =>
-      this.client instanceof ES6Client ? this.client.indices.validateQuery(params) : this.client.indices.validateQuery(params),
-    stats: (params: ES6RequestParams.IndicesStats & ES7RequestParams.IndicesStats): Promise<ApiResponse<IndicesStatsResponse>> =>
-      this.client instanceof ES6Client ? this.client.indices.stats(params) : this.client.indices.stats(params),
+    create: (params: ES7RequestParams.IndicesCreate) =>
+      this.client.indices.create(params),
+    delete: (params: ES7RequestParams.IndicesDelete) =>
+      this.client.indices.delete(params),
+    exists: (params: ES7RequestParams.IndicesExists): Promise<ApiResponse<IndicesExistsResponse>> =>
+      this.client.indices.exists(params),
+    existsAlias: (params: ES7RequestParams.IndicesExistsAlias): Promise<ApiResponse<IndicesExistsAliasResponse>> =>
+      this.client.indices.existsAlias(params),
+    putAlias: (params: ES7RequestParams.IndicesPutAlias) =>
+      this.client.indices.putAlias(params),
+    getAlias: (params: ES7RequestParams.IndicesGetAlias) =>
+      this.client.indices.getAlias(params),
+    updateAliases: (params: ES7RequestParams.IndicesUpdateAliases) =>
+      this.client.indices.updateAliases(params),
+    validateQuery: (params: ES7RequestParams.IndicesValidateQuery): Promise<ApiResponse<ValidateQueryResponse>> =>
+      this.client.indices.validateQuery(params),
+    stats: (params: ES7RequestParams.IndicesStats): Promise<ApiResponse<IndicesStatsResponse>> =>
+      this.client.indices.stats(params),
   };
 
   nodes = {
-    info: (): Promise<ApiResponse<NodesInfoResponse>> => (this.client instanceof ES6Client ? this.client.nodes.info() : this.client.nodes.info()),
+    info: (): Promise<ApiResponse<NodesInfoResponse>> => (this.client.nodes.info()),
   };
 
   ping() {
-    return this.client instanceof ES6Client ? this.client.ping() : this.client.ping();
+    return this.client.ping();
   }
 
-  reindex(params: ES6RequestParams.Reindex & ES7RequestParams.Reindex): Promise<ApiResponse<ReindexResponse>> {
-    return this.client instanceof ES6Client ? this.client.reindex(params) : this.client.reindex(params);
+  reindex(params: ES7RequestParams.Reindex): Promise<ApiResponse<ReindexResponse>> {
+    return this.client.reindex(params);
   }
 
-  search(params: ES6RequestParams.Search & ES7RequestParams.Search): Promise<ApiResponse<SearchResponse>> {
-    return this.client instanceof ES6Client ? this.client.search(params) : this.client.search(params);
+  search(params: ES7RequestParams.Search): Promise<ApiResponse<SearchResponse>> {
+    return this.client.search(params);
   }
 
 }

+ 6 - 37
apps/app/src/server/service/search-delegator/elasticsearch.ts

@@ -1,7 +1,6 @@
 import { Writable, Transform } from 'stream';
 import { URL } from 'url';
 
-import elasticsearch6 from '@elastic/elasticsearch6';
 import elasticsearch7 from '@elastic/elasticsearch7';
 import gc from 'expose-gc/function';
 import mongoose from 'mongoose';
@@ -79,9 +78,9 @@ class ElasticsearchDelegator implements SearchDelegator<Data, ESTermsKey, ESQuer
       throw new Error('Unsupported Elasticsearch version. Please specify a valid number to \'ELASTICSEARCH_VERSION\'');
     }
 
-    this.isElasticsearchV6 = elasticsearchVersion === 6;
+    this.isElasticsearchV6 = false;
 
-    this.elasticsearch = this.isElasticsearchV6 ? elasticsearch6 : elasticsearch7;
+    this.elasticsearch = elasticsearch7;
     this.isElasticsearchReindexOnBoot = this.configManager.getConfig('crowi', 'app:elasticsearchReindexOnBoot');
     this.client = null;
 
@@ -345,9 +344,7 @@ class ElasticsearchDelegator implements SearchDelegator<Data, ESTermsKey, ESQuer
   }
 
   async createIndex(index) {
-    let mappings = this.isElasticsearchV6
-      ? require('^/resource/search/mappings-es6.json')
-      : require('^/resource/search/mappings-es7.json');
+    let mappings = require('^/resource/search/mappings-es7.json');
 
     if (process.env.CI) {
       mappings = require('^/resource/search/mappings-es7-for-ci.json');
@@ -704,10 +701,6 @@ class ElasticsearchDelegator implements SearchDelegator<Data, ESTermsKey, ESQuer
       },
     };
 
-    if (this.isElasticsearchV6) {
-      Object.assign(query, { type: 'pages' });
-    }
-
     return query;
   }
 
@@ -769,7 +762,6 @@ class ElasticsearchDelegator implements SearchDelegator<Data, ESTermsKey, ESQuer
     }
 
     if (parsedKeywords.phrase.length > 0) {
-      const phraseQueries: any[] = [];
       parsedKeywords.phrase.forEach((phrase) => {
         const phraseQuery = {
           multi_match: {
@@ -783,21 +775,11 @@ class ElasticsearchDelegator implements SearchDelegator<Data, ESTermsKey, ESQuer
             ],
           },
         };
-        if (this.isElasticsearchV6) {
-          phraseQueries.push(phraseQuery);
-        }
-        else {
-          query.body.query.bool.must.push(phraseQuery);
-        }
+        query.body.query.bool.must.push(phraseQuery);
       });
-
-      if (this.isElasticsearchV6) {
-        query.body.query.bool.must.push(phraseQueries);
-      }
     }
 
     if (parsedKeywords.not_phrase.length > 0) {
-      const notPhraseQueries: any[] = [];
       parsedKeywords.not_phrase.forEach((phrase) => {
         const notPhraseQuery = {
           multi_match: {
@@ -810,18 +792,8 @@ class ElasticsearchDelegator implements SearchDelegator<Data, ESTermsKey, ESQuer
             ],
           },
         };
-
-        if (this.isElasticsearchV6) {
-          notPhraseQueries.push(notPhraseQuery);
-        }
-        else {
-          query.body.query.bool.must_not.push(notPhraseQuery);
-        }
+        query.body.query.bool.must_not.push(notPhraseQuery);
       });
-
-      if (this.isElasticsearchV6) {
-        query.body.query.bool.must_not.push(notPhraseQueries);
-      }
     }
 
     if (parsedKeywords.prefix.length > 0) {
@@ -956,11 +928,8 @@ class ElasticsearchDelegator implements SearchDelegator<Data, ESTermsKey, ESQuer
           number_of_fragments: 0,
         },
       },
+      max_analyzed_offset: 1000000 - 1, // Set the query parameter [max_analyzed_offset] to a value less than index setting [1000000] and this will tolerate long field values by truncating them.
     };
-
-    if (!this.isElasticsearchV6) {
-      query.body.highlight.max_analyzed_offset = 1000000 - 1; // Set the query parameter [max_analyzed_offset] to a value less than index setting [1000000] and this will tolerate long field values by truncating them.
-    }
   }
 
   async search(data: SearchableData<ESQueryTerms>, user, userGroups, option?): Promise<ISearchResult<unknown>> {