fix: escape Lucene boolean keywords as whole words, not individual characters (fixes #1302)#1371
Open
voidborne-d wants to merge 1 commit intogetzep:mainfrom
Open
Conversation
…aracters (fixes getzep#1302) lucene_sanitize() used str.maketrans to escape individual uppercase letters O, R, N, T, A, D — intending to block Lucene boolean operators (AND, OR, NOT). Because str.maketrans maps every occurrence of each character, this corrupted virtually every query containing those common letters: 'Donald Trump' → '\Donald \Trump' 'ORACLE' → '\O\R\ACLE' 'NASA' → '\N\AS\A' 'Toronto' → '\Toronto' This broke BM25 fulltext search for entity names, facts, and episodes whenever they contained any of those six letters — which is nearly all real-world text. Fix: - Remove O, R, N, T, A, D from the character escape map - Add a regex (_LUCENE_KEYWORD_RE) that matches AND, OR, NOT only as whole uppercase words (\b boundary) - Apply keyword escaping as a second pass after character escaping - Words like ANDROID, TORNADO, ORPHAN pass through correctly Also updates the existing test_lucene_sanitize assertion which expected the T in 'This' to be escaped. 58 new tests covering: - Uppercase letters preserved in normal words (17 cases) - Boolean keywords properly escaped (7 cases) - Keywords inside words NOT escaped (10 cases) - Special characters still escaped (18 cases) - Combined scenarios (6 cases)
Member
|
All contributors have signed the CLA ✍️ ✅ |
Author
|
I have read the CLA Document and I hereby sign the CLA |
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
lucene_sanitize()ingraphiti_core/helpers.pyusedstr.maketransto escape individual uppercase lettersO,R,N,T,A,D— intending to block Lucene boolean operators (AND,OR,NOT). Becausestr.maketransmaps every occurrence of each character, this corrupted virtually every real-world query:Donald Trump\Donald \TrumpDonald TrumpORACLE\O\R\ACLEORACLENASA\N\AS\ANASAToronto\TorontoTorontocats AND dogsc\\ats \\AN\D dogscats \AND dogsThis broke BM25 fulltext search for entity names, facts, and episodes whenever they contained any of those six letters — which is nearly all real-world text.
Fixes #1302.
Fix
O,R,N,T,A,Dfrom the character-levelstr.maketransescape map_LUCENE_KEYWORD_RE = re.compile(r'\b(AND|OR|NOT)\b')that matches the keywords only as standalone uppercase whole wordslambda m: '\\' + m.group(1)Result:
ANDROID,TORNADO,ORPHAN,NORMANDY— pass through unmodifiedAND,OR,NOT— correctly escaped to\AND,\OR,\NOTChanges
graphiti_core/helpers.py— replace character-level escaping with regex + lambdatests/test_lucene_sanitize.py— 58 new parametrized tests covering all casestests/helpers_test.py— update existing assertion that expectedTinThisto be escapedTest results