SULLEY\ryo-h 4 лет назад
Родитель
Сommit
db33dd8217
2 измененных файлов с 27 добавлено и 17 удалено
  1. 4 2
      packages/app/src/server/routes/search.js
  2. 23 15
      packages/app/src/server/service/search.js

+ 4 - 2
packages/app/src/server/routes/search.js

@@ -139,7 +139,9 @@ module.exports = function(crowi, app) {
 
     const result = {};
     try {
-      const esResult = await searchService.searchKeyword(keyword, user, userGroups, searchOpts);
+      const esResult = searchService.formatResult(
+        await searchService.searchKeyword(keyword, user, userGroups, searchOpts),
+      );
 
       // create score map for sorting
       // key: id , value: score
@@ -156,7 +158,7 @@ module.exports = function(crowi, app) {
           return page.id === data._id;
         });
         page._doc.tags = data._source.tag_names;
-        page._doc.elasticSearchResultInfo = data.elasticSearchResultInfo;
+        page._doc.elasticSearchResult = data.elasticSearchResult;
         return page;
       });
 

+ 23 - 15
packages/app/src/server/service/search.js

@@ -4,6 +4,15 @@ import loggerFactory from '~/utils/logger';
 const logger = loggerFactory('growi:service:search');
 const xss = require('xss');
 
+// options for filtering xss
+const filterXssOptions = {
+  whiteList: {
+    em: ['class'],
+  },
+};
+
+const filterXss = new xss.FilterXSS(filterXssOptions);
+
 class SearchService {
 
   constructor(crowi) {
@@ -138,15 +147,8 @@ class SearchService {
   }
 
   async searchKeyword(keyword, user, userGroups, searchOpts) {
-    const options = {
-      whiteList: {
-        em: ['class'],
-      },
-    };
-    const myXss = new xss.FilterXSS(options);
-    let esResult;
     try {
-      esResult = await this.delegator.searchKeyword(keyword, user, userGroups, searchOpts);
+      return this.delegator.searchKeyword(keyword, user, userGroups, searchOpts);
     }
     catch (err) {
       logger.error(err);
@@ -155,16 +157,22 @@ class SearchService {
       this.isErrorOccuredOnSearching = true;
       throw err;
     }
+  }
+
+  /**
+   * formatting result
+   */
+  formatResult(esResult) {
     esResult.data.forEach((data) => {
-      const elasticSearchResult = { snippet: '', matchedPath: '' };
       const highlightData = data._highlight;
       const snippet = highlightData['body.en'] || highlightData['body.ja'];
-      elasticSearchResult.snippet = myXss.process(snippet);
-      if (highlightData['path.en'] != null || highlightData['path.ja'] != null) {
-        const pathMatch = highlightData['path.en'] || highlightData['path.ja'];
-        elasticSearchResult.matchedPath = pathMatch;
-      }
-      data.elasticSearchResultInfo = elasticSearchResult;
+      const pathMatch = highlightData['path.en'] || highlightData['path.ja'];
+
+      data.elasticSearchResult = {
+        snippet: filterXss.process(snippet),
+        // todo: use filter xss.process() for matchedPath;
+        matchedPath: pathMatch || '',
+      };
     });
     return esResult;
   }