What is Vector Search?
Vector search (also called semantic search or similarity search) is a technique that finds items based on their meaning rather than exact keyword matches. Instead of searching for "blue shoes," you can search for "casual footwear for summer" and get relevant results.
This is made possible by converting text, images, or other data into vectors (arrays of numbers) that capture semantic meaning.
How Vector Search Works
Step 1: Create Embeddings
First, you convert your data into vectors using an embedding model:
// Using OpenAI's embedding API
const response = await openai.embeddings.create({
model: "text-embedding-ada-002",
input: "Wireless noise-canceling headphones"
});
const vector = response.data[0].embedding;
// Returns: [0.0023, -0.0145, 0.0267, ...] (1536 dimensions)
Step 2: Store Vectors in a Database
Store these vectors alongside your data:
// Store in NexaDB
await client.create('products', {
name: 'Sony WH-1000XM5',
description: 'Premium wireless noise-canceling headphones',
price: 399,
vector: vector // The embedding from step 1
});
Step 3: Search by Similarity
When a user searches, convert their query to a vector and find similar items:
// Convert query to vector
const queryVector = await getEmbedding("best headphones for airplane travel");
// Find similar products
const results = await client.vectorSearch('products', queryVector, {
limit: 5,
minScore: 0.7
});
// Returns products semantically similar to the query
// Even if they don't contain the exact words
Why Vectors Capture Meaning
Embeddings place semantically similar items close together in vector space:
"king" - "man" + "woman" ≈ "queen"
"Paris" - "France" + "Italy" ≈ "Rome"
This mathematical property allows us to:
- Find similar documents without keyword matching
- Discover relationships between concepts
- Power recommendation systems
- Enable multilingual search
Vector Search Algorithms
Brute Force (Exact)
Compare query vector against every vector in the database.
- Pros: 100% accurate
- Cons: O(n) complexity, too slow for large datasets
- Use when: < 10,000 vectors
HNSW (Hierarchical Navigable Small World)
The most popular approximate algorithm. Creates a graph structure for fast navigation.
- Pros: O(log n) search, high accuracy (95-99%)
- Cons: More memory usage
- Use when: > 10,000 vectors, need fast search
NexaDB uses HNSW by default:
// NexaDB automatically uses HNSW for vector search
const results = await client.vectorSearch('products', queryVector, {
limit: 10,
algorithm: 'hnsw' // Default
});
Other Algorithms
- IVF (Inverted File Index): Clusters vectors, searches relevant clusters
- PQ (Product Quantization): Compresses vectors for memory efficiency
- LSH (Locality Sensitive Hashing): Hash-based approximate search
Similarity Metrics
Cosine Similarity (Most Common)
Measures the angle between two vectors. Values range from -1 to 1.
cosine_similarity = (A · B) / (||A|| × ||B||)
- 1.0: Identical direction (most similar)
- 0.0: Perpendicular (unrelated)
- -1.0: Opposite direction (opposite meaning)
Euclidean Distance
Measures straight-line distance between vectors.
euclidean_distance = √(Σ(ai - bi)²)
Dot Product
Simple multiplication of vector components. Faster but requires normalized vectors.
Building a RAG System with Vector Search
Retrieval-Augmented Generation (RAG) combines vector search with LLMs:
async function ragSearch(userQuery) {
// 1. Convert query to vector
const queryVector = await getEmbedding(userQuery);
// 2. Find relevant documents
const docs = await client.vectorSearch('knowledge_base', queryVector, {
limit: 5
});
// 3. Build context from retrieved documents
const context = docs.map(d => d.content).join('\n\n');
// 4. Ask LLM with context
const response = await openai.chat.completions.create({
model: 'gpt-4',
messages: [
{ role: 'system', content: `Use this context: ${context}` },
{ role: 'user', content: userQuery }
]
});
return response.choices[0].message.content;
}
Vector Search Use Cases
1. Semantic Document Search
Search documents by meaning, not just keywords:
// User searches: "how to handle angry customers"
// Finds: "De-escalation techniques for support teams"
// Even without matching keywords!
2. Recommendation Engines
Find similar items based on embeddings:
// User viewed: "Introduction to Machine Learning"
// Recommend: "Deep Learning Fundamentals", "Neural Networks 101"
3. Image Search
Convert images to vectors for visual similarity:
const imageVector = await clipModel.encode(uploadedImage);
const similar = await client.vectorSearch('product_images', imageVector);
4. Anomaly Detection
Find outliers that don't cluster with normal data:
const anomalies = await client.vectorSearch('transactions', normalVector, {
limit: 100,
maxScore: 0.3 // Low similarity = anomaly
});
Performance Optimization
1. Choose the Right Dimensions
- 384 dimensions: Fast, good for most use cases
- 768 dimensions: Better accuracy, moderate speed
- 1536 dimensions: Highest accuracy, slower
2. Use Approximate Search
For datasets > 10K vectors, always use HNSW:
// NexaDB: 200x faster with HNSW
const results = await client.vectorSearch('products', queryVector, {
limit: 10,
algorithm: 'hnsw',
efSearch: 100 // Higher = more accurate, slower
});
3. Pre-filter When Possible
Combine with traditional filters to reduce search space:
const results = await client.vectorSearch('products', queryVector, {
limit: 10,
filter: { category: 'electronics', price: { $lt: 500 } }
});
Getting Started with Vector Search in NexaDB
const { NexaClient } = require('nexaclient');
// Connect to NexaDB
const client = new NexaClient({
host: 'localhost',
port: 6970,
username: 'root',
password: 'nexadb123'
});
await client.connect();
// Index documents with vectors
await client.create('articles', {
title: 'Understanding Vector Databases',
content: 'Vector databases are specialized systems...',
vector: await getEmbedding('Understanding Vector Databases')
});
// Search semantically
const queryVector = await getEmbedding('how do similarity searches work');
const results = await client.vectorSearch('articles', queryVector, { limit: 5 });
console.log(results);
Conclusion
Vector search has transformed how we build search and recommendation systems. By capturing semantic meaning in numerical vectors, we can find relevant content even without exact keyword matches.
Key takeaways:
- Embeddings convert text/images to vectors
- HNSW provides fast approximate search
- Cosine similarity measures semantic closeness
- RAG systems combine vector search with LLMs
Ready to implement vector search? Try NexaDB with built-in HNSW vector search.
Next: Learn about TOON Format for reducing LLM costs by 40-50%.