Overview

4 Semantic Search from Scratch

This chapter moves from the idea of semantic search to a practical, from-scratch build of a hotel finder called Travelle. It contrasts semantic search with keyword search, showing how meaning-aware retrieval supports natural, free-form queries like “quiet hotels with ocean views and a spa” or “room with a view of the Eiffel Tower.” The narrative lays out the end-to-end pipeline—data preparation, embedding generation, similarity scoring, and vector indexing—while explaining why learning the internals (rather than relying on high-level frameworks) yields control, debuggability, and adaptability.

Practically, the system is assembled around hotel reviews scraped from multiple cities but scoped to Paris for the build. Each review is treated as a standalone document and converted to sentence embeddings with Sentence Transformers (using a popular model such as all-MiniLM-L6-v2, with optional GPU acceleration). The chapter surveys encoder choices (open vs. closed models), then covers measuring similarity via cosine or Euclidean distance and contrasts cross-encoders (higher fidelity, slower) with bi-encoders (fast retrieval via precomputed embeddings). It first implements a direct cosine search over embeddings, then introduces FAISS to index normalized vectors (e.g., with an exact flat inner-product index) for fast top-k retrieval, and frames vector databases and approximate methods (like IVF, PQ, HNSW) as the path to large-scale performance.

Bringing it all together, the chapter provides a concise recipe: load and filter the dataset, embed reviews, build a FAISS index, encode the user query, and retrieve/rank matching reviews. It demonstrates improving result quality by increasing k, aggregating multiple matched reviews per hotel, and computing an overall score to present more robust, evidence-backed recommendations (e.g., hotels near the Louvre with strong nearby dining). The case for FAISS centers on efficiency, memory use, and scalability as datasets grow, while the from-scratch approach builds intuition for tuning and future enhancements. The chapter closes by pointing toward advanced indexing strategies, vector database integration, and hybrid techniques that combine semantic and keyword filtering to elevate search relevance and speed.

The landing page of the actual website Travelle; currently it provides recommendations for hotels in Paris. You can visit this site at https://travelle.traversaal.ai
An Overview of the various steps involved in creating Semantic Search from scratch for Hotel Finder in Paris: Travelle
An Overview of the Cross-Encoder Model to measure the similarity between two sentences.
An overview of the Bi-Encoder Model to measure the similarity between two sentences.
Cosine Similarity Search provides us with a great set of results and hotels related to the query
Updated flow of Semantic Search with FAISS included in the architecture
Cosine Similarity Search using FAISS provides us with the same answer as generated using the cosine similarity with bi-encoders
Hotel Recommendations when we increase the number of reviews: I removed the related reviews because the output was crossing the threshold, but the code should be able to show them

Summary

  • The first step towards building Semantic Search is to identify the data needed to build the engine. For Travelle (hotel search engine for finding relevant hotels in Paris based on free form search), we began by exploring the hotel dataset, which provided us with a rich collection of hotel information and reviews, serving as the foundation for our search engine.
  • To capture the semantic meaning of the hotel reviews, we utilized state-of-the-art models like Sentence Transformers to convert the text into dense vector representations, also known as embeddings. Specifically, we employed the Nomic Embedding Model, which has shown impressive performance in capturing semantic similarity.
  • It's worth noting that there is a vast array of encoder models available for generating embeddings. The choice of the most suitable model depends on various factors, such as the specific use case, the size of the dataset, and whether an open-source or closed-source model is preferred. Careful consideration should be given to selecting the model that aligns best with the requirements of the search engine.
  • Distance metrics play a pivotal role in measuring the similarity between embeddings and the user's query. Two commonly used metrics are cosine similarity and Euclidean distance. These metrics allow us to quantify the proximity or similarity between the query and the hotel reviews in the vector space, enabling us to retrieve the most relevant results.
  • When it comes to semantic similarity, two popular architectures are the Bi-Encoder and Cross-Encoder models. The Bi-Encoder model offers advantages in terms of efficiency and computational resources, making it a preferred choice for many applications. It allows for the pre-computation and storage of embeddings, enabling fast retrieval during the search process.
  • To efficiently store and search through the embeddings at scale, FAISS (Facebook AI Similarity Search) is a powerful library designed for similarity search and clustering of dense vectors. FAISS provides a range of indexing techniques and algorithms that optimize the search process, ensuring fast and accurate retrieval of the most relevant hotel reviews based on the user's query.

