Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

Porting #18737 to go (#18745)

Wang Qi158d672c5efa
diff --git a/internal/agent/tool/retrieval_nlp.go b/internal/agent/tool/retrieval_nlp.go
index 9fbcfbbf79..437a12dd61 100644
--- a/internal/agent/tool/retrieval_nlp.go
+++ b/internal/agent/tool/retrieval_nlp.go
@@ -29,8 +29,8 @@
// tool.RetrievalRequest.Query → nlp.RetrievalRequest.Question
// tool.RetrievalRequest.DatasetIDs → nlp.RetrievalRequest.KbIDs
// tool.RetrievalRequest.TopN → nlp.RetrievalRequest.PageSize
-// tool.RetrievalRequest.TopK → nlp.RetrievalRequest.Top
-// (fallback Top=TopN*4 so rerank
+// tool.RetrievalRequest.TopK → nlp.RetrievalRequest.KNNTopK
+// (fallback KNNTopK=TopN*4 so rerank
// has headroom)
// tool.RetrievalRequest.KeywordsSimilarityWeight
// → nlp.RetrievalRequest.VectorSimilarityWeight
@@ -329,10 +329,10 @@ func nlpRequestFromRetrieval(
nlpReq.RerankCandidatesCount = &req.RerankCandidatesCount
}
if req.TopK > 0 {
- nlpReq.Top = &req.TopK
+ nlpReq.KNNTopK = &req.TopK
} else if topN > 0 {
rerankBudget := topN * 4
- nlpReq.Top = &rerankBudget
+ nlpReq.KNNTopK = &rerankBudget
}
if req.SimilarityThreshold != nil {
nlpReq.SimilarityThreshold = req.SimilarityThreshold
diff --git a/internal/agent/tool/retrieval_nlp_test.go b/internal/agent/tool/retrieval_nlp_test.go
index 2de87c92fe..a6a1aa34ca 100644
--- a/internal/agent/tool/retrieval_nlp_test.go
+++ b/internal/agent/tool/retrieval_nlp_test.go
@@ -260,8 +260,8 @@ func TestNLPRequestFromRetrieval_ThreadsSearchControls(t *testing.T) {
if got.Page != 1 || got.PageSize != 3 {
t.Fatalf("Page/PageSize=%d/%d want 1/3", got.Page, got.PageSize)
}
- if got.Top == nil || *got.Top != 99 {
- t.Fatalf("Top=%v want 99", got.Top)
+ if got.KNNTopK == nil || *got.KNNTopK != 99 {
+ t.Fatalf("KNNTopK=%v want 99", got.KNNTopK)
}
if got.SimilarityThreshold == nil || *got.SimilarityThreshold != 0.42 {
t.Fatalf("SimilarityThreshold=%v want 0.42", got.SimilarityThreshold)
@@ -281,8 +281,8 @@ func TestNLPRequestFromRetrieval_FallsBackToTopNHeadroom(t *testing.T) {
TopN: 3,
}, []string{"tenant-a"}, 3, &modelModule.EmbeddingModel{})
- if got.Top == nil || *got.Top != 12 {
- t.Fatalf("Top=%v want 12", got.Top)
+ if got.KNNTopK == nil || *got.KNNTopK != 12 {
+ t.Fatalf("KNNTopK=%v want 12", got.KNNTopK)
}
if got.VectorSimilarityWeight != nil {
t.Fatalf("VectorSimilarityWeight=%v want nil", got.VectorSimilarityWeight)
diff --git a/internal/engine/elasticsearch/chunk.go b/internal/engine/elasticsearch/chunk.go
index 15b80500e3..9a5cbfa1cd 100644
--- a/internal/engine/elasticsearch/chunk.go
+++ b/internal/engine/elasticsearch/chunk.go
@@ -1175,13 +1175,17 @@ func (e *Engine) Search(ctx context.Context, req *types.SearchRequest) (*types.S
if k <= 0 {
k = 1024
}
- numCandidates := k * 2
+ k = min(k, 10000)
+ numCandidates := min(k*2, 10000)
similarity := 0.0
if matchDense.ExtraOptions != nil {
if sim, ok := matchDense.ExtraOptions["similarity"].(float64); ok {
similarity = sim
}
+ if n, ok := common.GetInt(matchDense.ExtraOptions["num_candidates"]); ok {
+ numCandidates = max(k, min(n, 10000))
+ }
}
vectorFieldName := matchDense.VectorColumnName
diff --git a/internal/engine/elasticsearch/chunk_helpers_test.go b/internal/engine/elasticsearch/chunk_helpers_test.go
index 4a76eb621c..24b47163fc 100644
--- a/internal/engine/elasticsearch/chunk_helpers_test.go
+++ b/internal/engine/elasticsearch/chunk_helpers_test.go
@@ -10,6 +10,7 @@ import (
"github.com/elastic/go-elasticsearch/v8"
+ "ragflow/internal/common"
"ragflow/internal/engine/types"
)
@@ -40,6 +41,48 @@ func TestBuildQueryStringQueryKeepsDocumentFieldsUnchanged(t *testing.T) {
assertEqual(t, queryString["fields"], []string{"name^10"})
}
+func TestSearchUsesConfiguredKNNNumCandidates(t *testing.T) {
+ if err := common.InitLogger("info", common.FileOutput{}, "elasticsearch_test"); err != nil {
+ t.Fatalf("init logger: %v", err)
+ }
+ var searchQuery map[string]interface{}
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if err := json.NewDecoder(r.Body).Decode(&searchQuery); err != nil {
+ t.Errorf("decode search query: %v", err)
+ }
+ w.Header().Set("X-Elastic-Product", "Elasticsearch")
+ w.Header().Set("Content-Type", "application/json")
+ _, _ = w.Write([]byte(`{"hits":{"total":{"value":0},"hits":[]}}`))
+ }))
+ defer server.Close()
+
+ client, err := elasticsearch.NewClient(elasticsearch.Config{Addresses: []string{server.URL}})
+ if err != nil {
+ t.Fatalf("new elasticsearch client: %v", err)
+ }
+ engine := &Engine{client: client}
+ _, err = engine.Search(t.Context(), &types.SearchRequest{
+ IndexNames: []string{"ragflow_tenant"},
+ Limit: 30,
+ MatchExprs: []interface{}{&types.MatchDenseExpr{
+ VectorColumnName: "q_2_vec",
+ EmbeddingData: []float64{0.1, 0.2},
+ TopN: 128,
+ ExtraOptions: map[string]interface{}{"num_candidates": 4096},
+ }},
+ })
+ if err != nil {
+ t.Fatalf("Search: %v", err)
+ }
+ knn, ok := searchQuery["knn"].(map[string]interface{})
+ if !ok {
+ t.Fatalf("KNN query missing from %#v", searchQuery)
+ }
+ if knn["k"] != float64(128) || knn["num_candidates"] != float64(4096) {
+ t.Fatalf("KNN parameters = (%v, %v), want (128, 4096)", knn["k"], knn["num_candidates"])
+ }
+}
+
func TestUpdateSingleMemoryMessageWaitsForRefresh(t *testing.T) {
var gotRefresh string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
diff --git a/internal/handler/dataset.go b/internal/handler/dataset.go
index 1fbaf8509e..2074fc29ec 100644
--- a/internal/handler/dataset.go
+++ b/internal/handler/dataset.go
@@ -1152,9 +1152,13 @@ func (h *DatasetsHandler) SearchDatasets(c *gin.Context) {
defaultSize := 30
req.Size = &defaultSize
}
- if req.TopK == nil {
+ if req.KNNTopK == nil && req.TopK == nil {
defaultTopK := 1024
- req.TopK = &defaultTopK
+ req.KNNTopK = &defaultTopK
+ }
+ if req.KNNNumCandidates == nil {
+ defaultNumCandidates := 2048
+ req.KNNNumCandidates = &defaultNumCandidates
}
if req.UseKG == nil {
defaultUseKG := false
@@ -1257,22 +1261,40 @@ func (h *DatasetsHandler) SearchDataset(c *gin.Context) {
}
func validateSearchDatasetsRequest(req *service.SearchDatasetsRequest) error {
- return validateSearchParams(req.Page, req.Size, req.TopK, req.SimilarityThreshold, req.VectorSimilarityWeight)
+ return validateSearchParams(req.Page, req.Size, req.KNNTopK, req.TopK, req.KNNNumCandidates, req.SimilarityThreshold, req.VectorSimilarityWeight)
}
func validateSearchDatasetRequest(req *service.SearchDatasetRequest) error {
- return validateSearchParams(req.Page, req.Size, req.TopK, req.SimilarityThreshold, req.VectorSimilarityWeight)
+ return validateSearchParams(req.Page, req.Size, req.KNNTopK, req.TopK, req.KNNNumCandidates, req.SimilarityThreshold, req.VectorSimilarityWeight)
}
-func validateSearchParams(page, size, topK *int, similarityThreshold, vectorSimilarityWeight *float64) error {
+func validateSearchParams(page, size, knnTopK, topK, knnNumCandidates *int, similarityThreshold, vectorSimilarityWeight *float64) error {
if page != nil && *page < 1 {
return fmt.Errorf("page must be greater than or equal to 1")
}
if size != nil && *size < 1 {
return fmt.Errorf("size must be greater than or equal to 1")
}
- if topK != nil && *topK < 1 {
- return fmt.Errorf("top_k must be greater than or equal to 1")
+ effectiveKNNTopK := 1024
+ knnTopKName := "knn_top_k"
+ if knnTopK != nil {
+ effectiveKNNTopK = *knnTopK
+ } else if topK != nil {
+ effectiveKNNTopK = *topK
+ knnTopKName = "top_k (alias for knn_top_k)"
+ }
+ if effectiveKNNTopK < 1 || effectiveKNNTopK > 2048 {
+ return fmt.Errorf("%s must be between 1 and 2048", knnTopKName)
+ }
+ effectiveKNNNumCandidates := 2048
+ if knnNumCandidates != nil {
+ effectiveKNNNumCandidates = *knnNumCandidates
+ }
+ if effectiveKNNNumCandidates < 1 || effectiveKNNNumCandidates > 10000 {
+ return fmt.Errorf("knn_num_candidates must be between 1 and 10000")
+ }
+ if effectiveKNNNumCandidates < effectiveKNNTopK {
+ return fmt.Errorf("knn_num_candidates must be greater than or equal to knn_top_k")
}
if similarityThreshold != nil && (*similarityThreshold < 0 || *similarityThreshold > 1) {
return fmt.Errorf("similarity_threshold must be between 0 and 1")
diff --git a/internal/handler/dataset_search_test.go b/internal/handler/dataset_search_test.go
index 2163da217e..b093d08a76 100644
--- a/internal/handler/dataset_search_test.go
+++ b/internal/handler/dataset_search_test.go
@@ -50,7 +50,7 @@ func TestDatasetsHandlerSearchDataset(t *testing.T) {
h := &DatasetsHandler{searchDatasetService: fake}
rec := httptest.NewRecorder()
- req := httptest.NewRequest(http.MethodPost, "/api/v1/datasets/ds-1/search", strings.NewReader(`{"question":"hello","doc_ids":["doc-1"],"top_k":7}`))
+ req := httptest.NewRequest(http.MethodPost, "/api/v1/datasets/ds-1/search", strings.NewReader(`{"question":"hello","doc_ids":["doc-1"],"knn_top_k":7,"knn_num_candidates":14}`))
req.Header.Set("Content-Type", "application/json")
c, _ := gin.CreateTestContext(rec)
c.Request = req
@@ -68,6 +68,9 @@ func TestDatasetsHandlerSearchDataset(t *testing.T) {
if fake.req == nil || fake.req.Question != "hello" || len(fake.req.DocIDs) != 1 || fake.req.DocIDs[0] != "doc-1" {
t.Fatalf("request = %#v", fake.req)
}
+ if fake.req.KNNTopK == nil || *fake.req.KNNTopK != 7 || fake.req.KNNNumCandidates == nil || *fake.req.KNNNumCandidates != 14 {
+ t.Fatalf("KNN parameters = (%v, %v), want (7, 14)", fake.req.KNNTopK, fake.req.KNNNumCandidates)
+ }
if len(fake.req.ToSearchDatasetsRequest("ds-1").DatasetIDs) != 1 {
t.Fatal("request conversion failed")
}
@@ -174,6 +177,9 @@ func TestDatasetsHandlerSearchDatasetsSuccess(t *testing.T) {
if fake.req.IncludeCompiledChunks == nil || *fake.req.IncludeCompiledChunks {
t.Fatalf("include_knowledge_compilation=%v want false", fake.req.IncludeCompiledChunks)
}
+ if fake.req.TopK == nil || *fake.req.TopK != 7 {
+ t.Fatalf("legacy top_k alias=%v want 7", fake.req.TopK)
+ }
body := decodeSearchResponse(t, rec)
if body["code"] != float64(common.CodeSuccess) {
t.Fatalf("code=%v want=%d body=%s", body["code"], common.CodeSuccess, rec.Body.String())
@@ -191,6 +197,11 @@ func TestDatasetsHandlerSearchDatasetsValidationErrorsUseArgumentEnvelope(t *tes
{name: "missing dataset ids", body: `{"question":"hello"}`},
{name: "empty dataset ids", body: `{"question":"hello","dataset_ids":[]}`},
{name: "invalid top k", body: `{"question":"hello","dataset_ids":["ds-1"],"top_k":0}`},
+ {name: "legacy top k too large", body: `{"question":"hello","dataset_ids":["ds-1"],"top_k":2049}`},
+ {name: "knn top k too large", body: `{"question":"hello","dataset_ids":["ds-1"],"knn_top_k":2049}`},
+ {name: "invalid knn candidates", body: `{"question":"hello","dataset_ids":["ds-1"],"knn_num_candidates":0}`},
+ {name: "knn candidates below default top k", body: `{"question":"hello","dataset_ids":["ds-1"],"knn_num_candidates":10}`},
+ {name: "knn candidates below top k", body: `{"question":"hello","dataset_ids":["ds-1"],"knn_top_k":8,"knn_num_candidates":7}`},
{name: "invalid threshold", body: `{"question":"hello","dataset_ids":["ds-1"],"similarity_threshold":1.1}`},
}
@@ -219,6 +230,19 @@ func TestDatasetsHandlerSearchDatasetsValidationErrorsUseArgumentEnvelope(t *tes
}
}
+func TestValidateSearchParamsKNNBounds(t *testing.T) {
+ knnTopK := 2048
+ if err := validateSearchParams(nil, nil, &knnTopK, nil, nil, nil, nil); err != nil {
+ t.Fatalf("knn_top_k=2048 with default knn_num_candidates should be valid: %v", err)
+ }
+
+ legacyTopK := 2049
+ err := validateSearchParams(nil, nil, nil, &legacyTopK, nil, nil, nil)
+ if err == nil || err.Error() != "top_k (alias for knn_top_k) must be between 1 and 2048" {
+ t.Fatalf("legacy top_k error = %v", err)
+ }
+}
+
func TestDatasetsHandlerSearchDatasetsPropagatesServiceError(t *testing.T) {
gin.SetMode(gin.TestMode)
fake := &fakeSearchDatasetsService{err: errors.New("boom")}
diff --git a/internal/handler/dify_retrieval_handler.go b/internal/handler/dify_retrieval_handler.go
index d022303987..e67919d51b 100644
--- a/internal/handler/dify_retrieval_handler.go
+++ b/internal/handler/dify_retrieval_handler.go
@@ -268,7 +268,7 @@ func (h *DifyRetrievalHandler) Retrieval(c *gin.Context) {
DocIDs: docIDs,
Page: 1,
PageSize: pageSize,
- Top: topK,
+ KNNTopK: topK,
SimilarityThreshold: scoreThreshold,
EmbeddingModel: embModel,
}
diff --git a/internal/service/chat_pipeline.go b/internal/service/chat_pipeline.go
index cdcb7810b3..2dde6f6cb9 100644
--- a/internal/service/chat_pipeline.go
+++ b/internal/service/chat_pipeline.go
@@ -708,7 +708,7 @@ func (s *ChatPipelineService) AsyncChat(
Page: 1,
PageSize: topN,
RerankCandidatesCount: &rerankCandidatesCount,
- Top: &top,
+ KNNTopK: &top,
SimilarityThreshold: &threshold,
VectorSimilarityWeight: &vsw,
RankFeature: &rankFeature,
diff --git a/internal/service/chunk/chunk.go b/internal/service/chunk/chunk.go
index dfb94d48ae..00982dc7f4 100644
--- a/internal/service/chunk/chunk.go
+++ b/internal/service/chunk/chunk.go
@@ -418,7 +418,7 @@ func (s *ChunkService) RetrievalTest(ctx context.Context, req *service.Retrieval
Page: common.CoalesceInt(req.Page, 1),
PageSize: common.CoalesceInt(req.Size, 30),
RerankCandidatesCount: &rerankCandidatesCount,
- Top: req.TopK,
+ KNNTopK: req.TopK,
SimilarityThreshold: req.SimilarityThreshold,
VectorSimilarityWeight: req.VectorSimilarityWeight,
RerankModel: rerankModel,
diff --git a/internal/service/dataset/search.go b/internal/service/dataset/search.go
index b77016cc18..1c0f4a79e2 100644
--- a/internal/service/dataset/search.go
+++ b/internal/service/dataset/search.go
@@ -54,14 +54,20 @@ func (d *DatasetService) SearchDatasets(ctx context.Context, req *service.Search
if req.VectorSimilarityWeight != nil {
vectorSimilarityWeight = *req.VectorSimilarityWeight
}
- topK := 1024
- if req.TopK != nil {
- topK = *req.TopK
+ knnTopK := 1024
+ if req.KNNTopK != nil {
+ knnTopK = *req.KNNTopK
+ } else if req.TopK != nil {
+ knnTopK = *req.TopK
}
- if topK < 1 {
- topK = 1
- } else if topK > 2048 {
- topK = 2048
+ if knnTopK < 1 {
+ knnTopK = 1
+ } else if knnTopK > 2048 {
+ knnTopK = 2048
+ }
+ knnNumCandidates := 2048
+ if req.KNNNumCandidates != nil {
+ knnNumCandidates = *req.KNNNumCandidates
}
keyword := false
if req.Keyword != nil {
@@ -142,11 +148,11 @@ func (d *DatasetService) SearchDatasets(ctx context.Context, req *service.Search
vectorSimilarityWeight = scVSW
}
if scTopK, ok := searchConfig["top_k"].(float64); ok {
- topK = int(scTopK)
- if topK < 1 {
- topK = 1
- } else if topK > 2048 {
- topK = 2048
+ knnTopK = int(scTopK)
+ if knnTopK < 1 {
+ knnTopK = 1
+ } else if knnTopK > 2048 {
+ knnTopK = 2048
}
}
if scRerankCandidatesCount, ok := common.GetInt(searchConfig["rerank_candidates_count"]); ok {
@@ -175,6 +181,7 @@ func (d *DatasetService) SearchDatasets(ctx context.Context, req *service.Search
return nil, fmt.Errorf("invalid search_id")
}
}
+ knnNumCandidates = max(knnNumCandidates, knnTopK)
// If meta_data_filter method is auto/semi_auto, get chat model
var chatModelForFilter *modelModule.ChatModel
@@ -272,7 +279,8 @@ func (d *DatasetService) SearchDatasets(ctx context.Context, req *service.Search
Page: page,
PageSize: pageSize,
RerankCandidatesCount: &rerankCandidatesCount,
- Top: &topK,
+ KNNTopK: &knnTopK,
+ KNNNumCandidates: &knnNumCandidates,
SimilarityThreshold: &similarityThreshold,
VectorSimilarityWeight: &vectorSimilarityWeight,
RerankModel: rerankModel,
diff --git a/internal/service/dataset/search_test.go b/internal/service/dataset/search_test.go
index b898964d6a..b6057b1763 100644
--- a/internal/service/dataset/search_test.go
+++ b/internal/service/dataset/search_test.go
@@ -9,7 +9,8 @@ import (
func TestSearchDatasetRequestToSearchDatasetsRequest(t *testing.T) {
page := 2
size := 15
- topK := 128
+ knnTopK := 128
+ knnNumCandidates := 256
useKG := true
keyword := true
similarityThreshold := 0.42
@@ -23,7 +24,8 @@ func TestSearchDatasetRequestToSearchDatasetsRequest(t *testing.T) {
Size: &size,
DocIDs: []string{"doc-1", "doc-2"},
UseKG: &useKG,
- TopK: &topK,
+ KNNTopK: &knnTopK,
+ KNNNumCandidates: &knnNumCandidates,
CrossLanguages: []string{"en", "zh"},
SearchID: &searchID,
MetadataFilter: map[string]interface{}{"method": "manual"},
@@ -44,7 +46,7 @@ func TestSearchDatasetRequestToSearchDatasetsRequest(t *testing.T) {
if len(converted.DocIDs) != 2 || converted.DocIDs[0] != "doc-1" || converted.DocIDs[1] != "doc-2" {
t.Fatalf("doc_ids=%v want [doc-1 doc-2]", converted.DocIDs)
}
- if converted.UseKG != req.UseKG || converted.TopK != req.TopK || converted.SearchID != req.SearchID {
+ if converted.UseKG != req.UseKG || converted.KNNTopK != req.KNNTopK || converted.KNNNumCandidates != req.KNNNumCandidates || converted.SearchID != req.SearchID {
t.Fatalf("converted request did not preserve optional fields: %#v", converted)
}
if converted.MetadataFilter["method"] != "manual" || converted.RerankID != req.RerankID || converted.Keyword != req.Keyword {
diff --git a/internal/service/dataset_types.go b/internal/service/dataset_types.go
index 4ef069c624..45833eefa9 100644
--- a/internal/service/dataset_types.go
+++ b/internal/service/dataset_types.go
@@ -44,7 +44,9 @@ type SearchDatasetsRequest struct {
RerankCandidatesCount *int `json:"rerank_candidates_count,omitempty"`
DocIDs []string `json:"doc_ids,omitempty"`
UseKG *bool `json:"use_kg,omitempty"`
- TopK *int `json:"top_k,omitempty"`
+ KNNTopK *int `json:"knn_top_k,omitempty"`
+ TopK *int `json:"top_k,omitempty"` // Legacy alias for knn_top_k.
+ KNNNumCandidates *int `json:"knn_num_candidates,omitempty"`
CrossLanguages []string `json:"cross_languages,omitempty"`
SearchID *string `json:"search_id,omitempty"`
MetadataFilter map[string]interface{} `json:"meta_data_filter,omitempty"`
@@ -72,7 +74,9 @@ type SearchDatasetRequest struct {
RerankCandidatesCount *int `json:"rerank_candidates_count,omitempty"`
DocIDs []string `json:"doc_ids,omitempty"`
UseKG *bool `json:"use_kg,omitempty"`
- TopK *int `json:"top_k,omitempty"`
+ KNNTopK *int `json:"knn_top_k,omitempty"`
+ TopK *int `json:"top_k,omitempty"` // Legacy alias for knn_top_k.
+ KNNNumCandidates *int `json:"knn_num_candidates,omitempty"`
CrossLanguages []string `json:"cross_languages,omitempty"`
SearchID *string `json:"search_id,omitempty"`
MetadataFilter map[string]interface{} `json:"meta_data_filter,omitempty"`
@@ -96,7 +100,9 @@ func (req *SearchDatasetRequest) ToSearchDatasetsRequest(datasetID string) *Sear
RerankCandidatesCount: req.RerankCandidatesCount,
DocIDs: req.DocIDs,
UseKG: req.UseKG,
+ KNNTopK: req.KNNTopK,
TopK: req.TopK,
+ KNNNumCandidates: req.KNNNumCandidates,
CrossLanguages: req.CrossLanguages,
SearchID: req.SearchID,
MetadataFilter: req.MetadataFilter,
diff --git a/internal/service/nlp/retrieval.go b/internal/service/nlp/retrieval.go
index 613ed83c19..77fd476647 100644
--- a/internal/service/nlp/retrieval.go
+++ b/internal/service/nlp/retrieval.go
@@ -54,7 +54,8 @@ type RetrievalRequest struct {
Page int
PageSize int
RerankCandidatesCount *int
- Top *int
+ KNNTopK *int
+ KNNNumCandidates *int
SimilarityThreshold *float64
VectorSimilarityWeight *float64
RankFeature *map[string]float64
@@ -88,8 +89,11 @@ func (s *RetrievalService) Retrieval(ctx context.Context, req *RetrievalRequest)
}
// Apply default values
- if req.Top == nil {
- req.Top = func() *int { v := 1024; return &v }()
+ if req.KNNTopK == nil {
+ req.KNNTopK = func() *int { v := 1024; return &v }()
+ }
+ if req.KNNNumCandidates == nil {
+ req.KNNNumCandidates = func() *int { v := 2048; return &v }()
}
if req.SimilarityThreshold == nil {
req.SimilarityThreshold = func() *float64 { v := 0.2; return &v }()
@@ -132,7 +136,8 @@ func (s *RetrievalService) Retrieval(ctx context.Context, req *RetrievalRequest)
DocIDs: req.DocIDs,
Page: 1,
PageSize: rerankCandidatesCount,
- Top: *req.Top,
+ KNNTopK: *req.KNNTopK,
+ KNNNumCandidates: *req.KNNNumCandidates,
RankFeature: *req.RankFeature,
EmbeddingModel: req.EmbeddingModel,
VectorSimilarityWeight: req.VectorSimilarityWeight,
@@ -493,7 +498,8 @@ type RetrievalSearchRequest struct {
TenantIDs []string
KbIDs []string
DocIDs []string
- Top int
+ KNNTopK int
+ KNNNumCandidates int
Page int
PageSize int
Sort bool
@@ -561,13 +567,18 @@ func (s *RetrievalService) Search(ctx context.Context, req *RetrievalSearchReque
filters["available_int"] = 1
}
pg := max(req.Page-1, 0)
- topk := req.Top
- if topk <= 0 {
- topk = 1024
+ knnTopK := req.KNNTopK
+ if knnTopK <= 0 {
+ knnTopK = 1024
+ }
+ numCandidates := req.KNNNumCandidates
+ if numCandidates <= 0 {
+ numCandidates = 2048
}
+ // Result pagination is independent of the KNN candidate pool size.
pageSize := req.PageSize
if pageSize <= 0 {
- pageSize = topk
+ pageSize = 30
}
limit := pageSize
@@ -637,13 +648,13 @@ func (s *RetrievalService) Search(ctx context.Context, req *RetrievalSearchReque
if similarityForGetVector <= 0 {
similarityForGetVector = 0.1
}
- matchDense, err := s.GetVector(ctx, req.Question, req.EmbeddingModel, topk, similarityForGetVector)
+ matchDense, err := s.GetVector(ctx, req.Question, req.EmbeddingModel, knnTopK, numCandidates, similarityForGetVector)
if err != nil {
return nil, fmt.Errorf("GetVector failed: %w", err)
}
// Execute search with fusion
- fusionExpr := buildRetrievalFusionExpr(s.docEngine.GetType(), topk, req.VectorSimilarityWeight)
+ fusionExpr := buildRetrievalFusionExpr(s.docEngine.GetType(), knnTopK, req.VectorSimilarityWeight)
// Build source with vector column for ES
searchSrc := make([]string, len(searchRequest.SelectFields))
@@ -762,7 +773,7 @@ func (s *RetrievalService) Search(ctx context.Context, req *RetrievalSearchReque
}
// GetVector computes query vector and returns MatchDenseExpr for hybrid search
-func (s *RetrievalService) GetVector(ctx context.Context, txt string, embModel *models.EmbeddingModel, topk int, similarity float64) (*types.MatchDenseExpr, error) {
+func (s *RetrievalService) GetVector(ctx context.Context, txt string, embModel *models.EmbeddingModel, knnTopK, numCandidates int, similarity float64) (*types.MatchDenseExpr, error) {
embeddingConfig := &models.EmbeddingConfig{
Dimension: 0,
}
@@ -780,8 +791,8 @@ func (s *RetrievalService) GetVector(ctx context.Context, txt string, embModel *
EmbeddingData: vector,
EmbeddingDataType: "float",
DistanceType: "cosine",
- TopN: topk,
- ExtraOptions: map[string]interface{}{"similarity": similarity},
+ TopN: knnTopK,
+ ExtraOptions: map[string]interface{}{"similarity": similarity, "num_candidates": numCandidates},
}, nil
}
diff --git a/internal/service/nlp/retrieval_test.go b/internal/service/nlp/retrieval_test.go
index 9cb3df9fbb..07e0bbb930 100644
--- a/internal/service/nlp/retrieval_test.go
+++ b/internal/service/nlp/retrieval_test.go
@@ -41,7 +41,7 @@ func TestRetrievalUsesRerankCandidatesCountAsCandidateSet(t *testing.T) {
TenantIDs: []string{"tenant-1"},
Page: 1,
PageSize: 10,
- Top: &top,
+ KNNTopK: &top,
SimilarityThreshold: &threshold,
VectorSimilarityWeight: &vectorWeight,
Aggs: &aggs,
@@ -267,7 +267,7 @@ func TestSearchPassesVectorSimilarityWeightToFusionExpr(t *testing.T) {
docEngine := &captureSearchDocEngine{engineType: string(engine.EngineInfinity)}
service := NewRetrievalService(docEngine, nil)
_, err := service.Search(context.Background(), &RetrievalSearchRequest{
- Question: "test question", TenantIDs: []string{"tenant-1"}, KbIDs: []string{"kb-1"}, Page: 1, PageSize: 10, Top: 10,
+ Question: "test question", TenantIDs: []string{"tenant-1"}, KbIDs: []string{"kb-1"}, Page: 1, PageSize: 10, KNNTopK: 10,
RankFeature: map[string]float64{}, EmbeddingModel: &modelModule.EmbeddingModel{ModelDriver: &captureEmbeddingDriver{}}, VectorSimilarityWeight: &vectorWeight,
})
if err != nil {
@@ -288,7 +288,7 @@ func TestRetrievalPassesVectorSimilarityWeightToSearch(t *testing.T) {
}
service := NewRetrievalService(docEngine, &dao.DocumentDAO{})
_, err := service.Retrieval(context.Background(), &RetrievalRequest{
- Question: "test question", TenantIDs: []string{"tenant-1"}, KbIDs: []string{"kb-1"}, Page: 1, PageSize: 10, Top: &top,
+ Question: "test question", TenantIDs: []string{"tenant-1"}, KbIDs: []string{"kb-1"}, Page: 1, PageSize: 10, KNNTopK: &top,
RankFeature: &map[string]float64{}, EmbeddingModel: &modelModule.EmbeddingModel{ModelDriver: &captureEmbeddingDriver{}}, VectorSimilarityWeight: &vectorWeight,
})
if err != nil {
@@ -334,7 +334,7 @@ func TestSearchKeepsLegacyFusionWeightForElasticsearch(t *testing.T) {
KbIDs: []string{"kb-1"},
Page: 1,
PageSize: 10,
- Top: 10,
+ KNNTopK: 10,
RankFeature: map[string]float64{},
EmbeddingModel: &modelModule.EmbeddingModel{ModelDriver: &captureEmbeddingDriver{}},
VectorSimilarityWeight: &vectorWeight,