Skip to content

Commit

Permalink
Merge pull request #1369 from Datawheel/filter-lunr-reserved-chars
Browse files Browse the repository at this point in the history
Sanitize LUNR search terms
  • Loading branch information
jhmullen authored Jul 13, 2022
2 parents bc00db0 + 45e8b1c commit a412d8b
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/cms/src/api/searchRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,14 @@ module.exports = function(app) {
const {searchIndexByLocale} = app.settings.cache;

if (useLUNR && searchIndexByLocale[locale]) {
const terms = query.split(" ").map(d => `+${d}~1*`).join(" ");
const terms = query
.replace(/[\+\-\~\*\:\^]/g, ' ') //Remove special characters that are reserved by Lunr https://lunrjs.com/guides/searching.html
.split(" ") //Split into individual terms
.filter(d => d.trim() !== '') //Remove empty trimmed terms
.map(d => `+${d}~1*`) //Add wildcard to each term
.join(" "); //Join back into a single string

// Perform the search using lunr index
const lunrResults = searchIndexByLocale[locale].search(terms);
contentIds = lunrResults.map(d => d.ref);
}
Expand Down

0 comments on commit a412d8b

Please sign in to comment.