-
Notifications
You must be signed in to change notification settings - Fork 1
Fix name search missing short queries #231
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| //! Helpers for name search filters. | ||
| //! | ||
| //! Name search is a hybrid: a case-insensitive substring match (`ILIKE | ||
| //! '%term%'`) OR a trigram similarity match (`display_name % term`). The `ILIKE` | ||
| //! half gives predictable substring/prefix matching that works for short queries | ||
| //! (`"sa"` finds `"sample.txt"`), which trigram similarity alone misses because a | ||
| //! two-character query produces too few trigrams to clear the similarity | ||
| //! threshold. The trigram half adds typo tolerance (`"smaple"` still finds | ||
| //! `"sample"`). Both halves are served by the same `gin_trgm_ops` index. | ||
|
|
||
| /// Wraps a user search term as an `ILIKE` "contains" pattern (`%term%`), with the | ||
| /// term's `LIKE` metacharacters neutralized so they match literally. | ||
| /// | ||
| /// A raw `%` or `_` in a search term would otherwise act as a wildcard — `"%"` | ||
| /// would match every row, `"a_b"` any three-char run — so each `%`, `_`, and the | ||
| /// `\` escape character itself is prefixed with `\` (Postgres's default `LIKE` | ||
| /// escape). The pattern is a bound parameter, never concatenated into SQL, so | ||
| /// this guards against wildcard injection, not SQL injection. | ||
| /// | ||
| /// Escaping is done in one pass — a `\` is emitted immediately before each | ||
| /// metacharacter — so there is no order-dependence to get wrong (unlike chained | ||
| /// `replace` calls, where the `\`-doubling step must run first). | ||
| pub(crate) fn ilike_contains(term: &str) -> String { | ||
| let mut pattern = String::with_capacity(term.len() + 2); | ||
| pattern.push('%'); | ||
| for ch in term.chars() { | ||
| if matches!(ch, '%' | '_' | '\\') { | ||
| pattern.push('\\'); | ||
| } | ||
| pattern.push(ch); | ||
| } | ||
| pattern.push('%'); | ||
| pattern | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::ilike_contains; | ||
|
|
||
| #[test] | ||
| fn wraps_in_contains_wildcards() { | ||
| assert_eq!(ilike_contains("sa"), "%sa%"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn escapes_like_metacharacters() { | ||
| assert_eq!(ilike_contains("50%_off"), "%50\\%\\_off%"); | ||
| assert_eq!(ilike_contains("a\\b"), "%a\\\\b%"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn empty_term_matches_everything() { | ||
| assert_eq!(ilike_contains(""), "%%"); | ||
| } | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.