Add negative word filters to search

Just a quick lil change on a whim, I hope it works well!
This commit is contained in:
Emi Matchu 2021-07-29 00:24:29 -07:00
parent 2ba18ace4d
commit fb12f817e2

View file

@ -308,10 +308,25 @@ function buildItemSearchConditions({
// Split the query into words, and search for each word as a substring // Split the query into words, and search for each word as a substring
// of the name. // of the name.
const words = query.split(/\s+/); const words = query.split(/\s+/);
const wordMatchersForMysql = words.map( const wordMatchConditions = [];
(word) => "%" + word.replace(/_%/g, "\\$0") + "%" const wordMatchValues = [];
); for (let word of words) {
const matcherPlaceholders = words.map((_) => "t.name LIKE ?").join(" AND "); // If the word starts with `-`, remove `-` and treat the filter as negative.
const isNegative = word.startsWith("-");
if (isNegative) {
word = word.substr(1);
}
if (!word) {
continue;
}
const condition = isNegative ? "t.name NOT LIKE ?" : "t.name LIKE ?";
const matcher = "%" + word.replace(/_%/g, "\\$0") + "%";
wordMatchConditions.push(condition);
wordMatchValues.push(matcher);
}
const wordMatchCondition = wordMatchConditions.join(" AND ") || "1";
const itemKindCondition = itemSearchKindConditions[itemKind] || "1"; const itemKindCondition = itemSearchKindConditions[itemKind] || "1";
const bodyIdCondition = bodyId const bodyIdCondition = bodyId
@ -341,12 +356,12 @@ function buildItemSearchConditions({
`; `;
const queryConditions = ` const queryConditions = `
(${matcherPlaceholders}) AND t.locale = "en" AND (${wordMatchCondition}) AND (${bodyIdCondition}) AND
(${bodyIdCondition}) AND (${zoneIdsCondition}) AND (${zoneIdsCondition}) AND (${itemKindCondition}) AND
(${itemKindCondition}) AND (${currentUserCondition}) (${currentUserCondition}) AND t.locale = "en"
`; `;
const queryConditionValues = [ const queryConditionValues = [
...wordMatchersForMysql, ...wordMatchValues,
...bodyIdValues, ...bodyIdValues,
...zoneIds, ...zoneIds,
...currentUserValues, ...currentUserValues,