Building a Simple In-Memory Search
What you'll learn: How to create a basic semantic search system using NumPy arrays to store embeddings and find the most similar items to a query.
The Core Idea
You've learned how to compute embeddings and measure similarity between them. Now it's time to put those pieces together into an actual search system. An in-memory search means storing all your embeddings in RAM (using NumPy arrays) so you can quickly compare a query against your entire collection and return the most relevant results.
Think of it like having a library where every book has a unique "fingerprint" (embedding). When someone describes what they want, you create a fingerprint of their description and compare it against all the book fingerprints to find the best matches.
Building Blocks
Your simple search system needs three things:
- Storage: A NumPy array holding all your document embeddings (one per row)
- Query processing: Convert the user's search query into an embedding
- Ranking: Compute similarity scores between the query embedding and all stored embeddings, then sort by highest similarity
The beauty of NumPy is that you can compute similarity against thousands of embeddings in milliseconds using vectorized operations. Instead of looping through embeddings one-by-one, NumPy processes them all at once.
The Search Flow
When a user searches for "machine learning tutorials," you:
- Get the query embedding
- Compute cosine similarity against your entire array of stored embeddings
- Sort results by similarity score (highest first)
- Return the top N most similar documents
Key Takeaway: A basic in-memory semantic search combines embedding storage (NumPy arrays), similarity computation (cosine or dot product), and ranking (sorting by score) to find relevant results without complex infrastructure.