Skip to content

Vector search silently drops results when entity and relation search_index ids collide #982

Closed

Description

@Funnypie

Describe the bug

Semantic/vector search silently omits notes from results when an entity row and a relation (or observation) row in search_index share the same numeric id. Because each row type has its own auto-increment sequence, collisions are essentially guaranteed in any young database (entity ids 1..N overlap relation ids 1..M), and the affected notes are simply never returned by vector or hybrid search — no error, no log.

Root cause

In src/basic_memory/repository/search_repository_base.py:

  1. _search_vector_only parses each vector hit's chunk_key (e.g. entity:4:0) but discards the type, keeping only the bare id:
    _, si_id = self._parse_chunk_key(chunk_key)
  2. _fetch_search_index_rows_by_ids then fetches WHERE id IN (...) with no type filter and builds a dict keyed by bare id:
    result[row.id] = SearchIndexRow(...)
    When an entity row and a relation row share an id, whichever the database returns last overwrites the other in the dict. The clobbered row's similarity entry then finds row is None and is dropped.

Note the FTS-filter branch a few lines above already guards against exactly this with (id, type) tuple keys ("Use (id, type) tuples to avoid collisions between different search_index row types that share the same auto-increment id") — but the primary lookup path doesn't.

Concrete example (real database, basic-memory 0.21.6, SQLite backend)

search_index contained entity rows with ids 1–4 and relation rows with ids 1, 2, 4, 5:

  • entity 1 ("Learner Profile") — clobbered by relation 1 → never returned
  • entity 2 — its row happened to be inserted after relation 2 → returned
  • entity 3 — no colliding relation id 3 → returned
  • entity 4 (a concept note) — clobbered by relation 4 → never returned

A direct KNN query against search_vector_embeddings ranked entity 4's chunk #1 for the test query (distance 0.64), confirming the embeddings are fine — the result is lost purely in the id→row hydration step. Searching with min_similarity=0 and verbatim text from the affected notes still returns nothing for them.

Expected behavior

Every vector hit should hydrate against the search_index row of the same type. Keying similarity_by_si_id / chunks_by_si_id / _fetch_search_index_rows_by_ids by (type, id) tuples fixes it (verified locally on 0.21.6 — the dropped notes immediately return at the expected ranks).

Environment

  • basic-memory 0.21.6 (bug also present in v0.22.0 source and current main)
  • SQLite backend, semantic search enabled (fastembed, bge-small-en-v1.5)
  • Windows 11

Activity

  1. phernandez commented on Jun 12, 2026

    @phernandez
    Member

    @Funnypie thanks for submitting this issue!

    @claude take a look at this one

  2. claude commented on Jun 12, 2026

    @claude
    Contributor

    Claude finished @phernandez's task in 9m 0s —— View job • Create PR ➔


    Fixing vector search id collision bug

    • Read and analyze search_repository_base.py
    • Implement fix: use (type, id) tuple keys throughout the vector search path
    • Update existing tests to use new tuple-keyed mock return values
    • Add regression test for the collision scenario
    • Run tests (blocked — pytest requires tool approval in this run)
    • Push changes — commit 4d37f59 is ready locally; push blocked by 403 (github-actions[bot] token lacks write access on this run)

    Changes made

    src/basic_memory/repository/search_repository_base.py

    Renamed similarity_by_si_id / chunks_by_si_id → similarity_by_si_key / chunks_by_si_key and changed their key type from int to tuple[str, int]. Updated _fetch_search_index_rows_by_ids to accept list[tuple[str, int]] and key its return dict by (row.type, row.id). Entity id=4 and relation id=4 now live under distinct keys ("entity", 4) and ("relation", 4) — neither overwrites the other.

    The SQL query is unchanged (WHERE id IN (...)); type discrimination is applied at Python dict-key construction time using row.type, which is both simpler and zero-overhead versus adding per-row SQL conditions.

    tests/repository/test_vector_threshold.py

    • Updated all _fetch_search_index_rows_by_ids mock return values from {i: FakeRow(id=i)} to {("entity", i): FakeRow(id=i)}.
    • Added test_entity_and_relation_with_same_id_both_returned — direct regression test for the bug. Creates chunk keys entity:4:0 and relation:4:0, asserts both come back (before this fix only one did).

    tests/repository/test_vector_pagination.py

    • Updated fake_index_rows mock to use ("entity", i) tuple keys.

    Note to maintainer: The workflow token (github-actions[bot]) doesn't have write access on this run, so the commit can't be pushed. You can push claude/issue-982-20260612-0300 manually (git push origin claude/issue-982-20260612-0300) or re-trigger with the bm-claudeai app token.

  3. self-assigned this
    on Jun 12, 2026
  4. added a commit that references this issue on Jun 12, 2026
    253e240
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions