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

78577 adding es to pages in service

Mao 4 лет назад
Родитель
Сommit
86500f827e

+ 1 - 4
packages/app/src/components/SearchPage/SearchResultList.jsx

@@ -13,10 +13,7 @@ class SearchResultList extends React.Component {
       // story: 77515 task : not created yet.
       let snippet = '';
       if (page.elasticSearchResultInfo.contentWithNoSearchedKeyword != null) {
-        if (page.elasticSearchResultInfo.contentWithNoSearchedKeyword.length > 40) {
-          snippet = page.elasticSearchResultInfo.contentWithNoSearchedKeyword.substr(0, 40);
-        }
-        snippet = page.elasticSearchResultInfo.contentWithNoSearchedKeyword;
+        snippet = page.elasticSearchResultInfo.contentWithNoSearchedKeyword.substr(0, 40);
       }
       else { snippet = page.elasticSearchResultInfo.snippet }
       return (

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

@@ -150,32 +150,8 @@ module.exports = function(crowi, app) {
 
       const ids = esResult.data.map((page) => { return page._id });
       const findResult = await Page.findListByPageIds(ids);
-      const xss = require('xss');
-      const options = {
-        whiteList: {
-          em: ['class'],
-        },
-      };
-      const myXss = new xss.FilterXSS(options);
-      // add tags snippet data/contentWithNoKeyword and mattched page name to result pages
-      findResult.pages.map((page) => {
-        const elasticSearchResult = { snippet: '', matchedPath: '' };
-        const data = esResult.data.find((data) => { return page.id === data._id });
-        page._doc.tags = data._source.tag_names;
-        if (data._highlight['body.en'] == null && data._highlight['body.ja'] == null) {
-          elasticSearchResult.contentWithNoSearchedKeyword = myXss.process(data._source.body);
-        }
-        else {
-          const snippet = data._highlight['body.en'] == null ? data._highlight['body.ja'] : data._highlight['body.en'];
-          elasticSearchResult.snippet = myXss.process(snippet);
-        }
-        if (data._highlight['path.en'] !== null && data._highlight['path.ja'] !== null) {
-          const pathMatch = data._highlight['path.en'] == null ? data._highlight['path.ja'] : data._highlight['path.en'];
-          elasticSearchResult.matchedPath = pathMatch;
-        }
-        page._doc.elasticSearchResultInfo = elasticSearchResult;
-        return page;
-      });
+
+      searchService.addElasticSearchInfo(findResult.pages, esResult);
 
       result.meta = esResult.meta;
       result.totalCount = findResult.totalCount;

+ 31 - 0
packages/app/src/server/service/search.js

@@ -2,6 +2,7 @@ import loggerFactory from '~/utils/logger';
 
 // eslint-disable-next-line no-unused-vars
 const logger = loggerFactory('growi:service:search');
+const xss = require('xss');
 
 class SearchService {
 
@@ -149,6 +150,36 @@ class SearchService {
     }
   }
 
+  addElasticSearchInfo(pages, esResult) {
+    const options = {
+      whiteList: {
+        em: ['class'],
+      },
+    };
+    const myXss = new xss.FilterXSS(options);
+
+    pages.map((page) => {
+      const elasticSearchResult = { snippet: '', matchedPath: '' };
+      const data = esResult.data.find((data) => {
+        return page.id === data._id;
+      });
+      page._doc.tags = data._source.tag_names;
+      if (data._highlight['body.en'] == null && data._highlight['body.ja'] == null) {
+        elasticSearchResult.contentWithNoSearchedKeyword = myXss.process(data._source.body);
+      }
+      else {
+        const snippet = data._highlight['body.en'] == null ? data._highlight['body.ja'] : data._highlight['body.en'];
+        elasticSearchResult.snippet = myXss.process(snippet);
+      }
+      if (data._highlight['path.en'] !== null && data._highlight['path.ja'] !== null) {
+        const pathMatch = data._highlight['path.en'] == null ? data._highlight['path.ja'] : data._highlight['path.en'];
+        elasticSearchResult.matchedPath = pathMatch;
+      }
+      page._doc.elasticSearchResultInfo = elasticSearchResult;
+      return page;
+    });
+  }
+
 }
 
 module.exports = SearchService;