RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
fix(searchbot): honor the search app's saved retrieval config (#20944)
qinling0210fb8c6bfe61d6
diff --git a/internal/engine/infinity/chunk.go b/internal/engine/infinity/chunk.goindex 5b9e7663ae..a8b1978cbc 100644--- a/internal/engine/infinity/chunk.go+++ b/internal/engine/infinity/chunk.go@@ -3018,13 +3018,13 @@ func calculateScores(chunks []map[string]interface{}, scoreColumn, pagerankFieldfor i := range chunks {score := 0.0if scoreVal, ok := chunks[i][scoreColumn]; ok {- if f, ok := utility.ToFloat64(scoreVal); ok {+ if f, ok := common.ToFloat(scoreVal); ok {score += f}}if pagerankField != "" {if prVal, ok := chunks[i][pagerankField]; ok {- if f, ok := utility.ToFloat64(prVal); ok {+ if f, ok := common.ToFloat(prVal); ok {score += f}}diff --git a/internal/handler/chunk.go b/internal/handler/chunk.goindex e6ec5a2e36..546f464eb5 100644--- a/internal/handler/chunk.go+++ b/internal/handler/chunk.go@@ -34,7 +34,6 @@ import (// chunkService is the consumer-side interface for ChunkHandler's service dependency.type chunkService interface {- RetrievalTest(ctx context.Context, req *service.RetrievalTestRequest, userID string) (*service.RetrievalTestResponse, error)Get(ctx context.Context, req *service.GetChunkRequest, userID string) (*service.GetChunkResponse, error)List(ctx context.Context, req *service.ListChunksRequest, userID string) (*service.ListChunksResponse, error)SwitchChunks(ctx context.Context, userID string, datasetID, documentID string, availableInt int, chunkIDs []string) error@@ -59,83 +58,6 @@ func NewChunkHandler(chunkService chunkService, userService *service.UserService}}-// RetrievalTest performs retrieval test for chunks-// @Summary Retrieval Test-// @Description Test retrieval of chunks based on question and knowledge base-// @Tags chunks-// @Param request body service.RetrievalTestRequest true "retrieval test parameters"-// @Success 200 {object} map[string]interface{}-// @Router /api/v1/datasets/search [post]-func (h *ChunkHandler) RetrievalTest(c *gin.Context) {- user, errorCode, errorMessage := GetUser(c)- if errorCode != common.CodeSuccess {- common.ErrorWithCode(c, errorCode, errorMessage)- return- }-- // Bind JSON request- var req service.RetrievalTestRequest- if err := c.ShouldBindJSON(&req); err != nil {- common.ResponseWithHttpCodeData(c, http.StatusBadRequest, common.CodeBadRequest, nil, "Invalid request body: "+err.Error())- return- }-- // Set default values for optional parameters- if req.Page == nil {- defaultPage := 1- req.Page = &defaultPage- }- if req.Size == nil {- defaultSize := 30- req.Size = &defaultSize- }- if req.TopK == nil {- defaultTopK := 1024- req.TopK = &defaultTopK- }- if req.UseKG == nil {- defaultUseKG := false- req.UseKG = &defaultUseKG- }-- // Strip and validate question. Matching Python chunk_api.py which returns- // an empty result for blank questions rather than an error.- if strings.TrimSpace(req.Question) == "" {- common.SuccessWithData(c, &service.RetrievalTestResponse{- Chunks: []map[string]interface{}{},- DocAggs: []map[string]interface{}{},- Total: 0,- }, "success")- return- }-- // Validate required fields- if req.Datasets == nil {- common.ResponseWithHttpCodeData(c, http.StatusBadRequest, common.CodeArgumentError, nil, "kb_id is required")- return- }-- if len(req.Datasets) == 0 {- common.ResponseWithHttpCodeData(c, http.StatusBadRequest, common.CodeArgumentError, nil, "kb_id array cannot be empty")- return- }- if req.TopK != nil && *req.TopK <= 0 {- common.ResponseWithHttpCodeData(c, http.StatusBadRequest, common.CodeArgumentError, nil, "top_k must be greater than 0")- return- }-- ctx := c.Request.Context()- // Call service with user ID for permission checks- resp, err := h.chunkService.RetrievalTest(ctx, &req, user.ID)- if err != nil {- common.Warn("dataset search failed", zap.String("error", err.Error()))- common.ResponseWithHttpCodeData(c, http.StatusInternalServerError, common.CodeServerError, nil, "dataset search failed")- return- }-- common.SuccessWithData(c, resp, "success")-}-// Get retrieves a chunk by ID.// @Summary Get Chunk// @Description Retrieve a single chunk by its ID.diff --git a/internal/handler/chunk_test.go b/internal/handler/chunk_test.goindex eb008babd9..b9791ec5aa 100644--- a/internal/handler/chunk_test.go+++ b/internal/handler/chunk_test.go@@ -20,12 +20,11 @@ import (// mockChunkSvc implements chunkSvcIface for testing ChunkHandler.// Only the methods actually called by the test are set; others panic.type mockChunkSvc struct {- retrievalTestFn func(ctx context.Context, req *service.RetrievalTestRequest, userID string) (*service.RetrievalTestResponse, error)- addChunkFn func(ctx context.Context, req *service.AddChunkRequest, userID string) (*service.AddChunkResponse, error)- listFn func(ctx context.Context, req *service.ListChunksRequest, userID string) (*service.ListChunksResponse, error)- switchChunksFn func(ctx context.Context, userID, datasetID, documentID string, availableInt int, chunkIDs []string) error- updateChunkFn func(ctx context.Context, req *service.UpdateChunkRequest, userID string) error- stopParsingFn func(ctx context.Context, userID, datasetID string, req service.StopParsingRequest) (*service.StopParsingResponse, common.ErrorCode, error)+ addChunkFn func(ctx context.Context, req *service.AddChunkRequest, userID string) (*service.AddChunkResponse, error)+ listFn func(ctx context.Context, req *service.ListChunksRequest, userID string) (*service.ListChunksResponse, error)+ switchChunksFn func(ctx context.Context, userID, datasetID, documentID string, availableInt int, chunkIDs []string) error+ updateChunkFn func(ctx context.Context, req *service.UpdateChunkRequest, userID string) error+ stopParsingFn func(ctx context.Context, userID, datasetID string, req service.StopParsingRequest) (*service.StopParsingResponse, common.ErrorCode, error)}type codedTestError struct {@@ -41,15 +40,6 @@ func (e codedTestError) Code() common.ErrorCode {return e.code}-func (m *mockChunkSvc) RetrievalTest(ctx context.Context, req *service.RetrievalTestRequest, userID string) (*service.RetrievalTestResponse, error) {- if m.retrievalTestFn != nil {- return m.retrievalTestFn(ctx, req, userID)- }- return &service.RetrievalTestResponse{- Chunks: []map[string]interface{}{{"docnm_kwd": "test", "content_with_weight": "content"}},- Total: 1,- }, nil-}func (m *mockChunkSvc) Get(context.Context, *service.GetChunkRequest, string) (*service.GetChunkResponse, error) {panic("not implemented")}@@ -90,28 +80,6 @@ func (m *mockChunkSvc) AddChunk(ctx context.Context, req *service.AddChunkRequesreturn &service.AddChunkResponse{Chunk: map[string]interface{}{"id": "chunk-1"}}, nil}-func setupChunkRetrievalTest(userID string) (*gin.Engine, *mockChunkSvc) {- mock := &mockChunkSvc{}- h := &ChunkHandler{chunkService: mock}- gin.SetMode(gin.TestMode)- r := gin.New()- r.Use(func(c *gin.Context) {- c.Set("user", &entity.User{ID: userID})- })- r.POST("/api/v1/datasets/search", h.RetrievalTest)- return r, mock-}--func setupChunkRetrievalTestNoAuth() *gin.Engine {- // Returns a router without the user middleware — used for error-path- // tests that don't call the service.- h := &ChunkHandler{}- gin.SetMode(gin.TestMode)- r := gin.New()- r.POST("/api/v1/datasets/search", h.RetrievalTest)- return r-}-func setupChunkStopParsingTest(userID string) (*gin.Engine, *mockChunkSvc) {mock := &mockChunkSvc{}h := &ChunkHandler{chunkService: mock}@@ -496,38 +464,6 @@ func TestChunkHandlerUpdateChunkStillRejectsUnknownFields(t *testing.T) {}}-func TestChunkRetrieval_EmptyQuestion(t *testing.T) {- r, _ := setupChunkRetrievalTest("user1")-- body := `{"dataset_ids": ["kb1"], "question": ""}`- w := httptest.NewRecorder()- req, _ := http.NewRequest("POST", "/api/v1/datasets/search", strings.NewReader(body))- req.Header.Set("Content-Type", "application/json")- r.ServeHTTP(w, req)-- if w.Code != http.StatusOK {- t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())- }- var resp map[string]interface{}- if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {- t.Fatal(err)- }- if resp["code"] != float64(common.CodeSuccess) {- t.Errorf("expected code 0, got %v: %q", resp["code"], resp["message"])- }- data, ok := resp["data"].(map[string]interface{})- if !ok {- t.Fatalf("expected data to be object, got %T", resp["data"])- }- chunks, _ := data["chunks"].([]interface{})- if chunks == nil || len(chunks) != 0 {- t.Errorf("expected empty chunks array, got %v", chunks)- }- if total, _ := data["total"].(float64); total != 0 {- t.Errorf("expected total 0, got %v", total)- }-}-func TestChunkStopParsing_Success(t *testing.T) {r, mock := setupChunkStopParsingTest("user1")mock.stopParsingFn = func(ctx context.Context, userID, datasetID string, req service.StopParsingRequest) (*service.StopParsingResponse, common.ErrorCode, error) {@@ -625,200 +561,6 @@ func TestChunkStopParsing_InvalidStateIncludesPythonErrorCode(t *testing.T) {}}-func TestChunkRetrieval_WhitespaceQuestion(t *testing.T) {- r, _ := setupChunkRetrievalTest("user1")-- body := `{"dataset_ids": ["kb1"], "question": " "}`- w := httptest.NewRecorder()- req, _ := http.NewRequest("POST", "/api/v1/datasets/search", strings.NewReader(body))- req.Header.Set("Content-Type", "application/json")- r.ServeHTTP(w, req)-- if w.Code != http.StatusOK {- t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())- }- var resp map[string]interface{}- if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {- t.Fatal(err)- }- if resp["code"] != float64(common.CodeSuccess) {- t.Errorf("expected code 0, got %v", resp["code"])- }-}--func TestChunkRetrieval_TopKZero(t *testing.T) {- r, _ := setupChunkRetrievalTest("user1")-- body := `{"dataset_ids": ["kb1"], "question": "test", "top_k": 0}`- w := httptest.NewRecorder()- req, _ := http.NewRequest("POST", "/api/v1/datasets/search", strings.NewReader(body))- req.Header.Set("Content-Type", "application/json")- r.ServeHTTP(w, req)-- if w.Code != http.StatusBadRequest {- t.Fatalf("expected 400, got %d: %s", w.Code, w.Body.String())- }- var resp map[string]interface{}- if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {- t.Fatal(err)- }- if msg, _ := resp["message"].(string); msg != "top_k must be greater than 0" {- t.Errorf("expected 'top_k must be greater than 0', got %q", msg)- }-}--func TestChunkRetrieval_MissingDatasetIDs(t *testing.T) {- r, _ := setupChunkRetrievalTest("user1")-- body := `{"question": "test"}`- w := httptest.NewRecorder()- req, _ := http.NewRequest("POST", "/api/v1/datasets/search", strings.NewReader(body))- req.Header.Set("Content-Type", "application/json")- r.ServeHTTP(w, req)-- if w.Code != http.StatusBadRequest {- t.Errorf("expected 400, got %d", w.Code)- }-}--func TestChunkRetrieval_EmptyDatasetIDs(t *testing.T) {- r, _ := setupChunkRetrievalTest("user1")-- body := `{"dataset_ids": [], "question": "test"}`- w := httptest.NewRecorder()- req, _ := http.NewRequest("POST", "/api/v1/datasets/search", strings.NewReader(body))- req.Header.Set("Content-Type", "application/json")- r.ServeHTTP(w, req)-- if w.Code != http.StatusBadRequest {- t.Errorf("expected 400, got %d", w.Code)- }- var resp map[string]interface{}- if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {- t.Fatal(err)- }- if msg, _ := resp["message"].(string); msg != "kb_id array cannot be empty" {- t.Errorf("expected 'kb_id array cannot be empty', got %q", msg)- }-}--func TestChunkRetrieval_NoAuth(t *testing.T) {- r := setupChunkRetrievalTestNoAuth()-- body := `{"dataset_ids": ["kb1"], "question": "test"}`- w := httptest.NewRecorder()- req, _ := http.NewRequest("POST", "/api/v1/datasets/search", strings.NewReader(body))- req.Header.Set("Content-Type", "application/json")- r.ServeHTTP(w, req)-- if w.Code != http.StatusOK {- t.Errorf("expected 200, got %d", w.Code)- }- // jsonError returns HTTP 200 with error code in body- var resp map[string]interface{}- if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {- t.Fatal(err)- }- if resp["code"] == float64(common.CodeSuccess) {- t.Errorf("expected error code, got %v", resp["code"])- }-}--func TestChunkRetrieval_InvalidJSON(t *testing.T) {- r, _ := setupChunkRetrievalTest("user1")-- w := httptest.NewRecorder()- req, _ := http.NewRequest("POST", "/api/v1/datasets/search", strings.NewReader("{invalid}"))- req.Header.Set("Content-Type", "application/json")- r.ServeHTTP(w, req)-- if w.Code != http.StatusBadRequest {- t.Errorf("expected 400, got %d", w.Code)- }-}--func TestChunkRetrieval_Success(t *testing.T) {- _, mock := setupChunkRetrievalTest("user1")- mock.retrievalTestFn = func(ctx context.Context, req *service.RetrievalTestRequest, userID string) (*service.RetrievalTestResponse, error) {- if userID != "user1" {- t.Errorf("expected userID 'user1', got %q", userID)- }- return &service.RetrievalTestResponse{- Chunks: []map[string]interface{}{{"docnm_kwd": "result"}},- DocAggs: []map[string]interface{}{{"doc_id": "1", "count": float64(1)}},- Total: 1,- }, nil- }-- gin.SetMode(gin.TestMode)- r := gin.New()- r.Use(func(c *gin.Context) {- c.Set("user", &entity.User{ID: "user1"})- })- h := &ChunkHandler{chunkService: mock}- r.POST("/api/v1/datasets/search", h.RetrievalTest)-- body := `{"dataset_ids": ["kb1"], "question": "test"}`- w := httptest.NewRecorder()- req, _ := http.NewRequest("POST", "/api/v1/datasets/search", strings.NewReader(body))- req.Header.Set("Content-Type", "application/json")- r.ServeHTTP(w, req)-- if w.Code != http.StatusOK {- t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())- }- var resp map[string]interface{}- if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {- t.Fatal(err)- }- if resp["code"] != float64(common.CodeSuccess) {- t.Fatalf("expected code 0, got %v: %q", resp["code"], resp["message"])- }- data, ok := resp["data"].(map[string]interface{})- if !ok {- t.Fatalf("expected data object, got %T", resp["data"])- }- if total, _ := data["total"].(float64); total != 1 {- t.Errorf("expected total 1, got %v", total)- }-}--func TestChunkRetrieval_ServiceError(t *testing.T) {- _, mock := setupChunkRetrievalTest("user1")- mock.retrievalTestFn = func(ctx context.Context, req *service.RetrievalTestRequest, userID string) (*service.RetrievalTestResponse, error) {- return nil, errors.New("db connection refused")- }-- gin.SetMode(gin.TestMode)- r := gin.New()- r.Use(func(c *gin.Context) {- c.Set("user", &entity.User{ID: "user1"})- })- h := &ChunkHandler{chunkService: mock}- r.POST("/api/v1/datasets/search", h.RetrievalTest)-- body := `{"dataset_ids": ["kb1"], "question": "test"}`- w := httptest.NewRecorder()- req, _ := http.NewRequest("POST", "/api/v1/datasets/search", strings.NewReader(body))- req.Header.Set("Content-Type", "application/json")- r.ServeHTTP(w, req)-- if w.Code != http.StatusInternalServerError {- t.Fatalf("expected 500, got %d: %s", w.Code, w.Body.String())- }- var resp map[string]interface{}- if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {- t.Fatal(err)- }- msg, _ := resp["message"].(string)- if msg != "dataset search failed" {- t.Errorf("expected generic error message, got %q", msg)- }- if strings.Contains(msg, "db connection refused") {- t.Errorf("internal error details leaked to response: %q", msg)- }-}-type addChunkTestError struct {code common.ErrorCodemsg stringdiff --git a/internal/handler/searchbot.go b/internal/handler/searchbot.goindex 892d37f4f3..a651805a47 100644--- a/internal/handler/searchbot.go+++ b/internal/handler/searchbot.go@@ -558,14 +558,12 @@ func toRetrievalServiceRequest(h *SearchBotRetrievalTestRequest) *service.Retrie}}-// ptrFloat64 returns a pointer to a float64 value.-func ptrFloat64(v float64) *float64 { return &v }--func intPtr(v int) *int { return &v }-func floatPtr(v float64) *float64 { return &v }-// applyRetrievalDefaults fills in default values for optional fields,// matching Python bot_api.py retrieval_test endpoint.+//+// similarity_threshold, vector_similarity_weight and top_k are left nil:+// ChunkService.RetrievalTest resolves them against the search app's saved+// search_config, which a default here would hide.func applyRetrievalDefaults(req *SearchBotRetrievalTestRequest) {if req.Page == nil {v := 1@@ -575,10 +573,6 @@ func applyRetrievalDefaults(req *SearchBotRetrievalTestRequest) {v := 30req.Size = &v}- if req.TopK == nil {- v := 1024- req.TopK = &v- }if req.UseKG == nil {v := falsereq.UseKG = &v@@ -587,12 +581,4 @@ func applyRetrievalDefaults(req *SearchBotRetrievalTestRequest) {v := falsereq.Keyword = &v}- if req.SimilarityThreshold == nil {- v := 0.0- req.SimilarityThreshold = &v- }- if req.VectorSimilarityWeight == nil {- v := 0.3- req.VectorSimilarityWeight = &v- }}diff --git a/internal/handler/searchbot_test.go b/internal/handler/searchbot_test.goindex 0d01b56bf8..640f3760a7 100644--- a/internal/handler/searchbot_test.go+++ b/internal/handler/searchbot_test.go@@ -285,6 +285,8 @@ func TestSearchBotsRetrieval_WhitespaceOnlyKbID(t *testing.T) {func TestSearchBotsRetrieval_DefaultsApplied(t *testing.T) {// Verify that when optional fields are omitted, the handler applies// defaults matching Python bot_api.py retrieval_test endpoint.+ // similarity_threshold / vector_similarity_weight / top_k must reach the+ // service as nil: it resolves them against the saved search_config._, mockSvc, r := setupSearchbotsTest("user1")body := `{"kb_ids": ["kb1"], "question": "does this default?"}`@@ -307,20 +309,20 @@ func TestSearchBotsRetrieval_DefaultsApplied(t *testing.T) {if svcReq.Size == nil || *svcReq.Size != 30 {t.Errorf("Size = %v, want 30", nullableInt(svcReq.Size))}- if svcReq.TopK == nil || *svcReq.TopK != 1024 {- t.Errorf("TopK = %v, want 1024", nullableInt(svcReq.TopK))- }if svcReq.UseKG == nil || *svcReq.UseKG != false {t.Errorf("UseKG = %v, want false", nullableBool(svcReq.UseKG))}if svcReq.Keyword == nil || *svcReq.Keyword != false {t.Errorf("Keyword = %v, want false", nullableBool(svcReq.Keyword))}- if svcReq.SimilarityThreshold == nil || *svcReq.SimilarityThreshold != 0.0 {- t.Errorf("SimilarityThreshold = %v, want 0.0", nullableFloat(svcReq.SimilarityThreshold))+ if svcReq.TopK != nil {+ t.Errorf("TopK = %v, want nil (resolved by the service)", nullableInt(svcReq.TopK))+ }+ if svcReq.SimilarityThreshold != nil {+ t.Errorf("SimilarityThreshold = %v, want nil (resolved by the service)", nullableFloat(svcReq.SimilarityThreshold))}- if svcReq.VectorSimilarityWeight == nil || *svcReq.VectorSimilarityWeight != 0.3 {- t.Errorf("VectorSimilarityWeight = %v, want 0.3", nullableFloat(svcReq.VectorSimilarityWeight))+ if svcReq.VectorSimilarityWeight != nil {+ t.Errorf("VectorSimilarityWeight = %v, want nil (resolved by the service)", nullableFloat(svcReq.VectorSimilarityWeight))}}diff --git a/internal/service/chunk/chunk.go b/internal/service/chunk/chunk.goindex c4075160c6..a867a9af81 100644--- a/internal/service/chunk/chunk.go+++ b/internal/service/chunk/chunk.go@@ -203,6 +203,9 @@ func (s *ChunkService) RetrievalTest(ctx context.Context, req *service.Retrieval// Determine meta_data_filtervar chatID stringvar chatModelForFilter *models.ChatModel+ // Filled by the search_id block below, then used to resolve retrieval+ // parameters.+ var searchConfig map[string]interface{}filter := req.FiltertenantRerankID := req.TenantRerankIDrerankID := req.RerankID@@ -220,7 +223,8 @@ func (s *ChunkService) RetrievalTest(ctx context.Context, req *service.Retrievalif err != nil {return nil, fmt.Errorf("failed to get search app %s: %w", *req.SearchID, err)}- if searchConfig, ok := searchConfigMap(searchDetail["search_config"]); ok && searchConfig != nil {+ if cfg, ok := searchConfigMap(searchDetail["search_config"]); ok && cfg != nil {+ searchConfig = cfgif req.RerankCandidatesCount == nil || *req.RerankCandidatesCount == 0 {rerankCandidatesCount = 100if configuredRerankCandidatesCount, ok := common.GetInt(searchConfig["rerank_candidates_count"]); ok {@@ -393,6 +397,10 @@ func (s *ChunkService) RetrievalTest(ctx context.Context, req *service.Retrieval}}+ // Resolved here because search_config is only known after the search_id+ // block above.+ similarityThreshold, vectorSimilarityWeight, topK := resolveSearchbotRetrievalParams(req, searchConfig)+retrievalReq := &nlp.RetrievalRequest{TenantIDs: tenantIDs,Question: modifiedQuestion,@@ -401,9 +409,9 @@ func (s *ChunkService) RetrievalTest(ctx context.Context, req *service.RetrievalPage: common.CoalesceInt(req.Page, 1),PageSize: common.CoalesceInt(req.Size, 30),RerankCandidatesCount: &rerankCandidatesCount,- KNNTopK: req.TopK,- SimilarityThreshold: req.SimilarityThreshold,- VectorSimilarityWeight: req.VectorSimilarityWeight,+ KNNTopK: &topK,+ SimilarityThreshold: &similarityThreshold,+ VectorSimilarityWeight: &vectorSimilarityWeight,RerankModel: rerankModel,RankFeature: &labels,EmbeddingModel: embeddingModel,@@ -441,6 +449,49 @@ func (s *ChunkService) RetrievalTest(ctx context.Context, req *service.Retrieval}, nil}+// Endpoint defaults for the searchbot retrieval_test path, used when neither+// the request nor the saved search_config provides a value.+const (+ searchbotDefaultSimilarityThreshold = 0.0+ searchbotDefaultVectorSimilarityWeight = 0.3+ searchbotDefaultTopK = 1024+)++// resolveSearchbotRetrievalParams resolves similarity_threshold,+// vector_similarity_weight and top_k from the request, falling back to the+// search app's saved search_config and then to the endpoint defaults. A nil+// pointer means the field was omitted, so an explicit zero is kept — it is how+// a caller disables the threshold floor or selects a zero vector weight.+// searchConfig may be nil.+//+// vector_similarity_weight also derives from keywords_similarity_weight+// (vector = 1 - keywords) when the config carries only the keyword form.+func resolveSearchbotRetrievalParams(req *service.RetrievalTestRequest, searchConfig map[string]interface{}) (similarityThreshold, vectorSimilarityWeight float64, topK int) {+ similarityThreshold = searchbotDefaultSimilarityThreshold+ if req.SimilarityThreshold != nil {+ similarityThreshold = *req.SimilarityThreshold+ } else if configured, ok := common.ToFloat(searchConfig["similarity_threshold"]); ok {+ similarityThreshold = configured+ }++ vectorSimilarityWeight = searchbotDefaultVectorSimilarityWeight+ if req.VectorSimilarityWeight != nil {+ vectorSimilarityWeight = *req.VectorSimilarityWeight+ } else if configured, ok := common.ToFloat(searchConfig["vector_similarity_weight"]); ok {+ vectorSimilarityWeight = configured+ } else if keywords, ok := common.ToFloat(searchConfig["keywords_similarity_weight"]); ok {+ vectorSimilarityWeight = 1 - keywords+ }++ topK = searchbotDefaultTopK+ if req.TopK != nil {+ topK = *req.TopK+ } else if configured, ok := common.GetInt(searchConfig["top_k"]); ok {+ topK = configured+ }+ return similarityThreshold, vectorSimilarityWeight, topK+}+// knowledgebaseEmbeddingKey groups datasets by their resolved base embedding// model name (e.g. "BAAI/bge-m3") so datasets pointing at the same model// through different provider instances or storage forms (tenant_model id vsdiff --git a/internal/service/chunk/retrieval_params_test.go b/internal/service/chunk/retrieval_params_test.gonew file mode 100644index 0000000000..61d503c9c2--- /dev/null+++ b/internal/service/chunk/retrieval_params_test.go@@ -0,0 +1,140 @@+//+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.+//++package chunk++import (+ "math"+ "testing"++ "ragflow/internal/service"+)++func floatPtr(v float64) *float64 { return &v }++func intPtr(v int) *int { return &v }++// TestResolveSearchbotRetrievalParams_EndpointDefaults: no request value and+// no saved config leaves the endpoint defaults.+func TestResolveSearchbotRetrievalParams_EndpointDefaults(t *testing.T) {+ threshold, weight, topK := resolveSearchbotRetrievalParams(&service.RetrievalTestRequest{}, nil)+ if threshold != 0.0 {+ t.Errorf("similarityThreshold = %v, want 0.0", threshold)+ }+ if weight != 0.3 {+ t.Errorf("vectorSimilarityWeight = %v, want 0.3", weight)+ }+ if topK != 1024 {+ t.Errorf("topK = %d, want 1024", topK)+ }+}++// TestResolveSearchbotRetrievalParams_RequestWins: a request value beats the+// saved config and the endpoint default.+func TestResolveSearchbotRetrievalParams_RequestWins(t *testing.T) {+ req := &service.RetrievalTestRequest{+ SimilarityThreshold: floatPtr(0.5),+ VectorSimilarityWeight: floatPtr(0.8),+ TopK: intPtr(100),+ }+ searchConfig := map[string]interface{}{+ "similarity_threshold": 0.1,+ "vector_similarity_weight": 0.9,+ "top_k": 50,+ }+ threshold, weight, topK := resolveSearchbotRetrievalParams(req, searchConfig)+ if threshold != 0.5 {+ t.Errorf("similarityThreshold = %v, want 0.5", threshold)+ }+ if weight != 0.8 {+ t.Errorf("vectorSimilarityWeight = %v, want 0.8", weight)+ }+ if topK != 100 {+ t.Errorf("topK = %d, want 100", topK)+ }+}++// TestResolveSearchbotRetrievalParams_SavedConfigFallback: an omitted request+// value falls back to the saved config.+func TestResolveSearchbotRetrievalParams_SavedConfigFallback(t *testing.T) {+ searchConfig := map[string]interface{}{+ "similarity_threshold": 0.2,+ "vector_similarity_weight": 0.7,+ "top_k": 200,+ }+ threshold, weight, topK := resolveSearchbotRetrievalParams(&service.RetrievalTestRequest{}, searchConfig)+ if threshold != 0.2 {+ t.Errorf("similarityThreshold = %v, want 0.2 from search_config", threshold)+ }+ if weight != 0.7 {+ t.Errorf("vectorSimilarityWeight = %v, want 0.7 from search_config", weight)+ }+ if topK != 200 {+ t.Errorf("topK = %d, want 200 from search_config", topK)+ }+}++// TestResolveSearchbotRetrievalParams_KeywordsWeightConfig: a config carrying+// only keywords_similarity_weight drives vector as 1 - keywords.+func TestResolveSearchbotRetrievalParams_KeywordsWeightConfig(t *testing.T) {+ searchConfig := map[string]interface{}{"keywords_similarity_weight": 0.7}+ _, weight, _ := resolveSearchbotRetrievalParams(&service.RetrievalTestRequest{}, searchConfig)+ if math.Abs(weight-0.3) > 1e-9 {+ t.Errorf("vectorSimilarityWeight = %v, want 0.3 (1 - 0.7)", weight)+ }+}++// TestResolveSearchbotRetrievalParams_ExplicitZeroWins: an explicit zero+// overrides the saved config — it is how a caller disables the threshold floor+// or selects a zero vector weight. Only a nil pointer means "omitted".+func TestResolveSearchbotRetrievalParams_ExplicitZeroWins(t *testing.T) {+ req := &service.RetrievalTestRequest{+ SimilarityThreshold: floatPtr(0),+ VectorSimilarityWeight: floatPtr(0),+ TopK: intPtr(0),+ }+ searchConfig := map[string]interface{}{+ "similarity_threshold": 0.15,+ "vector_similarity_weight": 0.6,+ "top_k": 300,+ }+ threshold, weight, topK := resolveSearchbotRetrievalParams(req, searchConfig)+ if threshold != 0 {+ t.Errorf("similarityThreshold = %v, want the explicit 0", threshold)+ }+ if weight != 0 {+ t.Errorf("vectorSimilarityWeight = %v, want the explicit 0", weight)+ }+ if topK != 0 {+ t.Errorf("topK = %d, want the explicit 0", topK)+ }+}++// TestResolveSearchbotRetrievalParams_PartialConfig: fields the saved config+// omits keep their endpoint defaults.+func TestResolveSearchbotRetrievalParams_PartialConfig(t *testing.T) {+ searchConfig := map[string]interface{}{"similarity_threshold": 0.4}+ threshold, weight, topK := resolveSearchbotRetrievalParams(&service.RetrievalTestRequest{}, searchConfig)+ if threshold != 0.4 {+ t.Errorf("similarityThreshold = %v, want 0.4 from search_config", threshold)+ }+ if weight != 0.3 {+ t.Errorf("vectorSimilarityWeight = %v, want 0.3 default", weight)+ }+ if topK != 1024 {+ t.Errorf("topK = %d, want 1024 default", topK)+ }+}diff --git a/internal/service/similarity_weight.go b/internal/service/similarity_weight.goindex 7e1c4356c3..7a3ede092b 100644--- a/internal/service/similarity_weight.go+++ b/internal/service/similarity_weight.go@@ -63,11 +63,20 @@ func NormalizeSimilarityWeights(values map[string]interface{}) error {return nil}- values["vector_similarity_weight"] = *resolvedVectorWeight- values["keywords_similarity_weight"] = 1 - *resolvedVectorWeight+ // Round before persisting: the float64 complement (1 - 0.7 =+ // 0.30000000000000004) would otherwise land in search_config verbatim.+ // keywords derives from the rounded vector, so the pair stays complementary.+ vectorWeight := roundSimilarityWeight(*resolvedVectorWeight)+ values["vector_similarity_weight"] = vectorWeight+ values["keywords_similarity_weight"] = roundSimilarityWeight(1 - vectorWeight)return nil}+// roundSimilarityWeight snaps a weight to 6 decimal places.+func roundSimilarityWeight(weight float64) float64 {+ return math.Round(weight*1e6) / 1e6+}+func similarityWeightFromMap(values map[string]interface{}, key string) (*float64, error) {value, ok := values[key]if !ok || value == nil {diff --git a/internal/service/similarity_weight_test.go b/internal/service/similarity_weight_test.goindex 5f15cbbef9..4f1e6df502 100644--- a/internal/service/similarity_weight_test.go+++ b/internal/service/similarity_weight_test.go@@ -68,15 +68,52 @@ func TestResolveVectorSimilarityWeight(t *testing.T) {}func TestNormalizeSimilarityWeights(t *testing.T) {- values := map[string]interface{}{"keywords_similarity_weight": 0.7}- if err := NormalizeSimilarityWeights(values); err != nil {- t.Fatalf("NormalizeSimilarityWeights failed: %v", err)- }- if got := values["vector_similarity_weight"].(float64); math.Abs(got-0.3) > similarityWeightTolerance {- t.Fatalf("vector_similarity_weight = %v, want 0.3", got)+ tests := []struct {+ name string+ values map[string]interface{}+ wantVector float64+ wantKeywords float64+ }{+ {+ // 1 - 0.7 is 0.30000000000000004 in float64; the persisted pair+ // must be the clean 0.3 / 0.7.+ name: "keyword weight derives an exact vector weight",+ values: map[string]interface{}{"keywords_similarity_weight": 0.7},+ wantVector: 0.3,+ wantKeywords: 0.7,+ },+ {+ name: "vector weight derives an exact keyword weight",+ values: map[string]interface{}{"vector_similarity_weight": 0.3},+ wantVector: 0.3,+ wantKeywords: 0.7,+ },+ {+ name: "matching weights are persisted unchanged",+ values: map[string]interface{}{+ "keywords_similarity_weight": 0.7,+ "vector_similarity_weight": 0.3,+ },+ wantVector: 0.3,+ wantKeywords: 0.7,+ },}- if got := values["keywords_similarity_weight"].(float64); got != 0.7 {- t.Fatalf("keywords_similarity_weight = %v, want 0.7", got)++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ if err := NormalizeSimilarityWeights(tt.values); err != nil {+ t.Fatalf("NormalizeSimilarityWeights failed: %v", err)+ }+ if got := tt.values["vector_similarity_weight"].(float64); got != tt.wantVector {+ t.Fatalf("vector_similarity_weight = %v, want exactly %v", got, tt.wantVector)+ }+ if got := tt.values["keywords_similarity_weight"].(float64); got != tt.wantKeywords {+ t.Fatalf("keywords_similarity_weight = %v, want exactly %v", got, tt.wantKeywords)+ }+ if sum := tt.values["vector_similarity_weight"].(float64) + tt.values["keywords_similarity_weight"].(float64); sum != 1 {+ t.Fatalf("weights sum to %v, want exactly 1", sum)+ }+ })}}diff --git a/internal/service/skill_search.go b/internal/service/skill_search.goindex 697814f300..d5c5f31ee5 100644--- a/internal/service/skill_search.go+++ b/internal/service/skill_search.go@@ -29,7 +29,6 @@ import ("ragflow/internal/entity""ragflow/internal/entity/models""ragflow/internal/permission"- "ragflow/internal/utility""strings""go.uber.org/zap"@@ -583,13 +582,13 @@ func (s *SkillSearchService) convertChunksToResults(chunks []map[string]interfac// Note: SCORE/SIMILARITY may be float32 or float64 depending on Infinity versionbm25Score := 0.0if scoreVal, ok := chunk["SCORE"]; ok {- if f, ok := utility.ToFloat64(scoreVal); ok {+ if f, ok := common.ToFloat(scoreVal); ok {bm25Score = f}}vectorScore := 0.0if simVal, ok := chunk["SIMILARITY"]; ok {- if f, ok := utility.ToFloat64(simVal); ok {+ if f, ok := common.ToFloat(simVal); ok {vectorScore = f}}diff --git a/internal/utility/convert.go b/internal/utility/convert.goindex 0b37a6dd15..6ba2841d18 100644--- a/internal/utility/convert.go+++ b/internal/utility/convert.go@@ -202,28 +202,6 @@ func SetFieldArray(result map[string]interface{}, destKey string, v interface{})}}-// ToFloat64 converts various types to float64-func ToFloat64(val interface{}) (float64, bool) {- switch v := val.(type) {- case float64:- return v, true- case float32:- return float64(v), true- case int:- return float64(v), true- case int64:- return float64(v), true- case string:- f, err := strconv.ParseFloat(v, 64)- if err != nil {- return 0, false- }- return f, true- default:- return 0, false- }-}-// ConvertToStringSlice converts an interface{} to []string// e.g. []interface{}{"a", "b", "c"} -> []string{"a", "b", "c"}// e.g. "hello" -> []string{"hello"}