Implementing Semantic Search with Vector database
Last Updated :
24 Jun, 2025
Semantic Search is an advanced search technique that enhances traditional search techniques by focusing on understanding the meaning or intent behind a query, rather than relying solely on exact keyword matches. This approach enables the system to interpret the context, relationships and underlying semantics of the words in both the query and the documents, resulting in more relevant and precise search outcomes that align with the user's true intent.
Key Concepts
Vector embeddings are dense representations of text in a high-dimensional space. Each word or document is transformed into a vector, where similar meanings are close to each other in the vector space. By using vector databases and embeddings we can capture the relationships between words and concepts.
How Vector Embeddings Work:
- Word Embeddings: Words are represented as vectors in high-dimensional space, where semantically similar words, like "dog" and "puppy" are close together.
- Sentence/Document Embeddings: Whole sentences or documents are also represented as vectors, capturing the overall meaning. Models like BERT, Word2Vec and Sentence-BERT generate these embeddings by mapping text into vectors while preserving contextual relationships.
- Dimensionality: Embeddings are high-dimensional with each dimension encoding abstract features of the text. More dimensions allow for richer information.
- Cosine Similarity: To measure similarity between vectors, cosine similarity is used to calculate the cosine of the angle between them where smaller angles indicate higher similarity.
Vector Database: A vector database is designed to store and query vector embeddings. Unlike traditional relational databases which are optimized for structured data, vector databases are optimized for similarity searches. These databases provide efficient methods to store and retrieve vectors by their similarity making them ideal for semantic search applications.
How Vector Databases Work:
- Storage: Vector databases store large volumes of embeddings, representing text (words, sentences, documents) as coordinates in high-dimensional space.
- Indexing: Specialized indexing techniques enable fast retrieval of similar vectors by organizing them for efficient similarity searches.
- Similarity Search: Queries are transformed into vector embeddings and the database retrieves the most similar vectors using distance metrics like cosine or Euclidean distance.
- Approximate Nearest Neighbor (ANN): Approximate Nearest Neighbor algorithms like HNSW or LSH optimize searches by providing fast, approximate results, balancing speed and accuracy.
Semantic Search using vector DatabasesSteps to Implement Semantic Search with a Vector Database
Lets understand the implementation of semantic search with vector database with following steps:
Step 1: Import Libraries
sentence_transformers
: For generating sentence embeddings (vector representations).faiss
: For performing similarity search on vector embeddings we will use faiss.NumPy
: NumPy is used for handling numerical arrays and converting embeddings.
Python
from sentence_transformers import SentenceTransformer
import faiss
import numpy as np
Step 2: Generate Vector Embeddings
- Load a pre-trained model
all-MiniLM-L6-v2
. - Convert the list of documents into vector embeddings using the model.
Python
model = SentenceTransformer('all-MiniLM-L6-v2')
documents = [
"The quick brown fox jumps over the lazy dog",
"A journey of a thousand miles begins with a single step",
"To be or not to be, that is the question",
"A ride full of surprises and joy starts tomorrow"
]
embeddings = model.encode(documents)
Step 3: Create the FAISS Index
- Convert embeddings to
float32
format for FAISS compatibility. - Create a FAISS index with the L2 distance metric and add the embeddings to the index.
Python
embeddings_np = np.array(embeddings).astype('float32')
index = faiss.IndexFlatL2(embeddings_np.shape[1])
index.add(embeddings_np)
Step 4: Querying the Database
- Generate an embedding for the query.
- Perform a similarity search in the FAISS index, retrieve the most similar document and display the result.
D
: A NumPy array containing the distances between the query's embedding and the document embeddings in the FAISS index. Smaller distances indicate more similarity.I
: A NumPy array containing the indices of the most similar documents from the documents
list, based on the similarity scores.k=1
: This means that the code will return the single most similar document to the query i.e only the top 1 result based on similarity.
Python
query = "What is the first step of a long journey?"
query_embedding = model.encode([query])
D, I = index.search(np.array(query_embedding).astype('float32'), k=1)
print(f"Most similar document: {documents[I[0][0]]}")
Output:
Querying the DatabaseStep 5: Improve Search Results
- Fine-tune the sentence transformer model for your specific domain or dataset.
- Use hierarchical indexing or more complex indexing structures available in FAISS or other vector databases.
- Experiment with different distance metrics like cosine similarity, which often works better in high-dimensional spaces.
Use Cases of Semantic Search
- E-commerce: Enhance product search by interpreting user intent even when different terms or phrases are used to describe the same product.
- Document Retrieval: Retrieve the most relevant documents or articles based on their semantic meaning not just exact keyword matches.
- Customer Support: Improve search results for FAQs and support tickets by understanding the context of user queries, leading to more accurate responses.
- Healthcare: Streamline the retrieval of medical research, patient records or clinical notes by understanding the relationships and context within the data.
Advantages of Using a Vector Database for Semantic Search:
- Contextual Relevance: Finds documents based on meaning not just keywords.
- Handling Synonyms: Can retrieve relevant documents even if different terms or synonyms are used in the query and documents.
- Scalability: Vector databases are designed to handle large datasets making them ideal for applications like e-commerce, document retrieval and knowledge graphs.
- Speed: With indexing and ANN vector databases provide fast, real-time similarity searches even for massive collections.
You can download source code from here.
Explore
Introduction to Machine Learning
Python for Machine Learning
Introduction to Statistics
Feature Engineering
Model Evaluation and Tuning
Data Science Practice