This commit is contained in:
leahjessie 2026-07-10 06:51:15 +02:00 committed by GitHub
commit 3f7583ac02
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 88 additions and 3 deletions

View file

@ -966,6 +966,20 @@ WHERE EXISTS (
return `unaccent(${value})`
}
/**
* Returns an expression that strips punctuation the older in-memory search ignored.
*
* @param {string} expression
* @returns {string}
*/
stripPunctuation(expression) {
const punctuationChars = ["'", '.', '`', '"', ',']
punctuationChars.forEach((char) => {
expression = `replace(${expression}, ${this.sequelize.escape(char)}, '')`
})
return expression
}
/**
* Initialize the text query.
*
@ -983,15 +997,17 @@ WHERE EXISTS (
* Get match expression for the specified column.
* If the query contains accents, match against the column as-is (case-insensitive exact match).
* otherwise match against a normalized column (case-insensitive match with accents removed).
* Punctuation is stripped from both sides in all cases.
*
* @param {string} column
* @returns {string}
*/
matchExpression(column) {
const pattern = this.sequelize.escape(`%${this.query}%`)
if (!this.supportsUnaccent) return `${column} LIKE ${pattern}`
const queryExpr = this.stripPunctuation(this.sequelize.escape(this.query))
const pattern = `'%' || ${queryExpr} || '%'`
if (!this.supportsUnaccent) return `${this.stripPunctuation(column)} LIKE ${pattern}`
const normalizedColumn = this.hasAccents ? column : this.normalize(column)
return `${normalizedColumn} LIKE ${pattern}`
return `${this.stripPunctuation(normalizedColumn)} LIKE ${pattern}`
}
}
}