Repository navigation
fix(memory): find Chinese and Japanese words inside a sentence in sqlite memory - #7960
Open
Fruiterer333 wants to merge 2 commits into
Open
Fruiterer333 wants to merge 2 commits into
Fruiterer333 wants to merge 2 commits into
Conversation
…ite memory FTS5's unicode61 tokenizer indexes a run of Chinese or Japanese text as one token, so searching a word inside the run found nothing. Route queries that contain Chinese or Japanese characters to the existing LIKE path, which matches them as substrings.
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.
Please ensure you have read the contribution guide before creating a pull request.
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
Problem:
With FTS5 (the default,
fts="auto"),SqliteMemoryServicecannot find a Chinese or Japanese word inside a stored sentence. Theunicode61tokenizer indexes我想买这辆车,价格是二十万。as two tokens,我想买这辆车and价格是二十万, so searching价格or二十万returns nothing. The same search works withfts="off"and withInMemoryMemoryService.Solution:
When a query token contains a Chinese or Japanese character (CJK ideographs, hiragana, katakana),
search_memoryuses the existingLIKEpath instead of FTS5. That path already matches tokens as substrings, so it is whatfts="off"does today.bm25ranking, unchanged.LIKEpath's ranking), instead ofbm25, and they scan the user's rows instead of using the index.I did not change other scripts written without spaces (e.g. Thai), since I have not tested them.
Testing Plan
Unit Tests:
Added
test_search_matches_cjk_word_inside_sentenceintests/unittests/memory/test_sqlite_memory_service.py. It runs in bothfts="on"andfts="off"modes with four cases:价格and二十万in a Chinese sentence, and使うandPython 使うin私はPythonを使う. Before the fix, the three Chinese/Japanese-only cases fail infts="on"mode.pre-commit run --fileson the two changed files passes (ruff, isort, pyink, codespell, ADK compliance checks).Manual End-to-End (E2E) Tests:
Ran the reproduction script from the issue.
Before:
After:
Checklist
Additional context
I used Claude Code to help find the issue and draft the fix. I reproduced the bug and verified the fix locally myself.