Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance Search Query Handling with Special Characters #178

Merged
merged 1 commit into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"express": "^4.18.2",
"express-validator": "^7.0.1",
"joi": "^17.11.0",
"lodash": "^4.17.21",
"pg": "^8.11.3",
"sequelize": "^6.33.0"
}
Expand Down
9 changes: 6 additions & 3 deletions backend/src/controllers/regionController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const turf = require('@turf/turf');

const { QueryTypes } = require('sequelize');
const _ = require('lodash');
const {
Region, Hierarchy, HierarchyNames,
} = require('../models');
Expand All @@ -21,22 +22,24 @@ exports.searchRegions = async (req, res) => {

// Split the input query into terms
const queryTerms = inputQuery.split(' ').filter((term) => term.trim() !== '');
// Escape the query terms to be used in the regex pattern
const escapedQueryTerms = queryTerms.map((term) => _.escapeRegExp(term));

// Construct the replacements object, to be used in the query
const replacements = {
hierarchyId,
inputQuery,
};
replacements.regexPattern = queryTerms.join('\\s+'); // Regex pattern to match all terms in the region name
queryTerms.forEach((term, index) => {
replacements.regexPattern = escapedQueryTerms.join('\\s+'); // Regex pattern to match all terms in the region name
escapedQueryTerms.forEach((term, index) => {
replacements[`term${index}`] = `%${term}%`;
});

const nameMatchClausesList = [];
const pathMatchClausesList = [];
const regexMatchCaseStatements = [];
const substringMatchCaseStatements = [];
for (let i = 0; i < queryTerms.length; i += 1) {
for (let i = 0; i < escapedQueryTerms.length; i += 1) {
regexMatchCaseStatements.push(`CASE WHEN result.main_name ~* ( '(^|\\w)' || :term${i} || '(\\w|$)' ) THEN 100 ELSE 0 END`);
substringMatchCaseStatements.push(`CASE WHEN result.main_name ILIKE '%' || :term${i} || '%' THEN ${i + 1} ELSE 0 END`);
nameMatchClausesList.push(`region_name ILIKE :term${i}`);
Expand Down
Loading