Semantic search

Semantic search is search that matches the meaning of a query rather than its exact words, usually by turning text into embeddings and returning the stored items whose embeddings are closest.

Keyword search finds documents that contain the words you typed. It misses documents that say the same thing differently, so a search for cheap flights may skip a page about low-cost airfares. Semantic search tries to match meaning instead. An embedding model turns each document, or each chunk of one, into a list of numbers called a vector, placed so that texts with similar meanings sit close together. The query is turned into a vector the same way, and the system returns the nearest documents, often measured by cosine similarity.

A key step came in 2019 with Sentence-BERT, by Nils Reimers and Iryna Gurevych, published at EMNLP. Using the original BERT model to find the most similar pair among 10,000 sentences required feeding every pair through the network, about 65 hours of computation. Sentence-BERT produced a vector for each sentence once, so the same search took about five seconds while keeping BERT's accuracy. That made meaning-based search practical over large collections.

Search engines moved the same way. In October 2019 Google said its BERT models would help it understand about one in ten English searches in the United States. Its example was the query 2019 brazil traveler to usa need a visa, where earlier systems ignored the word to and returned results about Americans travelling to Brazil.

Today semantic search is the retrieval step in most retrieval-augmented generation systems. Vectors are kept in a vector database, and many systems combine semantic search with keyword search, then pass the shortlist to a reranker that puts the best matches first before the model writes an answer.

In practice, similar is not the same as correct. Embeddings can miss exact matches that keyword search handles easily, such as product codes, names and figures, and results depend heavily on the embedding model and on how documents are split up. Calling a search semantic describes how it matches text, not whether it understands it.

Common questions

What is the difference between semantic search and keyword search?

Keyword search matches the exact words in a query, so it can miss documents that use different words for the same idea. Semantic search compares meanings using embeddings, so it can find paraphrases and related wording. Keyword search is still better for exact terms such as part numbers, which is why many systems use both.

How does semantic search work?

An embedding model converts every document or passage into a vector, and those vectors are stored. When a query comes in, it is converted the same way, and the system finds the stored vectors closest to it. The closest items are returned as results, sometimes after a reranking step.

Do you need a vector database for semantic search?

For small collections, vectors can be compared directly in memory. For large collections, a vector database or search index with approximate nearest neighbour search makes lookups fast enough to use.

Is semantic search the same as RAG?

No. Semantic search finds relevant documents. Retrieval-augmented generation uses search, often semantic search, to fetch documents and then has a language model write an answer based on them. Semantic search is one component of most RAG systems.

Finding more

Search for "semantic search embeddings", "dense retrieval" or "keyword vs semantic search" to see how the approach works and where it falls short. Research papers on arXiv, documentation for open-source libraries such as Sentence Transformers and official posts from search engine companies are the most reliable sources. Be careful with marketing that uses semantic to mean the system understands the user, since it describes a matching method.

Working out what this means for your business?

Aileron Shift builds the systems behind this site: AI newsrooms that research, write and publish on their own, marketing automation and WordPress hosting for companies that need to move faster than their competitors.