fix(graph_dbs): sanitize FTS query words with mixed content#1259
Open
zerone0x wants to merge 1 commit intoMemTensor:mainfrom
Open
fix(graph_dbs): sanitize FTS query words with mixed content#1259zerone0x wants to merge 1 commit intoMemTensor:mainfrom
zerone0x wants to merge 1 commit intoMemTensor:mainfrom
Conversation
FTS queries fail when the query string contains mixed content such as message IDs with underscores (e.g. `om_x100b544a390604b8c3e1b7d8641f08e`) combined with Chinese text. The raw words are passed directly to PostgreSQL `to_tsquery()` which expects valid tsquery syntax and chokes on special characters. Add `_sanitize_tsquery_words()` helper that strips tsquery-breaking characters while preserving alphanumeric, CJK, and underscore chars. Apply the sanitization in both `search_by_fulltext` and `search_by_keywords_tfidf` before building the tsquery string. Fixes MemTensor#1247 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes PostgreSQL full-text search (FTS) failures when query tokens contain mixed content (e.g., message IDs with underscores plus CJK text) by sanitizing query words before building a to_tsquery() expression.
Changes:
- Added
_sanitize_tsquery_words()helper inpolardb.pyto strip tsquery-breaking characters and deduplicate tokens. - Applied sanitization in
search_by_fulltextandsearch_by_keywords_tfidfbefore constructing theto_tsquery()parameter. - Added a new unit test module covering expected sanitization behaviors.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
src/memos/graph_dbs/polardb.py |
Adds and uses _sanitize_tsquery_words() to prevent invalid to_tsquery() inputs for mixed-content tokens. |
tests/graph_dbs/test_sanitize_tsquery.py |
Adds unit tests intended to validate sanitization behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+1
to
+27
| """Tests for _sanitize_tsquery_words — standalone, no heavy imports.""" | ||
|
|
||
| import re | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Inline the function under test to avoid pulling in the full memos import | ||
| # chain (which requires a running logging backend). The canonical copy lives | ||
| # in ``memos.graph_dbs.polardb._sanitize_tsquery_words``. | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| def _sanitize_tsquery_words(query_words: list[str]) -> list[str]: | ||
| valid_chars_re = re.compile( | ||
| r"[^\w\u4e00-\u9fff\u3400-\u4dbf\uf900-\ufaff]", | ||
| ) | ||
| sanitized: list[str] = [] | ||
| seen: set[str] = set() | ||
| for w in query_words: | ||
| w = w.strip().strip("'") | ||
| cleaned = valid_chars_re.sub("", w) | ||
| if cleaned and cleaned not in seen: | ||
| seen.add(cleaned) | ||
| sanitized.append(cleaned) | ||
| return sanitized | ||
|
|
||
|
|
Comment on lines
+1685
to
+1686
| # Sanitize and convert query_text to OR query format: "word1 | word2 | word3" | ||
| safe_words = _sanitize_tsquery_words(query_words) |
Comment on lines
+35
to
+38
| # Keep word characters (letters, digits, underscore) and CJK unified ideographs. | ||
| valid_chars_re = re.compile( | ||
| r"[^\w\u4e00-\u9fff\u3400-\u4dbf\uf900-\ufaff]", | ||
| ) |
Comment on lines
+25
to
+38
| """Sanitize query words for safe use with PostgreSQL to_tsquery(). | ||
|
|
||
| Strips tsquery operator characters and other special symbols that can | ||
| cause parsing errors when mixed content (e.g. message IDs with | ||
| underscores, Chinese text) is passed to ``to_tsquery``. Each word is | ||
| reduced to its alphanumeric/CJK core so that the jieba text-search | ||
| configuration can tokenize it correctly. | ||
|
|
||
| Returns a de-duplicated list of non-empty sanitized words. | ||
| """ | ||
| # Keep word characters (letters, digits, underscore) and CJK unified ideographs. | ||
| valid_chars_re = re.compile( | ||
| r"[^\w\u4e00-\u9fff\u3400-\u4dbf\uf900-\ufaff]", | ||
| ) |
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.
Summary
to_tsquery()which expects valid tsquery syntax_sanitize_tsquery_words()helper that strips tsquery-breaking characters (operators, punctuation) while preserving alphanumeric, CJK unified ideographs, and underscore characterssearch_by_fulltextandsearch_by_keywords_tfidfbefore building the tsquery stringFixes #1247
Test plan
_sanitize_tsquery_wordscovering: plain English, Chinese text, mixed content (the original bug scenario), single-quoted inputs, special character removal, deduplication, empty inputs, and tsquery operator stripping🤖 Generated with Claude Code