fix(cli): multi-word and literal-wildcard search in odysseus-docs (#7007)

* fix(cli): multi-word and literal-wildcard search in odysseus-docs

odysseus-docs search wrapped the whole query in one `%{query}%` LIKE over title/current_content, so multi-word queries only matched the exact adjacent phrase and literal % / _ acted as SQL wildcards. Split on whitespace, AND per token, and escape LIKE wildcards with the shared escape_like_pattern() helper. Whitespace-only queries now match all documents, consistent with document-library search.

Fixes #2123.

Tests: tests/cli/test_docs_cli_search.py (5 tests; multi-word, %, _, backslash, whitespace); 4 fail pre-fix, 5 pass post-fix. tests/cli full suite: 82 passed.

* test(cli): name whitespace-query search test accurately

The test passes a whitespace-only query, not an empty one; the old name implied a different input. Behavior is unchanged.
This commit is contained in:
Merajul Arefin
2026-11-02 23:48:14 +00:00
committed by GitHub
parent c1f959ae61
commit f00ae350cd
3 changed files with 116 additions and 5 deletions
+12 -4
View File
@@ -27,6 +27,7 @@ from pathlib import Path
try:
from core.database import SessionLocal, Document, DocumentVersion
from src.session_search import escape_like_pattern
quiet_logs()
except ModuleNotFoundError as e:
sys.stderr.write(f"error: {e}\nhint: run from repo root with venv active.\n")
@@ -139,10 +140,17 @@ def cmd_export(args):
def cmd_search(args):
db = SessionLocal()
try:
like = f"%{args.query}%"
rows = db.query(Document).filter(
(Document.title.ilike(like)) | (Document.current_content.ilike(like))
).order_by(Document.updated_at.desc()).limit(args.limit).all()
# Per-term AND search: a single `%foo bar%` LIKE only matched the
# exact phrase, so "machine learning" missed docs containing both
# words apart. LIKE wildcards are escaped per token.
q = db.query(Document)
for tok in args.query.split():
term = f"%{escape_like_pattern(tok)}%"
q = q.filter(
Document.title.ilike(term, escape="\\")
| Document.current_content.ilike(term, escape="\\")
)
rows = q.order_by(Document.updated_at.desc()).limit(args.limit).all()
emit([_serialize(d) for d in rows], args)
finally:
db.close()