SQLite CJK Full-Text Search (FTS5 trigram)
Make Chinese search actually work in SQLite — including the silent two-character blind spot.
Free
SQLite CJK Full-Text Search (FTS5 trigram)
Example session with this skill installed
I need to add Chinese search to my SQLite messages table. The table has columns id and content. Show me the table setup and a Python search function that won't fail on 2-character words.
- Read your context and instructions
- Compiled the sqlite cjk full-text
This implementation sets up an FTS5 virtual table using the trigram tokenizer and provides a Python search function that routes short queries to a standard LIKE search to bypass the trigram 3-character limit.
SQLite CJK Search Implementation
-- 1. Create the virtual table for search
CREATE VIRTUAL TABLE IF NOT EXISTS messages_fts USING fts5(
content,
tokenize='trigram'
);
-- 2. Rows are written to messages and messages_fts in the same transaction (see the Python below)
import sqlite3
# Write each new row to messages and messages_fts in the same transaction (rowid = messages.id)
with sqlite3.connect(DB_PATH) as conn:
cur = conn.execute("INSERT INTO messages (content, ts) VALUES (?, ?)", (content, ts))
conn.execute("INSERT INTO messages_fts (rowid, content) VALUES (?, ?)", (cur.lastrowid, content))
conn.commit()
def search_cjk(db_path: str, keyword: str, limit: int = 10):
kw = (keyword or "").strip()
if not kw: return []
with sqlite3.connect(db_path) as conn:
try:
# Handle the trigram < 3 character limitation
if len(kw) < 3:
# Fallback to LIKE for short Chinese terms (e.g., "今天")
safe = kw.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_")
query = "SELECT id, content FROM messages WHERE content LIKE ? ESCAPE '\\' LIMIT ?"
params = (f"%{safe}%", limit)
else:
# Use FTS5 for longer queries, escaping internal quotes
phrase = '"' + kw.replace('"', '""') + '"'
query = """
SELECT m.id, m.content FROM messages_fts f
JOIN messages m ON m.id = f.rowid
WHERE f.content MATCH ? LIMIT ?
"""
params = (phrase, limit)
return conn.execute(query, params).fetchall()
except sqlite3.OperationalError as e:
print(f"Search failed: {e}")
return None
Next steps
- Keep messages_fts in sync by writing INSERT/UPDATE/DELETE to both tables in the same transaction (triggers are only needed if you switch to an external-content table).
- Verify that searches for 2-character words return results via the
LIKEbranch. - Use
rowidmapping to ensure FTS results correctly join back to your primary metadata.
Connects securely to your tools. The creator never sees your data.
About this skill
A methods skill for Chinese/CJK keyword search with SQLite FTS5 trigram. It explains why the default tokenizer treats a whole sentence as one token, why queries shorter than three characters return an empty set with no error, and how to fall back to escaped LIKE plus safe MATCH phrase escaping. Includes copy-paste SQL and Python patterns. Knowledge and code samples only; no bundled executable.
Trigram matching needs 3+ characters; shorter queries use the LIKE fallback shown in the sample code (slower full scan).
Keyword search only, no semantic/embedding matching.
Index sync is your app's job: same-transaction writes, or all three triggers for external-content tables.
Don't point the index at another framework's live state.db (WAL) or a network-synced SQLite file: extra writers make the original writer refuse to write, and cross-host sharing breaks WAL consistency. Create your own DB.
How to use
- 1
Connect your agent to the Agensi MCP
Copy the prompt and paste it into Claude, ChatGPT, Cursor or any agent that supports MCP. It sets up the connection for you. Manual setup
- 2
Sign in to Agensi
Your agent shows a sign-in link. Sign in and click Allow.
- 3
Ask for what you need
Your agent loads this skill and uses it on your task. It always asks before buying a skill.
Reviews
No reviews yet
Be one of the first to try it. Every listed skill goes through the trust checks below.
Security scanned
Checked by our 8-point scan before listing
Fresh listing
Recently published to Agensi
Free to use
No account required to browse
Trust & safety
Security scan
Security scan passed 16 days ago
- Free with an Agensi account
Permissions
No special permissions declared or detected
What's inside
Frequently Asked Questions
Find more skills like this on the Claude Skills Marketplace, the Codex Skills Marketplace and the Gemini Skills Marketplace, or browse All Claude Skills.