- Keyword search (TF-IDF) with Whoosh
- Semantic retrieval using SentenceTransformers + cosine similarity
SentenceTransformers will download model weights on first use. If you’re running this in a restricted environment, pre-download models or set
SENTENCE_TRANSFORMERS_HOME to a writable cache folder. For larger corpora consider batching embeddings to avoid memory spikes.1) Keyword search with Whoosh (TF-IDF)
Whoosh is a pure-Python search library that indexes documents and computes TF-IDF style relevance under the hood. The example below creates a temporary Whoosh index, adds our documents, and runs a simple keyword query. Whoosh matches query terms and ranks results by term frequency / inverse document frequency.- Whoosh returns documents where the query terms appear and ranks by TF-IDF-like importance.
- If a conceptually relevant document doesn’t contain the exact query terms (e.g., “Motor Diagnostics Checklist” for the query “engine troubleshooting”), Whoosh will not return it unless the text contains matching tokens.
This example stores the index in a temporary directory. In production or repeated runs, persist the index directory or rebuild as needed. Remember to clean up temporary files to avoid disk bloat (
shutil.rmtree(index_dir)).2) Semantic search with SentenceTransformers (cosine similarity)
Semantic search embeds documents and queries into a vector space and ranks by vector similarity (cosine). This approach captures conceptual relationships beyond exact token overlap. We use theparaphrase-MiniLM-L6-v2 model for compact, fast embeddings.
- The embedding model captures conceptual similarity, so queries like “engine troubleshooting” will surface “motor diagnostics” and “valve timing” even without exact word overlap.
- Semantic retrieval improves recall for related documents; TF-IDF provides more precision for literal matches.
3) Compare TF-IDF and Semantic rankings side-by-side
We can combine Whoosh (TF-IDF) hits and SentenceTransformers (cosine similarity) hits into a pandas DataFrame to compare ranks and scores. This lets you directly inspect differences in ordering and the presence/absence of documents in each result set.
Interpretation:
- Whoosh (TF-IDF) excels at precision for literal queries and is simple to run locally.
- Semantic search returns documents ranked by conceptual relevance and can surface related material that lacks exact tokens from the query.
- A hybrid approach often works best: use TF-IDF for exact matches and embeddings to expand recall, or rerank TF-IDF candidates with embeddings for a balance of speed and semantic quality.
- This lesson illustrated the differences between keyword (TF-IDF) search and semantic retrieval using a small corpus.
- You can run the provided notebook-style code to experiment with queries, model choice, and ranking strategies.
- Whoosh documentation: https://whoosh.readthedocs.io/
- SentenceTransformers: https://www.sbert.net/
- scikit-learn cosine similarity: https://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise.cosine_similarity.html
- pandas: https://pandas.pydata.org/