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

Do not highlight if highlighted data has more than DEFAULT_HIGHLIGHT_FRAGMENT characters

Shun Miyazawa 3 лет назад
Родитель
Сommit
faf6334419

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

@@ -50,3 +50,5 @@ export type MongoTermsKey = 'match' | 'not_match' | 'prefix' | 'not_prefix';
 // Query Terms types
 // Query Terms types
 export type ESQueryTerms = Pick<QueryTerms, ESTermsKey>;
 export type ESQueryTerms = Pick<QueryTerms, ESTermsKey>;
 export type MongoQueryTerms = Pick<QueryTerms, MongoTermsKey>;
 export type MongoQueryTerms = Pick<QueryTerms, MongoTermsKey>;
+
+export const DEFAULT_HIGHLIGHT_FRAGMENT_SIZE = 250;

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

@@ -14,7 +14,7 @@ import {
 import loggerFactory from '~/utils/logger';
 import loggerFactory from '~/utils/logger';
 
 
 import {
 import {
-  SearchDelegator, SearchableData, QueryTerms, UnavailableTermsKey, ESQueryTerms, ESTermsKey,
+  SearchDelegator, SearchableData, QueryTerms, UnavailableTermsKey, ESQueryTerms, ESTermsKey, DEFAULT_HIGHLIGHT_FRAGMENT_SIZE,
 } from '../../interfaces/search';
 } from '../../interfaces/search';
 import { PageModel } from '../../models/page';
 import { PageModel } from '../../models/page';
 import { createBatchStream } from '../../util/batch-stream';
 import { createBatchStream } from '../../util/batch-stream';
@@ -945,7 +945,7 @@ class ElasticsearchDelegator implements SearchDelegator<Data, ESTermsKey, ESQuer
     query.body.highlight = {
     query.body.highlight = {
       fields: {
       fields: {
         '*': {
         '*': {
-          fragment_size: 150,
+          fragment_size: DEFAULT_HIGHLIGHT_FRAGMENT_SIZE,
           fragmenter: 'simple',
           fragmenter: 'simple',
           pre_tags: ["<em class='highlighted-keyword'>"],
           pre_tags: ["<em class='highlighted-keyword'>"],
           post_tags: ['</em>'],
           post_tags: ['</em>'],

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

@@ -4,12 +4,13 @@ import xss from 'xss';
 import { SearchDelegatorName } from '~/interfaces/named-query';
 import { SearchDelegatorName } from '~/interfaces/named-query';
 import { IPageHasId } from '~/interfaces/page';
 import { IPageHasId } from '~/interfaces/page';
 import { IFormattedSearchResult, IPageWithSearchMeta, ISearchResult } from '~/interfaces/search';
 import { IFormattedSearchResult, IPageWithSearchMeta, ISearchResult } from '~/interfaces/search';
+import {
+  DEFAULT_HIGHLIGHT_FRAGMENT_SIZE,
+  SearchDelegator, SearchQueryParser, SearchResolver, ParsedQuery, SearchableData, QueryTerms,
+} from '~/server/interfaces/search';
 import loggerFactory from '~/utils/logger';
 import loggerFactory from '~/utils/logger';
 
 
 import { ObjectIdLike } from '../interfaces/mongoose-utils';
 import { ObjectIdLike } from '../interfaces/mongoose-utils';
-import {
-  SearchDelegator, SearchQueryParser, SearchResolver, ParsedQuery, SearchableData, QueryTerms,
-} from '../interfaces/search';
 import NamedQuery from '../models/named-query';
 import NamedQuery from '../models/named-query';
 import { PageModel } from '../models/page';
 import { PageModel } from '../models/page';
 import { serializeUserSecurely } from '../models/serializers/user-serializer';
 import { serializeUserSecurely } from '../models/serializers/user-serializer';
@@ -455,9 +456,11 @@ class SearchService implements SearchQueryParser, SearchResolver {
         const pathMatch = highlightData['path.en'] || highlightData['path.ja'];
         const pathMatch = highlightData['path.en'] || highlightData['path.ja'];
         const isHtmlInPath = highlightData['path.en'] != null || highlightData['path.ja'] != null;
         const isHtmlInPath = highlightData['path.en'] != null || highlightData['path.ja'] != null;
 
 
+        const shouldHighlight = pathMatch != null ? DEFAULT_HIGHLIGHT_FRAGMENT_SIZE >= pathMatch[0].length : false;
+
         elasticSearchResult = {
         elasticSearchResult = {
           snippet: snippet != null && typeof snippet[0] === 'string' ? filterXss.process(snippet) : null,
           snippet: snippet != null && typeof snippet[0] === 'string' ? filterXss.process(snippet) : null,
-          highlightedPath: pathMatch != null && typeof pathMatch[0] === 'string' ? filterXss.process(pathMatch) : null,
+          highlightedPath: pathMatch != null && typeof pathMatch[0] === 'string' && shouldHighlight ? filterXss.process(pathMatch) : null,
           isHtmlInPath,
           isHtmlInPath,
         };
         };
       }
       }