Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(search): keep vector hits when keyword retrieval fails (#4459)

LuckVd97d7f0ad7b2b
diff --git a/internal/application/service/knowledgebase_search_fanout.go b/internal/application/service/knowledgebase_search_fanout.go
index 72936f225..db7dc910e 100644
--- a/internal/application/service/knowledgebase_search_fanout.go
+++ b/internal/application/service/knowledgebase_search_fanout.go
@@ -29,16 +29,13 @@ const (
// collected results span >1 engine type, the normalizer rescales vector
// scores into [0, 1] before fusion.
//
-// Failure policy: all-or-nothing for store-level failures. The first group
-// error fails the whole search and cancels siblings via errgroup, matching the
-// existing single-store behavior — chat/agent callers already treat "search
-// failed" as abort. A retrieve call that returns results *and* an error is a
-// different case: CompositeRetrieveEngine only does that when individual result
-// sets carry a partial failure (RetrieveResult.Error), i.e. some collections
-// answered and some did not. Those results are kept and the failure is logged
-// at WARN by retainPartialResults; failing the whole search would throw away
-// evidence that was retrieved successfully. A future PR can extract a
-// MergePolicy interface (open/closed) to push partial results further up.
+// Failure policy: a store group that fails without results fails the whole
+// search and cancels siblings via errgroup. A retrieve call may return results
+// alongside an error when one retriever leg or collection fails but another
+// succeeds. Those results are kept and the failure is logged at WARN by
+// retainPartialResults; failing the whole search would throw away evidence that
+// was retrieved successfully. A future PR can extract a MergePolicy interface
+// (open/closed) to push partial results further up.
//
// Fast path: len(groups) == 1 → returns the single engine's Retrieve
// directly with zero fan-out overhead. This is the dominant case today
@@ -160,15 +157,13 @@ func retrieveGroup(ctx context.Context, g *storeGroup) ([]*types.RetrieveResult,
// retainPartialResults keeps results that arrive together with an error.
//
-// CompositeRetrieveEngine returns a non-nil error alongside results only when
-// individual result sets carry a partial failure (RetrieveResult.Error): some
-// collections of the store answered, others did not. Dropping the results would
-// turn a degraded store into a total search failure and lose the evidence that
-// was retrieved; returning them without a trace would let callers treat an
-// incomplete answer as complete. So the failure is logged at WARN — this is the
-// closest consumer that still knows the store group — and the results are
-// returned with a nil error, leaving the all-or-nothing policy for store-level
-// failures untouched (those come back with no results, so err stays fatal).
+// CompositeRetrieveEngine returns a non-nil error alongside results when a
+// retriever leg or collection fails but other retrievals succeed. Dropping the
+// results would turn a degraded store into a total search failure and lose the
+// evidence that was retrieved; returning them without a trace would let callers
+// treat an incomplete answer as complete. So the failure is logged at WARN —
+// this is the closest consumer that still knows the store group — and the
+// results are returned with a nil error. Failures with no results remain fatal.
func retainPartialResults(
ctx context.Context, g *storeGroup, res []*types.RetrieveResult, err error,
) ([]*types.RetrieveResult, error) {
diff --git a/internal/application/service/retriever/composite.go b/internal/application/service/retriever/composite.go
index db1c7b47f..08420aa28 100644
--- a/internal/application/service/retriever/composite.go
+++ b/internal/application/service/retriever/composite.go
@@ -31,12 +31,12 @@ type CompositeRetrieveEngine struct {
// Retrieve performs retrieval operations by delegating to the appropriate engine
// based on the retriever type specified in the parameters.
//
-// Error contract: a store-level failure is returned with no results, matching
-// the long-standing all-or-nothing policy. A result set that reports a partial
-// failure in RetrieveResult.Error is different — the hits it did return are
-// kept and the failure is returned *alongside* them, so callers can log the
-// gap without losing the evidence. Callers must therefore treat a non-nil
-// error with non-nil results as "incomplete", not as "no results".
+// Error contract: a failure in one retriever leg does not discard results
+// collected from other successful legs. Any such error is returned alongside
+// the available results (or nil results when every leg fails), so callers can
+// degrade gracefully without treating an incomplete answer as complete. A
+// result set that reports a partial failure in RetrieveResult.Error follows
+// the same contract: its returned hits are kept alongside the failure.
func (c *CompositeRetrieveEngine) Retrieve(ctx context.Context,
retrieveParams []types.RetrieveParams,
) ([]*types.RetrieveResult, error) {
@@ -76,8 +76,8 @@ func (c *CompositeRetrieveEngine) Retrieve(ctx context.Context,
},
)
if err != nil {
- // Engine-level failure: drop the results collected so far, unchanged.
- return nil, err
+ // Keep results from successful retriever legs alongside the failure.
+ return results, err
}
if len(partialErrs) > 0 {
return results, fmt.Errorf("retrieve returned partial results: %w", errors.Join(partialErrs...))
@@ -181,7 +181,7 @@ func concurrentRetrieve(
// Check for errors
for err := range errCh {
if err != nil {
- return nil, err
+ return results, err
}
}
diff --git a/internal/application/service/retriever/composite_partial_test.go b/internal/application/service/retriever/composite_partial_test.go
index f76cd30ac..91ba34515 100644
--- a/internal/application/service/retriever/composite_partial_test.go
+++ b/internal/application/service/retriever/composite_partial_test.go
@@ -100,17 +100,15 @@ func TestCompositeRetrieveStaysSilentWhenResultSetsAreComplete(t *testing.T) {
require.NoError(t, results[0].Error)
}
-// An engine-level error is not a partial result: it still drops everything
-// collected so far, so the existing all-or-nothing policy is unchanged.
-func TestCompositeRetrieveDropsResultsOnEngineLevelError(t *testing.T) {
- cause := errors.New("vector store unavailable")
+func TestCompositeRetrieveKeepsVectorResultsWhenKeywordSearchFails(t *testing.T) {
+ cause := errors.New("postgres BM25 index is inconsistent")
engine := &cannedEngine{
- engineType: types.QdrantRetrieverEngineType,
+ engineType: types.PostgresRetrieverEngineType,
support: []types.RetrieverType{types.VectorRetrieverType, types.KeywordsRetrieverType},
results: map[types.RetrieverType][]*types.RetrieveResult{
types.VectorRetrieverType: {{
Results: []*types.IndexWithScore{{ChunkID: "vector-1", Score: 0.9}},
- RetrieverEngineType: types.QdrantRetrieverEngineType,
+ RetrieverEngineType: types.PostgresRetrieverEngineType,
RetrieverType: types.VectorRetrieverType,
}},
},
@@ -123,7 +121,23 @@ func TestCompositeRetrieveDropsResultsOnEngineLevelError(t *testing.T) {
})
require.ErrorIs(t, err, cause)
- require.Nil(t, results, "an engine-level failure still drops all results")
+ require.Len(t, results, 1, "a failed BM25 leg must not discard successful vector results")
+ require.Len(t, results[0].Results, 1)
+ require.Equal(t, "vector-1", results[0].Results[0].ChunkID)
+}
+
+func TestCompositeRetrieveReturnsErrorWhenNoLegSucceeds(t *testing.T) {
+ cause := errors.New("vector store unavailable")
+ engine := &cannedEngine{
+ engineType: types.QdrantRetrieverEngineType,
+ support: []types.RetrieverType{types.KeywordsRetrieverType},
+ errs: map[types.RetrieverType]error{types.KeywordsRetrieverType: cause},
+ }
+
+ results, err := compositeWith(engine).Retrieve(context.Background(), keywordsParams("q"))
+
+ require.ErrorIs(t, err, cause)
+ require.Nil(t, results, "when no retriever leg succeeds there are no fallback results")
}
// The canned engine must stay usable wherever the registry hands out an