Shun Miyazawa hace 3 años
padre
commit
6f06ed5309

+ 2 - 0
packages/app/src/interfaces/search.ts

@@ -44,3 +44,5 @@ export const SORT_ORDER = {
   ASC: 'asc',
 } as const;
 export type SORT_ORDER = typeof SORT_ORDER[keyof typeof SORT_ORDER];
+
+export const MAX_HIGHLITE_PAGE_PATH_FRAGMENT_SIZE = 250;

+ 3 - 4
packages/app/src/server/service/search-delegator/elasticsearch.ts

@@ -9,7 +9,7 @@ import streamToPromise from 'stream-to-promise';
 
 import { SearchDelegatorName } from '~/interfaces/named-query';
 import {
-  IFormattedSearchResult, ISearchResult, SORT_AXIS, SORT_ORDER,
+  IFormattedSearchResult, ISearchResult, SORT_AXIS, SORT_ORDER, MAX_HIGHLITE_PAGE_PATH_FRAGMENT_SIZE,
 } from '~/interfaces/search';
 import loggerFactory from '~/utils/logger';
 
@@ -951,9 +951,8 @@ class ElasticsearchDelegator implements SearchDelegator<Data, ESTermsKey, ESQuer
           fragment_size: 40,
         },
         'path.*': {
-          // No fragments are generated
-          // see: https://www.elastic.co/guide/en/elasticsearch/reference/current/highlighting.html#highlighting-settings
-          number_of_fragments: 0,
+          number_of_fragment: 1,
+          fragment_size: MAX_HIGHLITE_PAGE_PATH_FRAGMENT_SIZE,
         },
       },
     };

+ 7 - 2
packages/app/src/server/service/search.ts

@@ -3,7 +3,9 @@ import xss from 'xss';
 
 import { SearchDelegatorName } from '~/interfaces/named-query';
 import { IPageHasId } from '~/interfaces/page';
-import { IFormattedSearchResult, IPageWithSearchMeta, ISearchResult } from '~/interfaces/search';
+import {
+  IFormattedSearchResult, IPageWithSearchMeta, ISearchResult, MAX_HIGHLITE_PAGE_PATH_FRAGMENT_SIZE,
+} from '~/interfaces/search';
 import loggerFactory from '~/utils/logger';
 
 import { ObjectIdLike } from '../interfaces/mongoose-utils';
@@ -455,9 +457,12 @@ class SearchService implements SearchQueryParser, SearchResolver {
         const pathMatch = highlightData['path.en'] || highlightData['path.ja'];
         const isHtmlInPath = highlightData['path.en'] != null || highlightData['path.ja'] != null;
 
+        const highlightedPath = pathMatch != null && typeof pathMatch[0] === 'string' ? pathMatch[0] : null;
+        const shouldHighlight = highlightedPath != null && MAX_HIGHLITE_PAGE_PATH_FRAGMENT_SIZE >= pageData.path.length;
+
         elasticSearchResult = {
           snippet: snippet != null && typeof snippet[0] === 'string' ? filterXss.process(snippet) : null,
-          highlightedPath: pathMatch != null && typeof pathMatch[0] === 'string' ? filterXss.process(pathMatch) : null,
+          highlightedPath: shouldHighlight ? filterXss.process(highlightedPath) : null,
           isHtmlInPath,
         };
       }