From 9a933640dca4f05150e2cd98165a4743fb471bdc Mon Sep 17 00:00:00 2001 From: Gbubemi Smith Date: Thu, 19 May 2022 12:15:08 +0100 Subject: [PATCH] [ps-136] Ignore accented characters in vault search (#804) * query accented characters now possible * Added accented ignore to other fields * Added pipeline function to remove accented characters * lint fix * Corrected spelling --- common/src/services/search.service.ts | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/common/src/services/search.service.ts b/common/src/services/search.service.ts index e1c54ceb97..50aa27e87e 100644 --- a/common/src/services/search.service.ts +++ b/common/src/services/search.service.ts @@ -24,6 +24,8 @@ export class SearchService implements SearchServiceAbstraction { if (["zh-CN", "zh-TW"].indexOf(i18nService.locale) !== -1) { this.searchableMinLength = 1; } + //register lunr pipeline function + lunr.Pipeline.registerFunction(this.normalizeAccentsPipelineFunction, "normalizeAccents"); } clearIndex(): void { @@ -49,9 +51,12 @@ export class SearchService implements SearchServiceAbstraction { this.indexedEntityId = indexedEntityId; this.index = null; const builder = new lunr.Builder(); + builder.pipeline.add(this.normalizeAccentsPipelineFunction); builder.ref("id"); builder.field("shortid", { boost: 100, extractor: (c: CipherView) => c.id.substr(0, 8) }); - builder.field("name", { boost: 10 }); + builder.field("name", { + boost: 10, + }); builder.field("subtitle", { boost: 5, extractor: (c: CipherView) => { @@ -281,4 +286,19 @@ export class SearchService implements SearchServiceAbstraction { }); return uris.length > 0 ? uris : null; } + + private normalizeAccentsPipelineFunction(token: lunr.Token): any { + const searchableFields = ["name", "login.username", "subtitle", "notes"]; + const fields = (token as any).metadata["fields"]; + const checkFields = fields.every((i: any) => searchableFields.includes(i)); + + if (checkFields) { + return token + .toString() + .normalize("NFD") + .replace(/[\u0300-\u036f]/g, ""); + } + + return token; + } }