HybridSearcher

org.llm4s.vectorstore.HybridSearcher
See theHybridSearcher companion object
final class HybridSearcher

Hybrid searcher combining vector similarity and keyword matching.

Provides unified search over both vector embeddings (semantic similarity) and keyword indexes (BM25 term matching). Results are fused using configurable strategies like RRF or weighted scoring.

Usage:

for {
 vectorStore <- VectorStoreFactory.inMemory()
 keywordIndex <- KeywordIndex.inMemory()
 searcher = HybridSearcher(vectorStore, keywordIndex)
 // Add documents to both stores
 _ <- vectorStore.upsert(VectorRecord("doc-1", embedding, Some("content")))
 _ <- keywordIndex.index(KeywordDocument("doc-1", "content"))
 // Search with hybrid fusion
 results <- searcher.search(queryEmbedding, "search terms", topK = 10)
} yield results

Attributes

Companion
object
Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

def close(): Unit

Close both the vector store and keyword index.

Close both the vector store and keyword index.

Attributes

def search(queryEmbedding: Array[Float], queryText: String, topK: Int, strategy: FusionStrategy, filter: Option[MetadataFilter]): Result[Seq[HybridSearchResult]]

Perform hybrid search combining vector and keyword results.

Perform hybrid search combining vector and keyword results.

Value parameters

filter

Optional metadata filter applied to both searches

queryEmbedding

Query embedding for vector search

queryText

Query text for keyword search

strategy

Fusion strategy (default: RRF)

topK

Maximum results to return

Attributes

Returns

Fused search results ranked by combined score

def searchKeywordOnly(queryText: String, topK: Int, filter: Option[MetadataFilter]): Result[Seq[HybridSearchResult]]

Search with keyword matching only.

Search with keyword matching only.

Attributes

def searchVectorOnly(queryEmbedding: Array[Float], topK: Int, filter: Option[MetadataFilter]): Result[Seq[HybridSearchResult]]

Search with vector similarity only.

Search with vector similarity only.

Attributes

def searchWithReranking(queryEmbedding: Array[Float], queryText: String, topK: Int, rerankTopK: Int, strategy: FusionStrategy, filter: Option[MetadataFilter], reranker: Option[Reranker]): Result[Seq[HybridSearchResult]]

Perform hybrid search with optional cross-encoder reranking.

Perform hybrid search with optional cross-encoder reranking.

This method first performs hybrid search to get candidate documents, then optionally applies a reranker for improved precision. Reranking uses cross-encoder models that see both query and document together.

Value parameters

filter

Optional metadata filter

queryEmbedding

Query embedding for vector search

queryText

Query text for keyword search and reranking

rerankTopK

Number of candidates to retrieve for reranking (default: 50)

reranker

Optional reranker (None = skip reranking)

strategy

Fusion strategy (default: RRF)

topK

Maximum final results to return

Attributes

Returns

Reranked search results

Example
val reranker = RerankerFactory.cohere(apiKey)
val results = searcher.searchWithReranking(
 embedding, "search query",
 topK = 5,
 rerankTopK = 30,
 reranker = Some(reranker)
)