nextcloud / nextcloud/context_chat_backend

Selective Context can return 0 results for a user with a small share of a large collection (HNSW post-filter)

Open
#320 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
27
Forks
24
Avg merge
3d 4h
Merged PRs (30d)
6

Description

With Selective Context (a scoped provider), retrieval can return 0 documents for a user whose indexed chunks are only a small fraction of a large collection; users with small corpora are fine.

Cause: VectorDB._similarity_search runs roughly ... WHERE id IN (<chunk_ids>) ORDER BY embedding <=> :q LIMIT k. For a large IN list the planner can pick an HNSW index scan: it finds the globally-nearest ~hnsw.ef_search vectors first, then post-filters by id. If none of the user's chunks are among the global-nearest, the result is empty.

Two ways to address:

  • pgvector ≥ 0.8.0 native: SET hnsw.iterative_scan = relaxed_order (off by default) — HNSW keeps scanning until enough rows pass the filter. Simplest where available.
  • App-side (what we used on 5.3.x): a MATERIALIZED CTE that pre-filters by id (btree) before computing similarity, so the HNSW plan is never chosen — deterministic and exact.

Note: the plan choice is cost-dependent — in a re-test the planner used the btree id index and returned correct rows, so the 0-result plan isn't guaranteed; it's a latent risk under certain id-list sizes/stats rather than a constant failure.

(Disclosure: investigated with AI assistance; verified against the source and on a live deployment.)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading VectorDB._similarity_search and reproduce the empty-result case with a large filtered id list, checking the PostgreSQL query plan. Compare the available pgvector iterative-scan behavior with the app-side pre-filtering approach described in the issue. Done means Selective Context returns eligible documents reliably instead of zero results under the affected planner conditions.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, python, sql
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.