Vector Search
Build AI-powered semantic search with NexaDB's built-in vector capabilities.
Perfect for AI Applications
Use NexaDB for semantic search, RAG systems, recommendation engines, and any application that works with embeddings from OpenAI, Cohere, or other models.
Storing Vectors
// Store document with embedding vector
const doc = await db.create('documents', {
title: 'Introduction to AI',
content: 'Artificial intelligence is...',
embedding: [0.123, -0.456, 0.789, ...], // 1536-dim vector
metadata: {
author: 'John Doe',
date: '2024-01-15'
}
});Similarity Search
// Find similar documents
const queryEmbedding = [0.111, -0.432, 0.765, ...];
const results = await db.vectorSearch('documents', {
vector: queryEmbedding,
limit: 10,
threshold: 0.8 // Minimum similarity score
});
results.forEach(doc => {
console.log(`${doc.title}: ${doc.similarity}`);
});Real-World Example: Movie Search
Let's see vector search in action with a movie recommendation system. Each movie has a 4D semantic vector representing themes: [action, romance, sci-fi, drama].
from nexadb_client import NexaClient
# Connect to NexaDB
client = NexaClient(
host='localhost',
port=6970,
username='root',
password='nexadb123'
)
client.connect()
# Insert movies with semantic vectors
movies = [
{
'title': 'The Dark Knight',
'year': 2008,
'genre': 'Action/Thriller',
'vector': [0.95, 0.1, 0.3, 0.7] # High action, low romance
},
{
'title': 'The Notebook',
'year': 2004,
'genre': 'Romance/Drama',
'vector': [0.1, 0.98, 0.05, 0.85] # Very high romance
},
{
'title': 'The Matrix',
'year': 1999,
'genre': 'Sci-Fi/Action',
'vector': [0.9, 0.15, 0.97, 0.5] # High action + sci-fi
},
# ... 7 more movies
]
client.batch_write('movies', movies)
# Search for action movies
action_results = client.vector_search(
collection='movies',
vector=[0.95, 0.05, 0.3, 0.4], # Action-heavy query
limit=3,
dimensions=4
)
# Results show semantic similarity!
# 1. Mad Max: Fury Road - 99.15% similar
# 2. The Dark Knight - 97.30% similar
# 3. The Matrix - 88.54% similar🎬 Action/Thriller
Query: [0.95, 0.05, 0.3, 0.4]
💕 Romance/Drama
Query: [0.1, 0.95, 0.1, 0.8]
🚀 Sci-Fi/Future
Query: [0.5, 0.2, 0.98, 0.5]
💡 Key Insight
Vector search finds semantically similar content, not just keyword matches. Movies with similar themes automatically cluster together based on their vector coordinates in semantic space.
# Clone the repo and run the demo
git clone https://github.com/krishcdbry/nexadb.git
cd nexadb
python3 demo_vector_search.py
# Output shows beautiful results with similarity scores!Complete RAG Example
import OpenAI from 'openai';
import NexaClient from 'nexaclient';
const openai = new OpenAI();
const db = new NexaClient({
host: 'localhost',
port: 6970,
username: 'root',
password: 'nexadb123'
});
await db.connect();
async function answerQuestion(question) {
// 1. Generate embedding for question
const embedding = await openai.embeddings.create({
model: 'text-embedding-3-small',
input: question
});
// 2. Find relevant documents
const context = await db.vectorSearch('knowledge', {
vector: embedding.data[0].embedding,
limit: 5
});
// 3. Generate answer with context
const response = await openai.chat.completions.create({
model: 'gpt-4',
messages: [
{ role: 'system', content: 'Answer using the context' },
{ role: 'user', content: question },
{ role: 'assistant', content: JSON.stringify(context) }
]
});
return response.choices[0].message.content;
}
const answer = await answerQuestion('What is NexaDB?');
console.log(answer);Use Cases
Semantic Search
Find documents by meaning, not just keywords. Perfect for documentation, knowledge bases, and content discovery.
RAG Systems
Build chatbots and AI assistants that answer questions using your private data with retrieval-augmented generation.
Recommendations
Power recommendation engines by finding similar products, content, or users based on embedding similarity.
Duplicate Detection
Find duplicate or similar content automatically by comparing document embeddings.