Repository navigation
Description
Problem
Search results truncate content_snippet to 250 chars via SearchIndexRow.content property (CONTENT_DISPLAY_LIMIT = 250 in src/basic_memory/repository/search_index_row.py:45). This means search always returns the beginning of the note, not the relevant section that matched the query.
Evidence (Benchmark)
Ran 38-query retrieval benchmark against plugin MCP server:
- Recall@5: 94.6% — we find the right notes
- Content Hit Rate: 21.4% — but the returned snippet rarely contains the specific fact
Example: query "What is the team plan pricing?" finds the correct note (2026-02-12.md) but returns:
# 2026-02-12 — Wednesday\n\n## Observations\n- [date] 2026-02-12\n- [type] daily-note\n\n## Standup\n- Maya integrating Linear cycle time API...
The pricing info ($9/mo per seat) is deeper in the note, past the 250-char cutoff.
Root Cause Chain
SearchIndexRow.content_snippetstores full content (good)SearchIndexRow.contentproperty truncates to first 250 chars (bad for relevance)SearchResult.contentgets the truncated version (src/basic_memory/api/v2/utils.py:183:content=r.content)- MCP search tool returns truncated content to agents
- Agents/benchmarks only see note header, not the relevant section
Proposed Fix
Add matched_chunk field to SearchResult
The vector search pipeline already matches against specific chunks (chunk_key = "{type}:{id}:{chunk_index}"). The matching chunk text contains the relevant section but is discarded before results are returned.
Changes needed:
1. src/basic_memory/schemas/search.py — Add field to SearchResult
class SearchResult(BaseModel):
# ... existing fields ...
content: Optional[str] = None
matched_chunk: Optional[str] = None # NEW: the chunk text that scored highest in vector search2. src/basic_memory/repository/search_index_row.py — Add chunk text field
@dataclass
class SearchIndexRow:
# ... existing fields ...
matched_chunk_text: Optional[str] = None # Populated by vector search3. src/basic_memory/repository/sqlite_search_repository.py — Thread chunk text through vector results
In _vector_search(), the query already JOINs search_vector_chunks. The chunk_text column is available but not selected. Add it to the result:
-- Current (approx line 807):
SELECT c.entity_id, c.chunk_key, vector_matches.distance AS best_distance
FROM ... JOIN search_vector_chunks c ON c.id = vector_matches.rowid
-- Proposed:
SELECT c.entity_id, c.chunk_key, c.chunk_text, vector_matches.distance AS best_distance
FROM ... JOIN search_vector_chunks c ON c.id = vector_matches.rowidThen populate matched_chunk_text on the SearchIndexRow when building results.
4. src/basic_memory/repository/postgres_search_repository.py — Same change for Postgres
5. src/basic_memory/api/v2/utils.py — Pass through to SearchResult
SearchResult(
# ... existing fields ...
content=r.content,
matched_chunk=r.matched_chunk_text, # NEW
)6. Fallback for FTS-only search
When vector search is not used (text-only mode), matched_chunk will be None. Consider a lightweight fallback: find the paragraph in content_snippet that has the best keyword overlap with the query and return that as the snippet.
Impact
- Benchmark content hit rate should jump from 21.4% to 70%+ with chunk-level snippets
- Agent quality improves — agents get the relevant section, not the note header
- No breaking changes —
matched_chunkis a new optional field,contentbehavior unchanged - Token efficiency — agents get a focused snippet instead of needing to
read_notefor every search result
Files to Modify
src/basic_memory/schemas/search.py(SearchResult model)src/basic_memory/repository/search_index_row.py(SearchIndexRow dataclass)src/basic_memory/repository/sqlite_search_repository.py(vector search query + result mapping)src/basic_memory/repository/postgres_search_repository.py(same for postgres)src/basic_memory/repository/search_repository_base.py(hybrid merge logic)src/basic_memory/api/v2/utils.py(SearchResult construction)- Tests in
tests/repository/andtests/api/
Benchmark Reference
- Plugin repo:
benchmark/run.tsandbenchmark/results/bm-mcp-small-results.json - 38 queries, 9 categories, 11-file corpus
- Full results at: https://github.com/basicmachines-co/openclaw-basic-memory/tree/main/benchmark
Related
- Closes the content quality gap identified in retrieval benchmarks
- Supports SPEC-30 (Graph DB Pro) by establishing baseline metrics
- BUG: Hybrid RRF fusion dilutes vector scores with weak FTS scores #577 (hybrid RRF fusion, now closed) fixed scoring; this fixes snippets
Activity
claude commentedon Feb 22, 2026
Claude finished @bm-clawd's task in 1m 45s —— View job
Issue Triage
Classification
No duplicate issues found.
Technical Verification
I read all the relevant source files and confirmed the issue analysis is accurate:
Root cause confirmed:
_run_vector_queryinsqlite_search_repository.py:456andpostgres_search_repository.py:427already JOINsearch_vector_chunks cbut only SELECTc.entity_id, c.chunk_key, vector_matches.distance— thechunk_textcolumn is present in the table schema but not included in results.SearchIndexRow.contentatsearch_index_row.py:44-48truncates to 250 chars from the beginning of the fullcontent_snippet, not from the matched position.api/v2/utils.py:179mapscontent=r.contentwhich uses the truncated property.The fix is straightforward in isolation — adding
c.chunk_textto the SELECT is a one-line SQL change in each backend — but threading it through the pipeline correctly requires care:Key consideration for
_search_hybrid: The RRF merge atsearch_repository_base.py:1121-1127usesrows_by_id[row.id] = rowwhich overwrites with the last-seen result. When a document appears in both FTS and vector results, thematched_chunk_textfrom the vector result could be lost if the FTS row wins the overwrite. The merge logic should preservematched_chunk_textfrom vector results when building fused rows.FTS-only fallback: The issue mentions finding the best-matching paragraph for FTS queries (where no vector chunk is selected). This is a worthwhile improvement but can be done as a follow-up — the core fix (vector chunk passthrough) already covers hybrid and vector-only modes.
Proposed Implementation Scope
The issue's proposed fix is sound. Files to change:
src/basic_memory/repository/sqlite_search_repository.py— Addc.chunk_textto_run_vector_querySELECT (line ~456)src/basic_memory/repository/postgres_search_repository.py— Same for Postgres (line ~427)src/basic_memory/repository/search_index_row.py— Addmatched_chunk_text: Optional[str] = Nonefieldsrc/basic_memory/repository/search_repository_base.py— Populatematched_chunk_textfrom vector results; handlematched_chunk_textpreservation in hybrid mergesrc/basic_memory/schemas/search.py— Addmatched_chunk: Optional[str] = NonetoSearchResultsrc/basic_memory/api/v2/utils.py— Passmatched_chunk=r.matched_chunk_textinSearchResultconstructionLabels applied:
enhancementreturn matched chunk text in search results