Keyword-aware semantic search combines keyword matching and semantic understanding in a single DashVector query. By storing both sparse vectors (for term frequency) and dense vectors (for semantic meaning) in one collection, you get stronger recall without running two separate search systems.
Key concepts
Sparse vectors represent keywords and term frequency. They contain mostly zero values with a few non-zero values, one for each keyword in the document. In DashVector, the sparse vector {1:0.4, 10000:0.6, 222222:0.8} has three keywords (elements 1, 10000, and 222222), where the values are their weights.
Dense vectors represent the semantic meaning of content. An embedding model converts text into a fixed-length array of floating-point numbers, capturing conceptual relationships that keyword matching misses.
| Sparse vectors | Dense vectors | |
|---|---|---|
| Represents | Keywords and term frequency | Semantic meaning |
| Structure | Mostly zeros, few non-zero values | Mostly non-zero values |
| Built with | Inverted indexes, TF-IDF, BM25 | Embedding models |
| Strength | Exact term matching | Conceptual similarity |
| Example gap | Cannot match "raining heavily" to "rain cats and dogs" | Cannot match "rice irrigating" to "irrigating rice" if untrained |
Keyword-aware semantic search closes both gaps at once. The final score for each result is the dot product of the query vectors and the document vectors — dense and sparse scores are summed together.
When to use keyword-aware semantic search
Use keyword-aware semantic search when:
Queries mix natural language with specific terms (product names, error codes, IDs)
Users search for idioms, synonyms, or paraphrased concepts
Your corpus requires both precise keyword recall and contextual understanding
Prerequisites
Before you begin, ensure that you have:
A DashVector cluster (Create a cluster)
An API key (Manage API keys)
The latest DashVector SDK installed (Install DashVector SDK)
Run keyword-aware semantic search
The following steps create a collection, insert a document with both vector types, and run a hybrid query.
Only collections that use metric='dotproduct' support sparse vectors.
Step 1: Create a collection
import dashvector
# Replace YOUR_API_KEY with your API key (see Manage API keys).
# Replace YOUR_CLUSTER_ENDPOINT with your cluster endpoint (see the cluster details page).
client = dashvector.Client(
api_key='YOUR_API_KEY',
endpoint='YOUR_CLUSTER_ENDPOINT'
)
# dimension=4 is used here for simplicity.
# Set this to match your actual embedding model's output dimension.
ret = client.create('hybrid_collection', dimension=4, metric='dotproduct')
collection = client.get('hybrid_collection')
assert collectionStep 2: Insert a document with a sparse vector
from dashvector import Doc
collection.insert(Doc(
id='A',
vector=[0.1, 0.2, 0.3, 0.4],
sparse_vector={1: 0.3, 10: 0.4, 100: 0.3}
))The sparse_vector field maps keyword IDs to their weights. Use a sparse encoder like DashText to generate these from raw text — see Sparse vector encoder.
Step 3: Query with both vector types
Pass both a vector and a sparse_vector in the same query. DashVector scores each result by summing the dot products from both vector types.
docs = collection.query(
vector=[0.1, 0.1, 0.1, 0.1],
sparse_vector={1: 0.3, 20: 0.7}
)Results are ranked by the combined dot product score — higher is more relevant.
Sparse vector encoder
DashText is the recommended sparse vector encoder for DashVector. It converts raw text into the sparse_vector format shown above. See DashText quick start for setup instructions.
What's next
DashText quick start — generate sparse vectors from text
Create a cluster — set up your DashVector cluster
Manage API keys — manage authentication