Skip to content

Search results should return richer content context for content-hit scoring #609

Closed

Description

@bm-clawd

Problem

In benchmark evaluation (LoCoMo), BM finds the correct document 74.3% of the time (R@5) but the retrieved text only contains the exact answer 15.5% of the time (content hit rate). Mem0 gets 34.3% content hit with lower recall.

The gap is because search results return minimal text context:

  • matched_chunk: the single best-matching chunk (max 900 chars) — may not contain the answer even when the right note was found
  • content: the entity's content_snippet — often just the note header/title area

Root cause

  1. Single chunk returned: _search_vector_only() keeps only the best chunk per search_index row (best_chunk_by_si_id). If the answer is in chunk 3 but chunk 1 matched the query better, the answer text is lost.

  2. content_snippet truncation: The content field in search results comes from content_snippet on the search index, which for entities is a truncated version of the note. Long session notes get their content cut off.

  3. Observation-level hits: For person/topic entities with few observations, the matched chunk is just a single observation line like - [role] Conversation participant. The actual conversation content lives in session notes.

Proposed improvements

Option A: Return top-N chunks per result (preferred)

Instead of just the best matching chunk, return the top 2-3 chunks from the same entity. This preserves more context without returning the entire note.

# Instead of just best_chunk_by_si_id[si_id] = chunk_text
# Keep top 3 chunks per entity
top_chunks_by_si_id[si_id].append((similarity, chunk_text))
# Then join them: matched_chunk = '\n'.join(top 3 by similarity)

Option B: Return full content for small notes

If a note is under some threshold (e.g., 2000 chars), return the full content instead of just the snippet. Most LoCoMo notes are conversation sessions that fit in this range.

Option C: Add matched_chunks (plural) field

Return an array of all chunks that exceeded the similarity threshold, preserving the current matched_chunk for backward compat.

Benchmark evidence

Full LoCoMo run (locomo-full-20260226T055634Z):

Provider R@5 Content Hit
bm-local 74.3% 15.5%
mem0-local 64.6% 34.3%

BM finds the right doc 10 points more often but surfaces the answer text 19 points less often.

Impact

This affects real users too — when an LLM gets search results, having more context per hit means better answers even when retrieval is correct. This is the gap between 'found the right note' and 'gave the right answer.'

Activity

  1. claude commented on Feb 26, 2026

    @claude
    Contributor

    Claude finished @bm-clawd's task in 2m 56s —— View job


    Triage Analysis

    • Analyze issue and explore search codebase
    • Classify issue type, priority, and complexity
    • Check for duplicate issues
    • Add appropriate labels
    • Provide triage summary

    Issue Classification

    Attribute Value
    Type Enhancement
    Priority High
    Component Database / Search
    Complexity Medium

    No duplicates found. Issue #603 (time-decay scoring) is related but distinct.


    Code Analysis

    I explored the search implementation and confirmed the root causes described in the issue:

    1. Single best-chunk selection (search_repository_base.py:877-891):

    # Current: only keeps the single best-matching chunk per search_index row
    if current is None or similarity > current:
        similarity_by_si_id[si_id] = similarity
        best_chunk_by_si_id[si_id] = chunk_text

    The loop over vector results keeps only the highest-similarity chunk per si_id. If the answer is in chunk 3 but chunk 1 has a better embedding match, the answer text is discarded.

    2. Severe content truncation (search_index_row.py + utils.py):

    • CONTENT_DISPLAY_LIMIT = 250 chars — the content field shown in SearchResult is truncated at 250 characters
    • content_snippet in the database stores the full markdown, but only the first 250 chars are surfaced via the content property

    3. MAX_VECTOR_CHUNK_CHARS = 900 — individual chunks max out at 900 chars, so even the matched_chunk field is capped at 900 chars


    Feasibility Assessment

    Option A — Top-N chunks per result (recommended)

    • Straightforward change to _search_vector_only(): accumulate top-3 chunks instead of replacing with best
    • Minimal schema change — join chunks with \n---\n separator into existing matched_chunk field
    • Backward compatible
    • Estimated effort: Small — ~30 lines in search_repository_base.py + test updates

    Option B — Full content for small notes

    • Would need to pass content_snippet (or full entity text) alongside each result
    • The data is already in search_index.content_snippet — just needs to be surfaced when len(content_snippet) < threshold
    • Could combine with Option A
    • Estimated effort: Small — schema + API utils change

    Option C — matched_chunks array field

    • Cleanest API but breaks existing clients
    • Could introduce as additive field alongside existing matched_chunk
    • Estimated effort: Medium — schema change + migration + API update

    Recommended Approach

    Combining Option A + Option B gives the largest benefit with minimal risk:

    1. Keep top-3 chunks per result entity in vector search (replaces single-best-chunk logic)
    2. If content_snippet is under a threshold (e.g., 2000 chars), return it as content instead of the 250-char truncation
    3. No breaking schema changes needed — both fit into existing matched_chunk and content fields

    The fix is localized to:

    • src/basic_memory/repository/search_repository_base.py (~30 lines)
    • src/basic_memory/schemas/search.py (possibly raise content display limit)
    • src/basic_memory/api/v2/utils.py (adjust truncation logic)

    Labels Applied

    • enhancement ✓
  2. added
    enhancement
    on Feb 26, 2026
  3. added this to the v0.19.0 milestone on Feb 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions