Repository navigation
Fix Arabic search silently returning no results for queries with regex metacharacters - #4022
Open
m86hussein wants to merge 1 commit into
Open
m86hussein wants to merge 1 commit into
m86hussein wants to merge 1 commit into
Conversation
…x metacharacters
ArabicCharacterHelper.generateRegex() appended any character without a lookup-table entry straight into the regex, stripping only [ ( ). Other regex metacharacters (* + ? { } ^ \$ | . \\ ]) were emitted raw, so Pattern.compile() in ArabicSearcher.runQuery() threw a PatternSyntaxException. DatabaseHandler.search() catches it and returns null, so the entire Arabic search silently yields no results whenever the query contains one of these characters (e.g. a stray '*').
Escape these metacharacters when they are not lookup-table entries so the generated pattern always compiles and the character is matched literally.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
Arabic search silently returns no results whenever the query contains a regex metacharacter.
ArabicCharacterHelper.generateRegex()appends any character that has no lookup-table entry straight into the regex, stripping only[,(,)(bannedChars). Every other regex metacharacter —* + ? { } ^ $ | . \ ]— is emitted raw.ArabicSearcher.runQuery()then does:So a query like
*produces the pattern*, andPattern.compile("*")throwsPatternSyntaxException: Dangling meta character '*'.DatabaseHandler.search()wraps the call in atry/catchthat logs and returnsnull, so the entire search silently yields zero results — no error, no matches — for any query containing one of these characters.Fix
Escape regex metacharacters when they aren't lookup-table entries, so the generated pattern always compiles and the character is matched literally (which is the sensible behavior for a search box).
[,(,)remain stripped as before (they are only ever used to build the character classes). All regex building happens outside a character class, so only the outside-class metacharacters need escaping.Tests
Added to
ArabicCharacterHelperTest:*compiles and matches a literal*(the previous crash case);.matches a literal dot rather than any character../gradlew :common:search:testReleaseUnitTestpasses (ArabicCharacterHelperTest 23, 0 failures).AI disclosure: this PR was drafted with GitHub Copilot and opened by @m86hussein, who is responsible for it.