-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Search: Fix word exclusion in client side search #13893
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
Open
cglukas
wants to merge
9
commits into
sphinx-doc:master
Choose a base branch
from
cglukas:work/fix-excluded-search-terms
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0449e48
Fix that negated search words are filtered out in the splitQuery.
cglukas dbfbd46
Fix that negated search words create a SyntaxError: Invalid or unexpe…
cglukas 7a8d46e
Fix that excluded words would abort the entire search if matched in o…
cglukas cfdfda4
Fix format and add contribution information.
cglukas 80d6ad1
Fix long line.
cglukas 9b06fd5
Add a dedicated fixture for testing excluded words in searches.
cglukas 11d2e2e
Is the pipeline flaky?
cglukas 6cc4055
Add missing file.
cglukas ffc193b
Remove debug change.
cglukas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -171,15 +171,28 @@ const _orderResultsByScoreThenName = (a, b) => { | |
* Default splitQuery function. Can be overridden in ``sphinx.search`` with a | ||
* custom function per language. | ||
* | ||
* The regular expression works by splitting the string on consecutive characters | ||
* that are not Unicode letters, numbers, underscores, or emoji characters. | ||
* This is the same as ``\W+`` in Python, preserving the surrogate pair area. | ||
* The `consecutiveLetters` regular expression works by matching consecutive characters | ||
* that are Unicode letters, numbers, underscores, or emoji characters. | ||
* | ||
* The `searchWords` regular expression works by matching a word like structure | ||
* that matches the `consecutiveLetters` with or without a leading hyphen '-' which is | ||
* used to exclude search terms later on. | ||
*/ | ||
if (typeof splitQuery === "undefined") { | ||
var splitQuery = (query) => | ||
query | ||
.split(/[^\p{Letter}\p{Number}_\p{Emoji_Presentation}]+/gu) | ||
.filter((term) => term); // remove remaining empty strings | ||
var splitQuery = (query) => { | ||
const consecutiveLetters = | ||
/[\p{Letter}\p{Number}_\p{Emoji_Presentation}]+/gu; | ||
const searchWords = new RegExp( | ||
`(${consecutiveLetters.source})|\\s(-${consecutiveLetters.source})`, | ||
"gu", | ||
); | ||
return Array.from( | ||
query | ||
.matchAll(searchWords) | ||
.map((results) => results[1] ?? results[2]) // select one of the possible groups (e.g. "word" or "-word"). | ||
.filter((term) => term), // remove remaining empty strings. | ||
); | ||
}; | ||
} | ||
|
||
/** | ||
|
@@ -627,15 +640,18 @@ const Search = { | |
|
||
// ensure that none of the excluded terms is in the search result | ||
if ( | ||
[...excludedTerms].some( | ||
(term) => | ||
terms[term] === file | ||
|| titleTerms[term] === file | ||
|| (terms[term] || []).includes(file) | ||
|| (titleTerms[term] || []).includes(file), | ||
) | ||
[...excludedTerms].some((excludedTerm) => { | ||
// Both mappings will contain either a single integer or a list of integers. | ||
// Converting them to lists makes the comparison more readable. | ||
let excludedTermFiles = [].concat(terms[excludedTerm]); | ||
let excludedTitleFiles = [].concat(titleTerms[excludedTerm]); | ||
return ( | ||
excludedTermFiles.includes(file) | ||
|| excludedTitleFiles.includes(file) | ||
); | ||
}) | ||
) | ||
break; | ||
continue; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is probably the most important change! |
||
|
||
// select one (max) score for the file. | ||
const score = Math.max(...wordList.map((w) => scoreMap.get(file).get(w))); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IDK if it's bad to define is regex first and reference it later in the new regex. It could also be plain text. But I thought this way, the origin of the regex can be easier understood.