update lunr for leading wildcard searches

This commit is contained in:
Kyle Spearrin 2018-09-03 21:51:19 -04:00
parent 26625a58d0
commit eb48abbcf6
3 changed files with 36 additions and 38 deletions

6
package-lock.json generated
View File

@ -4595,9 +4595,9 @@
} }
}, },
"lunr": { "lunr": {
"version": "2.3.1", "version": "2.3.3",
"resolved": "https://registry.npmjs.org/lunr/-/lunr-2.3.1.tgz", "resolved": "https://registry.npmjs.org/lunr/-/lunr-2.3.3.tgz",
"integrity": "sha1-ETYWorYC3cEJMqe/ik5uV+v+zfI=" "integrity": "sha512-rlAEsgU9Bnavca2w1WJ6+6cdeHMXNyadcersyk3ZpuhgWb5HBNj8l4WwJz9PjksAhYDlpQffCVXPctOn+wCIVA=="
}, },
"make-dir": { "make-dir": {
"version": "1.3.0", "version": "1.3.0",

View File

@ -77,7 +77,7 @@
"form-data": "2.3.2", "form-data": "2.3.2",
"keytar": "4.2.1", "keytar": "4.2.1",
"lowdb": "1.0.0", "lowdb": "1.0.0",
"lunr": "2.3.1", "lunr": "2.3.3",
"node-fetch": "2.1.2", "node-fetch": "2.1.2",
"node-forge": "0.7.1", "node-forge": "0.7.1",
"papaparse": "4.3.5", "papaparse": "4.3.5",

View File

@ -8,8 +8,7 @@ import { SearchService as SearchServiceAbstraction } from '../abstractions/searc
import { DeviceType } from '../enums/deviceType'; import { DeviceType } from '../enums/deviceType';
import { FieldType } from '../enums/fieldType'; import { FieldType } from '../enums/fieldType';
import { UriMatchType } from '../enums/uriMatchType';
const IgnoredTlds = ['com', 'net', 'org', 'io', 'co', 'uk', 'au', 'nz', 'fr', 'de', 'eu', 'me', 'jp', 'cn'];
export class SearchService implements SearchServiceAbstraction { export class SearchService implements SearchServiceAbstraction {
private indexing = false; private indexing = false;
@ -47,36 +46,7 @@ export class SearchService implements SearchServiceAbstraction {
(builder as any).field('login.username', { (builder as any).field('login.username', {
extractor: (c: CipherView) => c.login != null ? c.login.username : null, extractor: (c: CipherView) => c.login != null ? c.login.username : null,
}); });
(builder as any).field('login.uris', { (builder as any).field('login.uris', { boost: 2, extractor: (c: CipherView) => this.uriExtractor(c) });
boost: 2,
extractor: (c: CipherView) => c.login == null || !c.login.hasUris ? null :
c.login.uris.filter((u) => u.hostname != null).map((u) => u.hostname),
});
(builder as any).field('login.uris_split', {
boost: 2,
extractor: (c: CipherView) => {
if (c.login == null || !c.login.hasUris) {
return null;
}
let uriParts: string[] = [];
c.login.uris.forEach((u) => {
if (u.hostname == null) {
return;
}
const parts = u.hostname.split('.');
if (parts.length > 0 && parts.length <= 2) {
uriParts.push(parts[0]);
} else if (parts.length > 2) {
uriParts = uriParts.concat(parts.slice(0, parts.length - 2));
const lastBit = parts[parts.length - 2];
if (IgnoredTlds.indexOf(lastBit) === -1) {
uriParts.push(lastBit);
}
}
});
return uriParts.length === 0 ? null : uriParts;
},
});
(builder as any).field('fields', { extractor: (c: CipherView) => this.fieldExtractor(c, false) }); (builder as any).field('fields', { extractor: (c: CipherView) => this.fieldExtractor(c, false) });
(builder as any).field('fields_joined', { extractor: (c: CipherView) => this.fieldExtractor(c, true) }); (builder as any).field('fields_joined', { extractor: (c: CipherView) => this.fieldExtractor(c, true) });
(builder as any).field('attachments', { extractor: (c: CipherView) => this.attachmentExtractor(c, false) }); (builder as any).field('attachments', { extractor: (c: CipherView) => this.attachmentExtractor(c, false) });
@ -128,12 +98,11 @@ export class SearchService implements SearchServiceAbstraction {
} catch { } } catch { }
} else { } else {
// tslint:disable-next-line // tslint:disable-next-line
const soWild = lunr.Query.wildcard.TRAILING; const soWild = lunr.Query.wildcard.LEADING | lunr.Query.wildcard.TRAILING;
searchResults = index.query((q) => { searchResults = index.query((q) => {
q.term(query, { fields: ['name'], wildcard: soWild }); q.term(query, { fields: ['name'], wildcard: soWild });
q.term(query, { fields: ['subTitle'], wildcard: soWild }); q.term(query, { fields: ['subTitle'], wildcard: soWild });
q.term(query, { fields: ['login.uris'], wildcard: soWild }); q.term(query, { fields: ['login.uris'], wildcard: soWild });
q.term(query, { fields: ['login.uris_split'], wildcard: soWild });
lunr.tokenizer(query).forEach((token) => { lunr.tokenizer(query).forEach((token) => {
q.term(token.toString(), {}); q.term(token.toString(), {});
}); });
@ -216,4 +185,33 @@ export class SearchService implements SearchServiceAbstraction {
} }
return joined ? attachments.join(' ') : attachments; return joined ? attachments.join(' ') : attachments;
} }
private uriExtractor(c: CipherView) {
if (c.login == null || !c.login.hasUris) {
return null;
}
const uris: string[] = [];
c.login.uris.forEach((u) => {
if (u.uri == null || u.uri === '') {
return;
}
if (u.hostname != null) {
uris.push(u.hostname);
return;
}
let uri = u.uri;
if (u.match !== UriMatchType.RegularExpression) {
const protocolIndex = uri.indexOf('://');
if (protocolIndex > -1) {
uri = uri.substr(protocolIndex + 3);
}
const queryIndex = uri.search(/\?|&|#/);
if (queryIndex > -1) {
uri = uri.substring(0, queryIndex);
}
}
uris.push(uri);
});
return uris.length > 0 ? uris : null;
}
} }