NEXUS 如何用 PostgreSQL 16 + pgvector 為 6,000 個開源程式碼庫建立混合搜尋
How I Built Hybrid Search Across 6,000 Open-Source Repos with PostgreSQL 16 + pgvector (19.7s 1.8s)
Shivam Gupta 建立 NEXUS,使用 PostgreSQL 16 和 pgvector 為約 6,000 個 AI 及軟件工程開源程式碼庫提供混合搜尋。
Most of us still evaluate open-source tools by scrolling GitHub stars and skimming marketing READMEs.
I wanted a faster, evidence-backed way to find and compare production-ready repositories — so I built NEXUS, a hybrid search engine indexing ~6,000 AI & software engineering repositories and 252,000+ line-anchored documentation passages.
Here’s why I kept the entire search stack inside PostgreSQL 16 + pgvector, how hybrid ranking doubled retrieval quality, and the pgvector HNSW trap that cost me 18 seconds per query until I caught it with EXPLAIN ANALYZE.
Why PostgreSQL 16 + pgvector Instead of a Separate Vector DB?
When building semantic search, the default instinct is to spin up a separate vector database alongside your relational database. For NEXUS, that would have meant syncing stars, licenses, release tags, and archived status across two datastores.
Instead, keeping relational metadata, full-text search (tsvector GIN), and 384-d embeddings (pgvector HNSW) inside a single PostgreSQL 16 database gave me:
- One ACID query that combines vector similarity, keyword rank, star popularity, push freshness, and hard filters (license safety, language, category).
- Zero dual-write sync bugs and $0 extra vector-DB infrastructure.
For embeddings, I run sentence-transformers/all-MiniLM-L6-v2 (384-d) locally via ONNX Runtime (fastembed) on CPU — avoiding external embedding API latency/costs and keeping the container lightweight compared to PyTorch.
Why Hybrid Search Doubled Retrieval Quality
On an internal 30-query developer benchmark:
-
Pure Lexical (
tsvector):0.456MRR — nails exact library/API names (PagedAttention,AGPL-3.0), misses natural-language intent. -
Pure Semantic (
pgvector):0.465MRR — understands intent ("fast local LLM runner"), drifts on exact technical terms. -
NEXUS Hybrid:
0.936MRR (90.0%Hit@1,100%Hit@5) — fuses semantic cosine similarity, weightedtsvectorrank, log-scaled star popularity, 180-day half-life push freshness, and curation-list damping (so"awesome-*"lists don't crowd out real tools).
Every search result also surfaces commit-pinned line excerpts directly from READMEs and /docs, plus 1-click similar alternatives, 4-repo side-by-side comparison, commercial license risk tiers, and a 2D Relevance vs. Health map.
The pgvector HNSW Trap: 19.7s → 1.8s
Right before launching, searching a broad term like "vector" was taking 19.7 seconds on production.
The culprit? My vector CTE filtered by cosine distance without an ORDER BY ... LIMIT:
-- Slow: triggers a sequential scan over all 252k vectors!
SELECT chunk_id, 1 - (embedding <=> :query_vec) AS similarity
FROM chunk_embeddings
WHERE (embedding <=> :query_vec) < 0.85;
In pgvector, the HNSW index is only used when Postgres sees ORDER BY embedding <=> :query_vec LIMIT K. Extracting a materialized top_vec_chunks CTE fixed it immediately:
-- Fast: uses the HNSW cosine index (~20-50ms)
WITH top_vec_chunks AS MATERIALIZED (
SELECT chunk_id, (embedding <=> :query_vec) AS dist
FROM chunk_embeddings
ORDER BY embedding <=> :query_vec
LIMIT 300
)
SELECT ... FROM top_vec_chunks WHERE dist < 0.85;
That single change dropped live hybrid search from 19.7s → 1.87s and similar-repository lookups from timing out to ~1.08s.
Try It Live
NEXUS is live and free to use at https://www.getnexus.in/.
Try searching for a tool you’re evaluating this week — I’d love your honest feedback on search relevance, speed, or what you’d want to see next!
來源:DEV Community · dev.to