Embeddings and semantic search
Turning text into numbers that capture meaning, so you can search by idea rather than exact words.
What is it?
An embedding is a list of numbers that represents the meaning of a piece of text, an image or other data. Texts with similar meanings get similar numbers, even if they use different words: "How do I reset my password?" ends up close to "I forgot my login".
Semantic search uses embeddings to find items by meaning. You turn your documents into embeddings once and store them, often in a vector database. When someone searches, their query is turned into an embedding too, and the closest matches are returned. This is a core building block of RAG, recommendations and duplicate detection.
Which models use it?
- OpenAI, Google and Cohere offer embedding models through their APIs.
- Open-source embedding models are available on Hugging Face.
- Vector databases such as Pinecone, Weaviate, Qdrant and the pgvector extension for PostgreSQL store embeddings.
- Search and recommendation features in many apps use embeddings behind the scenes.
How to use it
- Pick an embedding model that supports your languages and content type.
- Split long documents into passages before embedding them.
- Store each embedding with its original text and a reference to the source.
- Embed the user's query with the same model.
- Return the closest matches and, if useful, re-rank or filter them.
- Check search quality with real queries and adjust chunk size or model if needed.
Benefits
- Finds relevant results even when the words don't match.
- Works across phrasing and, with suitable models, across languages.
- Fast to search once the embeddings are stored.
- Useful for search, recommendations, clustering and finding duplicates.
Limits and risks
- Embeddings from different models are not compatible with each other.
- Exact terms such as product codes or names can be missed; keyword search may still be needed.
- Re-embedding everything is required when you change models.
- Embeddings of personal data are still personal data and need protection.
Prompt template
Copy it, replace the parts in [brackets] and paste it into any AI chat.
I want to build semantic search over [type of content, e.g. support articles] in [languages]. Explain the pipeline in plain steps: chunking, embedding, storing, querying and ranking. Suggest how big each chunk should be for my content, how to combine semantic and keyword search for names and codes, and how I can measure whether search results are good using [number] test queries.
More in Building with AI
- Using AI through APIsHow apps and scripts send requests to AI models in code instead of a chat window.
- AI agents and tool useAI systems that can take actions, such as searching, running code or using apps, to finish a task.
- MCP (Model Context Protocol) and connectorsAn open standard for connecting AI assistants to tools and data sources in a consistent way.
- RAG (retrieval-augmented generation)Letting an AI answer from your own documents by finding relevant passages first.
- Fine-tuning vs promptingWhen to customize a model with extra training, and when better prompts are enough.
- Evaluating AI outputPractical ways to check whether AI answers are accurate, useful and safe before you rely on them.