Similarity search retrieves passages whose meanings are close to a query, even when their words differ. In a retrieval-augmented generation (RAG) system, an embedding model converts document chunks and user questions into vectors. The system compares a question’s vector with stored document vectors and passes the closest matches to the language model as context. If the embedding model misses domain-specific meaning, a fluent answer can still be grounded in the wrong passage. section 4.0 section 1.4.1
A practical search pipeline has three parts:
- Embed document chunks and queries with an appropriate model. A medical-literature model is used for health-care policies because general-purpose embeddings may miss relevant terminology. section 4.5.4
- Store each chunk’s vector, unique ID, content, and policy metadata in a vector index. Set the index dimension to the embedding output size; a cosine metric compares vector direction. section 4.5.4 section 4.1
- Embed the incoming question, request the top matches, and optionally filter by fields such as department or active status. Supply the retrieved excerpts and their policy IDs when generating an answer. section 4.5.4
Finding the right policy
A user asks, “Is physical therapy covered?” The search should surface the applicable coverage passage rather than merely a passage containing similar words. Filtering for active Benefits policies can exclude irrelevant or outdated results. A separate query—“How do I get reimbursed for therapy?”—can be close in meaning to a mental-health claim question despite little word overlap. section 4.0 section 4.5.4 section 4.1
Nearest does not necessarily mean relevant or current. Check retrieval quality separately from the language model’s answer: an unsuitable embedding model can return unrelated passages, while missing or stale context can undermine grounding. Scale also matters; naive vector search becomes slow as collections grow, making the choice of vector-search infrastructure an operational concern. section 4.0 section 4.5 section 1.4.1
def search_policies(query, filters=None, top_k=3):
"""
Search policy documents with semantic search and optional filters
Args:
query: User question or query string
filters: Dictionary of metadata filters to apply
top_k: Number of results to return
Returns:
List of relevant policy chunks with metadata
"""
query_embedding = model.encode([query])[0].tolist() #1
filter_dict = None #2
if filters: #2
filter_dict = {} #2
for key, value in filters.items(): #2
filter_dict[key] = value #2
query_results = index.query( #3
vector=query_embedding, #3
filter=filter_dict, #3
top_k=top_k, #3
include_metadata=True #3
)
results = [] #4
for match in query_results['matches']: #4
results.append({ #4
"score": match['score'],
"content": match['metadata']['content'],
"section_title": match['metadata']['section_title'],
"policy_id": match['metadata']['policy_id'],
"department": match['metadata']['department'],
"effective_date": match['metadata']['effective_date']
})
return results
query = "What's covered for physical therapy after knee surgery?" #5
filters = {"status": "Active", "department": "Benefits"} #5
results = search_policies(query, filters) #5
for i, result in enumerate(results): #6
print(f"\nResult {i+1} (Score: {result['score']:.4f})")
print(f"Policy: {result['policy_id']} ({result['department']})")
print(f"Section: {result['section_title']}")
print(f"Content: {result['content'][:200]}...")
The Python search function shows the query path: encode a question, pass its vector and optional metadata filters to Pinecone, choose a top-k limit, and return matched chunks with metadata. section 4.5.4





















