[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
This commit is contained in:
Gbubemi Smith 2022-05-19 12:15:08 +01:00 committed by GitHub
parent 2f54893854
commit 9a933640dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 1 deletions

View File

@ -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;
}
}