diff --git a/src/models/projects/projects.model.js b/src/models/projects/projects.model.js index 762cbf0c..a3cf872c 100644 --- a/src/models/projects/projects.model.js +++ b/src/models/projects/projects.model.js @@ -170,10 +170,20 @@ class Project extends Model { if (columns.length) { fields = columns.join(', '); } - - // hyphens cause errors in sqlite, but we can replace it with a + and - // the fulltext search will work the same - searchStr = searchStr = searchStr.replaceAll('-', '+'); + + searchStr = searchStr.replace(/[-](?=.*[-])/g, "+"); // Replace all but the final dash + searchStr = searchStr.replace('-', ''); //Replace the final dash with nothing + searchStr += '*'; // Query should end with asterisk for partial matching + if (searchStr === '*') { // * isn't a valid matcher on its own. return empty set + return { + count: 0, + rows: [], + } + } + + if (searchStr.startsWith('+')) { + searchStr = searchStr.replace('+', '') // If query starts with +, replace it + } let sql = `SELECT ${fields} FROM projects_fts WHERE projects_fts MATCH :search`; @@ -181,7 +191,7 @@ class Project extends Model { sql = `${sql} AND orgUid = :orgUid`; } - const replacements = { search: `${searchStr}*`, orgUid }; + const replacements = { search: searchStr, orgUid }; const count = ( await sequelize.query(sql, { diff --git a/src/models/units/units.model.js b/src/models/units/units.model.js index c5ff292f..5a887806 100644 --- a/src/models/units/units.model.js +++ b/src/models/units/units.model.js @@ -175,7 +175,7 @@ class Unit extends Model { unitMarketplaceLink, cooresponingAdjustmentDeclaration, correspondingAdjustmentStatus - ) AGAINST ":search" + ) AGAINST '":search"' `; if (orgUid) { @@ -216,15 +216,27 @@ class Unit extends Model { fields = columns.join(', '); } - searchStr = searchStr = searchStr.replaceAll('-', '+'); - + searchStr = searchStr.replace(/[-](?=.*[-])/g, "+"); // Replace all but the final dash + searchStr = searchStr.replace('-', ''); //Replace the final dash with nothing + searchStr += '*'; // Query should end with asterisk for partial matching + if (searchStr === '*') { // * isn't a valid matcher on its own. return empty set + return { + count: 0, + rows: [], + } + } + + if (searchStr.startsWith('+')) { + searchStr = searchStr.replace('+', '') // If query starts with +, replace it + } + let sql = `SELECT ${fields} FROM units_fts WHERE units_fts MATCH :search`; if (orgUid) { sql = `${sql} AND orgUid = :orgUid`; } - const replacements = { search: `${searchStr}*`, orgUid }; + const replacements = { search: searchStr, orgUid }; const count = ( await sequelize.query(sql, {