From a7fbbfd6f974631eadebc6effca8ce7982592786 Mon Sep 17 00:00:00 2001
From: ax_rd <ax_rd@aisim.cn>
Date: Thu, 03 Sep 2026 12:51:40 +0800
Subject: [PATCH] fix: explore 段落命中两级回退(原词→展开词)+ expandKeywords 去重 + HardBudget 接入

---
 internal/search/explore.go |   28 ++++++++++++++++++++++++----
 1 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/internal/search/explore.go b/internal/search/explore.go
index 7ff127e..9645fe8 100644
--- a/internal/search/explore.go
+++ b/internal/search/explore.go
@@ -94,13 +94,23 @@
 			}
 		}
 	}
-	return out
+	// 去重保序(原词在前、展开词在后):避免 bigram 与原词重复时 text 分重复计权
+	var dedup []string
+	seen := make(map[string]bool, len(out))
+	for _, kw := range out {
+		if !seen[kw] {
+			seen[kw] = true
+			dedup = append(dedup, kw)
+		}
+	}
+	return dedup
 }
 
 // ExploreOptions explore 参数
 type ExploreOptions struct {
-	Budget int // 字节预算(0 = 用配置默认)
-	TopN   int // 0 = 用配置默认
+	Budget     int // 字节预算(0 = 用配置默认)
+	TopN       int // 0 = 用配置默认
+	HardBudget int // 字节硬上限(0 = 用代码缺省 32000)
 }
 
 // ExploredDoc 入选文档及其输出正文
@@ -131,7 +141,11 @@
 		budget = 16000 // 代码缺省(config 读取在 cmd 层完成)
 	}
 	if budget > 32000 {
-		budget = 32000 // 硬上限
+		budget = 32000 // 硬上限缺省
+	}
+	// 配置显式给出的硬上限优先(cmd 层读 config.yaml 的 explore.hard_budget)
+	if cfg.HardBudget > 0 && budget > cfg.HardBudget {
+		budget = cfg.HardBudget
 	}
 	topN := cfg.TopN
 	if topN <= 0 {
@@ -158,8 +172,14 @@
 		if err != nil || content == "" {
 			continue
 		}
+		// 两级回退:先用原词提取(精确);原词在任何段落都不连续出现时
+		//(复合 CJK 词常见:bigram 召回了文档,但原词不连续)回退到展开词提取,
+		// 避免大文档被 continue 静默丢弃、最终输出「未找到相关文档」。
 		body := extractRelevantParagraphs(content, keywords, perDoc)
 		if body == "" {
+			body = extractRelevantParagraphs(content, searchKeywords, perDoc)
+		}
+		if body == "" {
 			// 无命中段落但文档入选 → 整篇(若放得下),否则跳过
 			if len(content) <= perDoc {
 				body = content

--
Gitblit v1.10.0