Mastering Search Types in LangChain with SearchType Enum
Posted: Feb 3, 2025.
The SearchType enum class in LangChain provides a way to specify different types of vector search algorithms when retrieving documents. This guide will explore how to use SearchType to optimize your document retrieval results.
What is SearchType?
SearchType is an enumeration class that defines the type of search to perform when retrieving documents from a vector store or retriever. It currently supports two search methods:
similarity
: Standard similarity search using vector embeddingsmmr
: Maximal Marginal Relevance (MMR) search which optimizes for both relevance and diversity in results
Reference
Method | Description |
---|---|
similarity | Performs a standard similarity search using vector embeddings to find the most relevant documents |
mmr | Uses Maximal Marginal Relevance algorithm to balance between relevance and diversity in search results |
How to use SearchType
Let's look at different ways to use SearchType in your LangChain applications.
Basic Similarity Search
The default search type is similarity search. Here's how to explicitly specify it:
Using MMR Search
MMR search helps reduce redundancy in search results by considering both relevance and diversity:
Using SearchType with Zep Retriever
The SearchType enum is particularly useful when working with specialized retrievers like ZepRetriever:
Combining with MultiVector Retrieval
SearchType is particularly powerful when used with MultiVectorRetriever for advanced retrieval scenarios:
When using SearchType with MMR, you can tune the trade-off between relevance and diversity by adjusting parameters like lambda_mult
- higher values favor relevance while lower values promote diversity in the results.
This configuration gives you fine-grained control over how documents are retrieved, allowing you to optimize for your specific use case whether you need the most relevant results or a more diverse set of documents.
An alternative to LangSmith
Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.
LangChain DocsJoin 10,000+ subscribers
Every 2 weeks, latest model releases and industry news.
An alternative to LangSmith
Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.