Skip to content

Neo4j vector search returns empty results in shared-database multi-tenant mode #1360

Closed

Description

@CaralHsi

Description

When using Neo4j in shared-database multi-tenant mode (use_multi_db=False), search_by_embedding returns empty results for specific users even though their data exists in the database.

Root Cause

  1. Post-filter problem: db.index.vector.queryNodes(k, embedding) returns the global top-k most similar nodes, then WHERE clauses (user_name, memory_type, status) filter them further. In a shared database with many users, the target user's nodes are often excluded from the initial top-k, yielding 0 results after filtering.

  2. sources KeyError: metadata["sources"] is accessed without .get() in multiple locations in neo4j.py and neo4j_community.py. When metadata is serialized with model_dump(exclude_none=True) and sources is None, the key is absent, causing a KeyError.

  3. _parse_node deserialization bug: The sources JSON deserialization check node["sources"][idx][0] == "}" should be node["sources"][idx][-1] == "}" (last char, not first), making json.loads never execute.

  4. neo4j_community.py add_node crashes on missing embedding: raise ValueError when embedding is None, but some memory types (e.g., UserMemory) may not have embeddings.

Expected Behavior

  • Vector search should return correct results for any user in a shared database, regardless of how many other users' data exists.
  • Adding nodes without sources or embedding should not crash.

Environment

  • Neo4j >= 5.18 (required for vector.similarity.cosine() pre-filtering)
  • Shared-database multi-tenant mode (use_multi_db=False)

Activity

  1. lvca commented on Apr 5, 2026

    @lvca

    Hi, I'm Luca Garulli, creator of ArcadeDB, and wanted to flag something that might help here.

    The root cause you've identified is a known architectural limitation of Neo4j's db.index.vector.queryNodes: it operates on the full graph and returns global top-k candidates before any WHERE predicates are applied, which is why per-tenant filtering collapses results to zero for users with few nodes.

    ArcadeDB is an Apache 2.0 multi-model database that is Bolt-compatible (same protocol as Neo4j), so it works with existing Python Neo4j driver code without rewriting your query layer. The key difference: ArcadeDB's vector index supports filtered ANN, predicates like user_name, memory_type, and status are pushed into the index scan rather than applied after retrieval. That means per-tenant queries return accurate results regardless of how sparse the user's data is.

    Beyond this specific bug, ArcadeDB also handles Map-type property values natively (relevant to issue #1122) and runs as a single lightweight process or fully embedded: no separate container required.

    Happy to help with a quick integration test or point you to a working LightRAG/Graphiti adapter that uses ArcadeDB as a drop-in Neo4j replacement if that would be useful.

  2. added
    types:bugSomething isn't working | 功能异常
    area:coreMOS 编排层 / 框架底座 / 跨模块问题
    on Jun 1, 2026
  3. added
    ai:taskDispatched to AI coding agent | 已派发给 AI 编码任务
    on Jun 29, 2026
  4. Memtensor-AI commented on Jun 29, 2026

    @Memtensor-AI
    Collaborator

    🤖 AutoDev has picked up this issue and started working on it.

    Task ID: cca6c0d25ac3db4e
    Working branch: bugfix/autodev-1360
    Target branch: latest dev* branch
    Workflow: opsp (analysis → coding → testing → PR)

    I will post the PR link here once done. If I need more information, I will ask in the comments.

  5. self-assigned this
    on Jun 29, 2026
  6. Memtensor-AI commented on Jun 29, 2026

    @Memtensor-AI
    Collaborator

    ✅ AutoDev task cca6c0d25ac3db4e completed.

    Summary: Fixes issue #1360 by removing a leftover regression in the Neo4j sources serialization path. PR #1359 had already addressed the four root causes listed in the issue (post-filter rewrite, sources KeyError guard, _parse_node bracket check, missing-embedding warn-instead-of-raise), but a close re-read showed sources were still being double-encoded: _prepare_node_metadata JSON-dumps each element once, and then add_node / add_nodes_batch in neo4j.py plus add_node in neo4j_community.py repeated the same json.dumps step. The resulting values came back from Neo4j with a leading " instead of {, so _parse_node's [0] == "{" guard skipped json.loads and callers received escaped JSON strings instead of dicts — the exact deserialization-skip symptom listed as bug #3 of the original issue, just one layer upstream.

    The change removes the redundant serialization at all three sites, leaving the single canonical encode inside _prepare_node_metadata (matching the already-correct pattern in import_graph and neo4j_community.add_nodes_batch). Existing rows written with the double-encoded value remain readable as escaped strings (no new exception), and new writes round-trip correctly.

    Tests: added TestSourcesDoubleSerializationRegression to tests/graph_dbs/test_neo4j_vector_search.py with three cases — add_node single-encode invariant, add_nodes_batch single-encode invariant, and full add_node → _parse_node round-trip. Results: python3 -m pytest tests/graph_dbs/ → 32 passed / 3 skipped (skipped cases are live-Neo4j 5.18+ integration tests). ruff check and ruff format --check are clean on all three touched files.

    Base branch: main
    Branch: bugfix/autodev-1360
    Commit: 2603999
    PR: #1974
    Assigned to: @MatthewZhuang, @CarltonXiang, @syzsunshine219, @World-controller
    Reviewers: @MatthewZhuang, @CarltonXiang, @syzsunshine219, @World-controller

  7. added
    ai:testingAI agent is running tests | AI 正在运行测试
    and removed
    ai:codingAI agent is coding | AI 正在编码
    on Jun 29, 2026
  8. added
    area:databasegraph_db + vector_db | 图数据库与向量数据库
    and removed
    types:bugSomething isn't working | 功能异常
    ai:taskDispatched to AI coding agent | 已派发给 AI 编码任务
    area:coreMOS 编排层 / 框架底座 / 跨模块问题
    ai:testingAI agent is running tests | AI 正在运行测试
    on Jul 7, 2026
  9. added theissue type on Jul 7, 2026
  10. added
    status:readyReady for implementation; waiting for assignee or AI dispatch | 可进入实现,等待认领或派发
    on Jul 8, 2026
  11. Memtensor-AI commented on Jul 13, 2026

    @Memtensor-AI
    Collaborator

    Resolved by #1359. The merged fix is already included in main and released in v2.0.23, so closing this issue as completed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area:databasegraph_db + vector_db | 图数据库与向量数据库status:readyReady for implementation; waiting for assignee or AI dispatch | 可进入实现,等待认领或派发

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions