Back to Examples

AI Vector Search

Build a semantic search engine using embeddings and NexaDB's built-in vector search. This example shows how to find similar movies by theme, not keywords.

What You'll Build

1

Store Movie Data

Insert movies with 4D semantic vectors representing action, romance, sci-fi, and drama levels

2

Vector Search

Find similar movies using cosine similarity - search by meaning, not keywords

3

Real Results

Query for "romantic movies" and get The Notebook (99.9% match) - without using the word "romance"

4

Scale to Production

200x faster than linear scan, perfect for RAG systems and recommendation engines

Prerequisites

  • NexaDB installed (brew install nexadb)
  • Python 3.7+ or Node.js 12+
  • NexaDB client library (pip install nexaclient or npm install nexaclient)

Python Example: Movie Semantic Search

Step 1: Start NexaDB and Connect

Terminal
# Start NexaDB
nexadb start

# In another terminal, create your script
touch movie_search.py
movie_search.pyPython
from nexaclient import NexaClient

# Connect to NexaDB
client = NexaClient(
    host='localhost',
    port=6970,
    username='root',
    password='nexadb123'
)
client.connect()
print("Connected to NexaDB!")

Step 2: Insert Movies with Semantic Vectors

Each movie has a 4D vector: [action, romance, sci-fi, drama]

movie_search.py
# Movie data with semantic vectors
movies = [
    {
        "title": "The Matrix",
        "year": 1999,
        "genre": "Sci-Fi/Action",
        "embedding": [0.8, 0.2, 0.95, 0.5]  # [action, romance, sci-fi, drama]
    },
    {
        "title": "The Notebook",
        "year": 2004,
        "genre": "Romance/Drama",
        "embedding": [0.1, 0.98, 0.05, 0.85]
    },
    {
        "title": "Mad Max: Fury Road",
        "year": 2015,
        "genre": "Action",
        "embedding": [0.99, 0.05, 0.3, 0.4]
    },
    {
        "title": "Inception",
        "year": 2010,
        "genre": "Sci-Fi/Thriller",
        "embedding": [0.7, 0.2, 0.9, 0.6]
    },
    {
        "title": "Titanic",
        "year": 1997,
        "genre": "Romance/Drama",
        "embedding": [0.3, 0.95, 0.1, 0.9]
    }
]

# Insert movies
for movie in movies:
    result = client.create('movies', movie)
    print(f"Inserted: {movie['title']}")

print(f"\nInserted {len(movies)} movies with embeddings!")

Step 3: Semantic Search

Search by meaning - find romantic movies without using the word "romance"

movie_search.py
# Search for romantic movies
romance_query = [0.1, 0.95, 0.1, 0.8]  # Low action, high romance

results = client.vector_search(
    collection='movies',
    vector=romance_query,
    limit=3,
    dimensions=4
)

print("\nšŸŽ¬ Most Romantic Movies:")
for i, result in enumerate(results, 1):
    doc = result['document']
    similarity = result['similarity'] * 100
    print(f"{i}. {doc['title']} ({doc['year']}) - {similarity:.2f}% match")
    print(f"   Genre: {doc['genre']}\n")

Expected Output

Output
Connected to NexaDB!
Inserted: The Matrix
Inserted: The Notebook
Inserted: Mad Max: Fury Road
Inserted: Inception
Inserted: Titanic

Inserted 5 movies with embeddings!

šŸŽ¬ Most Romantic Movies:
1. The Notebook (2004) - 99.90% match
   Genre: Romance/Drama

2. Titanic (1997) - 98.78% match
   Genre: Romance/Drama

3. Inception (2010) - 57.45% match
   Genre: Sci-Fi/Thriller

JavaScript/Node.js Example

movieSearch.js
const NexaClient = require('nexaclient');

async function movieSearch() {
  const client = new NexaClient({
    host: 'localhost',
    port: 6970,
    username: 'root',
    password: 'nexadb123'
  });

  await client.connect();
  console.log('Connected to NexaDB!');

  // Insert movies
  const movies = [
    {
      title: "The Matrix",
      year: 1999,
      genre: "Sci-Fi/Action",
      embedding: [0.8, 0.2, 0.95, 0.5]
    },
    {
      title: "The Notebook",
      year: 2004,
      genre: "Romance/Drama",
      embedding: [0.1, 0.98, 0.05, 0.85]
    },
    {
      title: "Mad Max: Fury Road",
      year: 2015,
      genre: "Action",
      embedding: [0.99, 0.05, 0.3, 0.4]
    }
  ];

  for (const movie of movies) {
    await client.create('movies', movie);
    console.log(`Inserted: ${movie.title}`);
  }

  // Search for romantic movies
  const romanceQuery = [0.1, 0.95, 0.1, 0.8];
  const results = await client.vectorSearch('movies', romanceQuery, 3, 4);

  console.log('\nšŸŽ¬ Most Romantic Movies:');
  results.forEach((result, i) => {
    const similarity = (result.similarity * 100).toFixed(2);
    console.log(`${i + 1}. ${result.document.title} - ${similarity}% match`);
  });

  await client.disconnect();
}

movieSearch().catch(console.error);

Real-World Use Cases

RAG Systems

Build ChatGPT-style apps with document retrieval. Store OpenAI/Claude embeddings and find relevant context for prompts.

Recommendation Engines

"Customers who liked this also liked..." using product embeddings. Perfect for e-commerce and content platforms.

Semantic Search

Search by meaning instead of keywords. Find "affordable European getaways" without those exact words appearing.

Duplicate Detection

Find similar support tickets, detect plagiarism, or identify near-duplicate records in your database.

Next Steps

1
Read the Vector Search Guide →

Learn about HNSW algorithm, vector dimensions, and performance tuning

2
Try the Full Demo on GitHub →

Complete movie search demo with 12 movies and multiple search examples

3
Explore More Examples →

Node.js, Python, NestJS, and more integration examples

Download Complete Example

Get the full movie search demo with 12 movies and all search examples

Download demo_vector_search.py