Package com.tnsai.memory.advanced
Class HybridMemoryRetriever
java.lang.Object
com.tnsai.memory.advanced.HybridMemoryRetriever
Combines BM25 text search and vector semantic search via Reciprocal Rank Fusion.
For each query, both the BM25 index and the vector store are searched
independently. The two ranked result lists are then fused using RRF to
produce a single ranked list that balances lexical and semantic relevance.
Raw RRF scores are normalized against the number of non-empty result streams
so callers can apply a stable 0.0..1.0 relevance threshold.
-
Constructor Summary
ConstructorsConstructorDescriptionHybridMemoryRetriever(BM25Index bm25Index, VectorMemoryStore vectorStore) Creates a hybrid retriever with default RRF (k=60).HybridMemoryRetriever(BM25Index bm25Index, VectorMemoryStore vectorStore, ReciprocalRankFusion rrf) Creates a hybrid retriever with a custom RRF combiner. -
Method Summary
Modifier and TypeMethodDescriptionintReturns an upper bound on the number of distinct entries this retriever can return without overflowing its internal candidate multiplier.Searches using both BM25 and vector search, then fuses results via RRF.
-
Constructor Details
-
HybridMemoryRetriever
Creates a hybrid retriever with default RRF (k=60).- Parameters:
bm25Index- the BM25 text search indexvectorStore- the vector semantic search store
-
HybridMemoryRetriever
public HybridMemoryRetriever(BM25Index bm25Index, VectorMemoryStore vectorStore, ReciprocalRankFusion rrf) Creates a hybrid retriever with a custom RRF combiner.- Parameters:
bm25Index- the BM25 text search indexvectorStore- the vector semantic search storerrf- the RRF combiner to use
-
-
Method Details
-
search
Searches using both BM25 and vector search, then fuses results via RRF.Each search retrieves up to
candidateMultiplier * topKcandidates before fusion to ensure sufficient diversity.The returned score is normalized to
0.0..1.0. An entry ranked first in every non-empty stream scores1.0; empty streams are not counted in the normalization ceiling.- Parameters:
query- the search querytopK- maximum number of final results- Returns:
- fused results sorted by descending normalized RRF score
-
candidateCount
public int candidateCount()Returns an upper bound on the number of distinct entries this retriever can return without overflowing its internal candidate multiplier.Entries may exist in both backing stores, so the value can exceed the number of distinct entry IDs.
- Returns:
- a safe upper bound on available retrieval candidates
-