Pass 3 of 3 · Deep dive of PHAROSPass 1 · OverviewPass 2 · How it works
Code Observatory / Source
Dense search with pgvector
Cosine distance through the HNSW index, filtered to one embedding model, with an iterative scan on pgvector 0.8 or later.
PINNED SOURCE Copied from the repository at this commit. Source, not an execution.
PHAROS at 975a38dMIT license
Retriever._densebackend/app/codeintel/retrieval.py, lines 370–405Open source
def _dense(self, qvec: list[float], filters: Filters, limit: int) -> list[tuple[str, float]]: assert self.embedder is not None model = self.embedder.model_id if not self.is_pg: rows = self.db.execute( select(DocumentChunk.id, DocumentChunk.embedding).where( DocumentChunk.embedding_model == model, *self._filter_clauses(filters) ) ).all() scored = [(str(cid), _cosine(qvec, emb)) for cid, emb in rows if emb] scored.sort(key=lambda x: (-x[1], x[0])) return scored[:limit] # Higher ef_search keeps recall up when filters discard candidates # after the HNSW scan (pgvector post-filters). pgvector >= 0.8 can # also keep scanning until enough rows pass the filter, which matters # once one database holds many repositories; results are re-sorted # below because the relaxed order is only approximately sorted. self.db.execute(text("SELECT set_config('hnsw.ef_search', :ef, true)"), {"ef": str(max(100, limit * 4))}) if _supports_iterative_scan(self.db): self.db.execute(text("SELECT set_config('hnsw.iterative_scan', 'relaxed_order', true)")) distance = DocumentChunk.embedding.op("<=>", return_type=Float)( bindparam("qvec", qvec, type_=EmbeddingVector()) ) stmt = ( select(DocumentChunk.id, distance.label("distance")) .where( DocumentChunk.embedding.isnot(None), DocumentChunk.embedding_model == model, *self._filter_clauses(filters), ) .order_by(distance) .limit(limit) ) rows = sorted(((str(cid), 1.0 - float(dist)) for cid, dist in self.db.execute(stmt)), key=lambda x: (-x[1], x[0])) return rowsWhere this comes from
- Commit
975a38d5d17839c26ee219295a5be6ac66b6d910- File
backend/app/codeintel/retrieval.py- Lines
- 370–405
- Git blob
884729a39ae0b3d340ad83f75946677dcddf3791- License
- MIT, © 2024–2026 Pharos Contributors. Notice
Read it on GitHub at this commit
Check it yourself
From a clone of the repository, the first command prints the blob above and the second prints these lines.
git rev-parse 975a38d:backend/app/codeintel/retrieval.py
git show 975a38d:backend/app/codeintel/retrieval.py | sed -n '370,405p'