[FEATURE] ReRankingContentAggregator: add minResults to guarantee a minimum number of results when minScore filters out everything

Author: riferreiCreated Sep 26, 2026Updated Sep 26, 2026
Labelsenhancement

Is your feature request related to a problem? Please describe.

tl;dr: when no candidate clears minScore, ReRankingContentAggregator returns nothing, even when the correct content was retrieved. Rerankers can score correct content very low, so the model ends up answering without context.

Example: a question that contradicts a stored fact

Setup (LangChain4j 1.20.1):

  • Pipeline: CompressingQueryTransformer → LanguageModelQueryRouter → EmbeddingStoreContentRetriever → ReRankingContentAggregator
  • Scoring model: Cohere rerank-multilingual-v3.0
  • Stored memories: "Ricardo's favorite color is black" and "User's favorite color is black."

The user asks whether blue is their favorite color. Both memories are retrieved, and both are exactly what the model needs to answer "No, it's black". But Cohere scores them very low:

Query Score (memory 1) Score (memory 2)
whether blue is my favorite color 0.0079 0.00002
Does Ricardo have a stored preference indicating that blue is his favorite color? (compressed query) 0.1061 0.0001

With minScore(0.8), both are dropped and the assistant says it has no memory of the user's favorite color.

Why lowering the threshold doesn't help

The threshold also does useful work. With the same memory set:

Query: which PR is a priority right now Score
The PR adding support for JSON is the priority this week 0.6883
User's favorite color is black. 0.0009
User has a dentist appointment this week 0.00009

Here minScore correctly keeps only the PR memory. The problem:

  • Correct-but-contradicting content scores around 0.007.
  • Irrelevant content scores around 0.0009.
  • That gap is small and shifts with query wording, so no single threshold separates them reliably.

Describe the solution you'd like

Add an optional minResults to the builder. It defaults to 0, so existing behavior doesn't change.

java
ReRankingContentAggregator.builder()
        .scoringModel(scoringModel)
        .minScore(0.5)
        .minResults(1)
        .build();

Behavior after reranking:

  1. Sort candidates by score, highest first.
  2. Keep every candidate with score >= minScore.
  3. If fewer than minResults passed, fill up with the highest-ranked remaining candidates.
  4. Apply maxResults as today.

In other words: filter strictly when something is clearly relevant; otherwise still give the model the best candidate.

How it fixes the examples above

With minScore(0.5) and minResults(1):

Favorite color (whether blue is my favorite color)

Memory Score Result
User's favorite color is black. 0.0079 ✅ kept (fill-up, step 3)
Ricardo's favorite color is black 0.00002 ❌ dropped
  • Nothing clears 0.5, so step 2 keeps zero items.
  • 0 < minResults, so step 3 adds the top-ranked memory.
  • The model receives "User's favorite color is black." and answers "No, your favorite color is black."
  • Today: the list is empty and the model says it has no memory of the user's favorite color.

PR priority (which PR is a priority right now)

Memory Score Result
The PR adding support for JSON is the priority this week 0.6883 ✅ kept (passes minScore, step 2)
User's favorite color is black. 0.0009 ❌ dropped
User has a dentist appointment this week 0.00009 ❌ dropped
  • The PR memory clears 0.5, so minResults is already satisfied and step 3 does nothing.
  • The irrelevant memories stay out of the prompt, exactly as with today's minScore.

Filtering stays strict when a memory clearly matches, and the correct memory still gets through when a contradicting question scores it low.

Notes:

  • Validation: minResults >= 0 and minResults <= maxResults.
  • The change lives in toReRankedContents, so both aggregate and aggregateAsync get it.
  • RERANKED_SCORE metadata is still attached, so downstream code can tell that a kept item scored low.

Describe alternatives you've considered

  • Remove minScore: irrelevant content reaches the prompt.
  • Lower minScore: no value reliably separates contradicting from irrelevant content (see above).
  • Custom ContentAggregator: what I run today. It works, but it duplicates fusion, reranking and metadata handling, and it loses querySelector and async support.
  • Subclass and override reRankAndFilter: the filtering logic is in private methods, so the override has to redo scoring.
  • Wrap the aggregator (minScore(null), then post-filter on RERANKED_SCORE): works, but it's not obvious and moves filtering away from the class that owns minScore.
  • Change the default to always return one result: silently breaks users who rely on an empty result to make the model say "I don't know".

Additional context