Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
static singleTermSearch(term, index, fields) {
if (!fields) {
fields = index.fields;
}
if (term.length > 15) {
term = term.substring(0, 14) + lunr.Query.wildcard;
}
return index.query(query => {
query.term(term,
{
fields: fields,
usePipeline: true,
boost: 100
});
if (term.includes(lunr.Query.wildcard)) {
// wildcard in term, disable stemming
query.term(term,
{
fields: fields,
usePipeline: false,
let results = index.query(function() {
// exact matches should have the highest boost
this.term(lunr.tokenizer(query), { boost: 100 });
// prefix matches should be boosted slightly
this.term(query, { boost: 10, usePipeline: false, wildcard: lunr.Query.wildcard.TRAILING });
// finally, try a fuzzy search with character 2, without any boost
this.term(query, { boost: 5, usePipeline: false, editDistance: 2 });
});
return results.slice(0, numValues);
// Fall back to basic search if index is not available
return this.searchCiphersBasic(ciphers, query);
}
const ciphersMap = new Map();
ciphers.forEach((c) => ciphersMap.set(c.id, c));
let searchResults: lunr.Index.Result[] = null;
const isQueryString = query != null && query.length > 1 && query.indexOf('>') === 0;
if (isQueryString) {
try {
searchResults = index.search(query.substr(1).trim());
} catch { }
} else {
// tslint:disable-next-line
const soWild = lunr.Query.wildcard.LEADING | lunr.Query.wildcard.TRAILING;
searchResults = index.query((q) => {
lunr.tokenizer(query).forEach((token) => {
const t = token.toString();
q.term(t, { fields: ['name'], wildcard: soWild });
q.term(t, { fields: ['subtitle'], wildcard: soWild });
q.term(t, { fields: ['login.uris'], wildcard: soWild });
q.term(t, {});
});
});
}
if (searchResults != null) {
searchResults.forEach((r) => {
if (ciphersMap.has(r.ref)) {
results.push(ciphersMap.get(r.ref));
}
value.split(" ").forEach(term => {
q.term(term, { usePipeline: true, boost: 100 });
q.term(term, {
usePipeline: false,
boost: 10,
wildcard: lunr.Query.wildcard.LEADING | lunr.Query.wildcard.TRAILING
});
q.term(term, { usePipeline: false, editDistance: 1 });
});
});
searchString.split(/\s+/).forEach(term => {
q.term(term, { usePipeline: true, boost: 100 });
q.term(term, {
usePipeline: false,
boost: 10,
wildcard: lunr.Query.wildcard.LEADING | lunr.Query.wildcard.TRAILING
});
q.term(term, { usePipeline: false, editDistance: 1 });
});
});
function addTerm(query, term, field) {
query.term(term, { field });
query.term(term, {
field,
wildcard: lunr.Query.wildcard.LEADING | lunr.Query.wildcard.TRAILING,
});
query.term(term, {
field,
wildcard: lunr.Query.wildcard.LEADING,
editDistance: 1,
});
}
function addTerm(query, term, field) {
query.term(term, { field });
query.term(term, {
field,
wildcard: lunr.Query.wildcard.LEADING | lunr.Query.wildcard.TRAILING,
});
query.term(term, {
field,
wildcard: lunr.Query.wildcard.LEADING,
editDistance: 1,
});
}
function addTerm(query, term, field) {
query.term(term, { field });
query.term(term, {
field,
wildcard: lunr.Query.wildcard.LEADING | lunr.Query.wildcard.TRAILING,
});
query.term(term, {
field,
wildcard: lunr.Query.wildcard.LEADING,
editDistance: 1,
});
}