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

Refactor validateQuery method to improve query structure and type safety for ES7, ES8, and ES9

Shun Miyazawa 8 месяцев назад
Родитель
Сommit
6a1264efaa

+ 2 - 1
apps/app/src/server/service/search-delegator/elasticsearch-client-delegator/es7-client-delegator.ts

@@ -49,7 +49,8 @@ export class ES7ClientDelegator {
     updateAliases: (params: RequestParams.IndicesUpdateAliases['body']): Promise<ApiResponse<estypes.IndicesUpdateAliasesResponse>> => {
       return this.client.indices.updateAliases({ body: params });
     },
-    validateQuery: async(params:RequestParams.IndicesValidateQuery<ES7SearchQuery>): Promise<estypes.IndicesValidateQueryResponse> => {
+    validateQuery: async(params: RequestParams.IndicesValidateQuery<{ query?: estypes.QueryDslQueryContainer }>)
+      : Promise<estypes.IndicesValidateQueryResponse> => {
       return (await this.client.indices.validateQuery<estypes.IndicesValidateQueryResponse>(params)).body;
     },
     stats: async(params: RequestParams.IndicesStats): Promise<estypes.IndicesStatsResponse> => {

+ 1 - 0
apps/app/src/server/service/search-delegator/elasticsearch-client-delegator/interfaces.ts

@@ -31,6 +31,7 @@ export type ES7SearchQuery = RequestParams.Search<{
   sort?: ES7types.Sort
   highlight?: ES7types.SearchHighlight
 }>
+
 export interface ES8SearchQuery {
   index: ES8types.IndexName
   _source: ES8types.Fields

+ 9 - 11
apps/app/src/server/service/search-delegator/elasticsearch.ts

@@ -569,32 +569,30 @@ class ElasticsearchDelegator implements SearchDelegator<Data, ESTermsKey, ESQuer
       const validateQueryResponse = await (async() => {
         if (isES7ClientDelegator(this.client)) {
           const es7SearchQuery = query as ES7SearchQuery;
-          const { body } = es7SearchQuery;
           return this.client.indices.validateQuery({
-            index: es7SearchQuery.index,
-            type: es7SearchQuery.type,
             explain: true,
-            ...body,
+            index: es7SearchQuery.index,
+            body: {
+              query: es7SearchQuery.body?.query,
+            },
           });
         }
 
         if (isES8ClientDelegator(this.client)) {
           const es8SearchQuery = query as ES8SearchQuery;
-          const { body } = es8SearchQuery;
           return this.client.indices.validateQuery({
-            index: es8SearchQuery.index,
             explain: true,
-            ...body,
+            index: es8SearchQuery.index,
+            query: es8SearchQuery.body.query,
           });
         }
 
         if (isES9ClientDelegator(this.client)) {
           const es9SearchQuery = query as ES9SearchQuery;
-          const { body } = es9SearchQuery;
           return this.client.indices.validateQuery({
-            index: es9SearchQuery.index,
             explain: true,
-            ...body,
+            index: es9SearchQuery.index,
+            query: es9SearchQuery.body.query,
           });
         }
 
@@ -661,7 +659,7 @@ class ElasticsearchDelegator implements SearchDelegator<Data, ESTermsKey, ESQuer
     const fields = ['path', 'bookmark_count', 'comment_count', 'seenUsers_count', 'updated_at', 'tag_names', 'comments'];
 
     // sort by score
-    const query = {
+    const query: SearchQuery = {
       index: this.aliasName,
       _source: fields,
       body: {