pi-memory icon indicating copy to clipboard operation
pi-memory copied to clipboard

Bug Report: FTS5 search fails for CJK (Chinese) characters

Open paomaostudio opened this issue 6 months ago • 1 comments

Bug Report: FTS5 search fails for CJK (Chinese) characters

Description

memory_search tool cannot find memories when searching with Chinese keywords. The FTS5 full-text search uses SQLite's default simple tokenizer, which doesn't handle CJK text properly.

Root Cause

SQLite FTS5's simple tokenizer splits tokens by spaces and punctuation. For Chinese text without spaces, consecutive characters are treated as a single token. For example:

  • Stored value: 查询小米 MiMo 套餐用量
  • FTS5 tokenizes as: ["查询小米", "MiMo", "套餐用量"]
  • Searching for 小米 fails because the token 小米 doesn't exist — only 查询小米 does

Reproduction

// Store a fact with Chinese content
store.setSemantic("mimo.platform.usage", "查询小米 MiMo 套餐用量必须用 agent-browser...", 0.95, "user");

// Search fails
store.searchSemantic("小米", 10);  // Returns [] (empty)
store.searchSemantic("套餐", 10);  // Returns [] (empty)

// But these work (English keywords)
store.searchSemantic("mimo", 10);     // Returns 3 results
store.searchSemantic("agent", 10);    // Returns 6 results

Suggested Fix

In searchSemantic(), when FTS5 returns 0 results, fall back to _searchSemanticFallback() (which uses LIKE-based substring matching):

const rows = this.db.prepare(`...`).all(ftsQuery, limit);

// Add this check:
if (rows.length > 0) return rows;
return this._searchSemanticFallback(query, limit);

This is a minimal fix. A more comprehensive solution would be to use FTS5's unicode61 tokenizer with tokenchars configured for CJK, or integrate a CJK-aware tokenizer like jieba.

Environment

  • @samfp/pi-memory version: 1.3.2
  • Node.js: v22+
  • SQLite: via node:sqlite (built-in)
  • OS: Linux (Ubuntu 22.04)

Impact

Chinese-speaking users cannot effectively search their memory store. English searches work fine. This affects a significant portion of the user base.

paomaostudio avatar Jun 02 '26 16:06 paomaostudio

Fixed in v1.3.4. When FTS5 returns empty results, both searchSemantic and searchLessons now fall back to LIKE-based substring matching — which handles CJK and any other text where FTS5 tokenization misses substrings.

samfoy avatar Jun 02 '26 18:06 samfoy