使用稀疏向量与稠密向量进行混合搜索
学习 Pinecone 如何将稠密语义向量与稀疏关键词向量结合,实现同时捕捉含义和精确术语的混合搜索。
使用稀疏向量与稠密向量进行混合搜索 是 CoddyKit 上的免费 Vector Databases: Pinecone, Weaviate & pgvector 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Vector Databases: Pinecone, Weaviate & pgvector 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Vector Databases: Pinecone, Weaviate & pgvector 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
The Limits of Dense-Only Search
Dense vectors capture meaning but can miss exact terms like product codes, names, or rare keywords. A user searching 'error E4012' may get semantically related but wrong results.
Hybrid search fixes this by adding keyword matching.
Dense vs Sparse Vectors
Two vector kinds:
- Dense — a few hundred floats encoding semantic meaning
- Sparse — mostly zeros, with weights only for present terms (like keyword scores)
Sparse vectors behave like classic keyword search.
What Sparse Vectors Look Like
A sparse vector is stored as indices and values for the non-zero terms.
sparse = {'indices': [10, 42, 77], 'values': [0.8, 0.5, 0.3]}
print('non-zero terms:', len(sparse['indices']))Why Hybrid Wins
Hybrid search combines the strengths:
- Dense handles synonyms and intent
- Sparse guarantees exact-term matches
- Together they boost recall and precision
Pinecone Hybrid Indexes
To use hybrid search in Pinecone, create a dotproduct index and upsert each record with both a dense values array and a sparse_values field. Queries supply both representations of the query.
Upserting a Hybrid Record
A record carries dense and sparse parts together.
record = {
'id': 'doc1',
'values': [0.1, 0.2, 0.3],
'sparse_values': {'indices': [5, 9], 'values': [0.7, 0.4]},
'metadata': {'title': 'Setup guide'}
}
print(record['id'], 'has', len(record['values']), 'dense dims')The Alpha Weighting
Hybrid queries use an alpha parameter to weight dense vs sparse. alpha=1 is pure dense, alpha=0 is pure sparse. Tune it for your data.
def weight(dense_vec, sparse_vals, alpha):
d = [v*alpha for v in dense_vec]
s = [v*(1-alpha) for v in sparse_vals]
return d, s
print(weight([1.0], [1.0], 0.7))Generating Sparse Vectors
Sparse vectors come from keyword models like BM25 or learned sparse encoders (e.g. SPLADE). They map terms to weighted indices that Pinecone can match against stored records.
Tuning Alpha
The right alpha depends on your queries:
- Keyword-heavy domains (codes, IDs) -> lower alpha
- Natural-language questions -> higher alpha
Test on real queries and measure both recall and precision.
When to Use Hybrid
Reach for hybrid when exact terms matter: legal, medical, technical docs, or catalogs with SKUs. For purely conversational content, dense alone may be enough and simpler.
Bringing It Together
Hybrid search in Pinecone = a dotproduct index, records with dense and sparse values, queries supplying both, and a tuned alpha. It captures meaning and exact terms in one ranked result set.
Quick Check
Test your understanding of hybrid search.
Recap
You learned that hybrid search combines dense semantic vectors with sparse keyword vectors so Pinecone captures both meaning and exact terms. Use a dotproduct index, upsert both representations, and tune the alpha weighting to your query mix.
常见问题解答
「使用稀疏向量与稠密向量进行混合搜索」课时是免费的吗?
是的 — 「使用稀疏向量与稠密向量进行混合搜索」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Vector Databases: Pinecone, Weaviate & pgvector 课程的其余内容,请升级到 CoddyKit PRO。 Vector Databases: Pinecone, Weaviate & pgvector 课程共包含 4 节课。
「使用稀疏向量与稠密向量进行混合搜索」这节课中我会学到什么?
学习 Pinecone 如何将稠密语义向量与稀疏关键词向量结合,实现同时捕捉含义和精确术语的混合搜索。 你通过在浏览器中直接运行的动手代码来练习 Vector Databases: Pinecone, Weaviate & pgvector,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Vector Databases: Pinecone, Weaviate & pgvector 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Vector Databases: Pinecone, Weaviate & pgvector 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「使用稀疏向量与稠密向量进行混合搜索」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Vector Databases: Pinecone, Weaviate & pgvector 课中编写并运行代码吗?
能。每节 Vector Databases: Pinecone, Weaviate & pgvector 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。