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

Fix the JavaScript search term splitting for compound words #307

Merged
merged 1 commit into from
Dec 12, 2023
Merged
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
59 changes: 36 additions & 23 deletions src/assets/javascripts/sphinx_search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -709,36 +709,49 @@ export async function getResults(query: string): Promise<SearchResultStream> {
// Object search terms.
const objectterms = []

for (const origTerm of splitQuery(query)) {
const lowerTerm = origTerm.toLowerCase()
for (let origTerm of splitQuery(query)) {
let negative = false;
if (origTerm[0] === '-') {
negative = true;
origTerm = origTerm.substr(1);
}
let lowerTerm = origTerm.toLowerCase()
if (lowerTerm.length === 0) {
continue
}
objectterms.push(lowerTerm)

if (stopwords.indexOf(lowerTerm) !== -1) {
// skip this "word"
continue
}
// stem the word
let word = stemmer.stemWord(lowerTerm)
// prevent stemmer from cutting word smaller than two chars
if (word.length < 3 && lowerTerm.length >= 3) {
word = lowerTerm
let atLeastOneWord = false
// The search term made be made up of multiple "words" separated
// by special characters like [-._]. Split them up and treat each
// as a separate search term.
for (const wordMatch of lowerTerm.matchAll(/\w+/g)) {
const subTerm = wordMatch[0];
if (stopwords.indexOf(subTerm) !== -1) {
// skip this "word"
continue
}
// stem the word
let word = stemmer.stemWord(subTerm)
// prevent stemmer from cutting word smaller than two chars
if (word.length < 3 && subTerm.length >= 3) {
word = subTerm
}
let toAppend: string[]
// select the correct list
if (negative) {
toAppend = excluded
} else {
toAppend = searchterms
atLeastOneWord = true
}
// only add if not already in the list
if (toAppend.indexOf(word) === -1) {
toAppend.push(word)
}
}
let toAppend: string[]
// select the correct list
if (word[0] === "-") {
toAppend = excluded
word = word.substr(1)
} else {
toAppend = searchterms
if (!negative && atLeastOneWord) {
hlterms.push(lowerTerm)
}
// only add if not already in the list
if (toAppend.indexOf(word) === -1) {
toAppend.push(word)
}
}

// console.debug('SEARCH: searching for:');
Expand Down