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
// of the name.
const words = query.split(/\s+/);
const wordMatchersForMysql = words.map(
(word) => "%" + word.replace(/_%/g, "\\$0") + "%"
);
const matcherPlaceholders = words.map((_) => "t.name LIKE ?").join(" AND ");
const wordMatchConditions = [];
const wordMatchValues = [];
for (let word of words) {
// 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 bodyIdCondition = bodyId
@ -341,12 +356,12 @@ function buildItemSearchConditions({
`;
const queryConditions = `
(${matcherPlaceholders}) AND t.locale = "en" AND
(${bodyIdCondition}) AND (${zoneIdsCondition}) AND
(${itemKindCondition}) AND (${currentUserCondition})
(${wordMatchCondition}) AND (${bodyIdCondition}) AND
(${zoneIdsCondition}) AND (${itemKindCondition}) AND
(${currentUserCondition}) AND t.locale = "en"
`;
const queryConditionValues = [
...wordMatchersForMysql,
...wordMatchValues,
...bodyIdValues,
...zoneIds,
...currentUserValues,