FAQ

How does semantic search differ from keyword search?Keyword search matches exact words, so a query like “furry animal” only finds texts containing those words. Semantic search understands meaning and context, so it also returns texts about “dogs,” “cats,” or “rabbits,” even if “furry animal” isn’t explicitly mentioned.
What is Travelle and how is it different from typical hotel search engines?Travelle is a hotel search engine built from scratch that lets users write free‑form queries (e.g., “quiet hotels with ocean views and a spa for couples”). Instead of rigid filters, it retrieves semantically similar hotel reviews to surface hotels that best match the intent behind the query.
What dataset is used and how is it prepared?Hotel reviews and descriptions are sourced from TripAdvisor and loaded via the Hugging Face dataset “traversaal-ai-hackathon/hotel_datasets.” For this chapter, we filter to Paris: about 1,200 reviews across 30 hotels (~40 reviews per hotel). We focus on the review_text field (treating each review as a document) and keep other columns (like hotel_name, rating_value, etc.) for context.
Do I need frameworks like LangChain or LlamaIndex for this chapter?No. The chapter builds semantic search from first principles to deepen your understanding of each component (data, embeddings, similarity, indexing). Frameworks are helpful in production, but building from scratch improves customization, troubleshooting, and adaptability. Chunking and other framework-like strategies are covered later.
Which embedding model and library are used, and why?We use Sentence Transformers with the model “all-MiniLM-L6-v2.” Sentence Transformers produces high‑quality sentence-level embeddings that are easy to store, index, and compare. It’s fast and accurate for our use case. Alternatives include OpenAI’s text-embedding-3-small (paid) and open-source options like E5 (e.g., E5-Mistral-7B), ML-E5-large, BGE-M3, and Nomic-Embed.
How are embeddings generated and can I use a GPU?Load the SentenceTransformer model and call encode() on the list of review texts (and on each incoming query). If torch.cuda.is_available() is true, move the model to CUDA for faster encoding; otherwise it runs on CPU.
Which similarity metrics are used and what’s the difference?We use cosine similarity (angle between vectors) and discuss Euclidean (L2) distance (straight‑line distance). Cosine focuses on orientation, not magnitude; it’s common for text embeddings. In FAISS, we normalize vectors and use an inner‑product index (IndexFlatIP) to compute cosine similarity. Higher cosine scores mean higher similarity.
What’s the difference between bi-encoders and cross-encoders, and when should I use each?- Bi-encoders: Encode query and documents separately; document embeddings are precomputed. Very fast for retrieval and scalable; ideal for first‑pass search.
- Cross-encoders: Jointly encode query+document to score fine‑grained interactions; more accurate but slow. Best used to re-rank the top results from a bi‑encoder stage.
What is FAISS and why do we use it here?FAISS is a library for fast similarity search over dense vectors. It scales to millions/billions of embeddings and supports exact and approximate search. In this chapter we use an in‑memory flat index (IndexFlatIP + normalized vectors for cosine). Later, you can adopt advanced indexes (IVF, PQ, HNSW) or integrate FAISS behind a vector database for larger-scale systems.
How can we make results more robust than a simple top‑5 list?Increase k (e.g., to 25) when searching, group results by hotel, compute an overall score (e.g., mean similarity), and require at least two matching reviews per hotel. Sorting by this aggregated score yields more reliable, evidence‑backed recommendations. You can also summarize grouped reviews to reduce redundancy.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Build an Advanced RAG Application (From Scratch) ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Build an Advanced RAG Application (From Scratch) ebook for free