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_test.go | 59 ++++++++++++++++++++++++++++-
cmd/explore.go | 3 +
internal/search/explore.go | 28 ++++++++++++--
3 files changed, 83 insertions(+), 7 deletions(-)
diff --git a/cmd/explore.go b/cmd/explore.go
index 84152d6..eb808c3 100644
--- a/cmd/explore.go
+++ b/cmd/explore.go
@@ -78,8 +78,9 @@
if topN <= 0 && cfg.Explore.TopN > 0 {
topN = cfg.Explore.TopN
}
+ hardBudget := cfg.Explore.HardBudget // 0 = Explore 内部用缺省 32000
- res, err := search.Explore(store, allKeywords, search.ExploreOptions{Budget: budget, TopN: topN})
+ res, err := search.Explore(store, allKeywords, search.ExploreOptions{Budget: budget, TopN: topN, HardBudget: hardBudget})
if err != nil {
return fmt.Errorf("explore 失败: %w", err)
}
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
diff --git a/internal/search/explore_test.go b/internal/search/explore_test.go
index 125fcac..1bba277 100644
--- a/internal/search/explore_test.go
+++ b/internal/search/explore_test.go
@@ -1,7 +1,12 @@
package search
import (
+ "path/filepath"
+ "strings"
"testing"
+
+ "github.com/aisim/kb-cli/internal/graph"
+ "github.com/aisim/kb-cli/internal/index"
)
// TestExploreParagraphExtraction 段落截取:只输出命中关键词的段落,整段不截半句
@@ -22,10 +27,10 @@
}
}
-// TestExploreKeywordExpansion 长 CJK 词拆 bigram(原词保留,ASCII/短词不拆)
+// TestExploreKeywordExpansion 长 CJK 词拆 bigram(原词保留,ASCII/短词不拆;末尾去重保序)
func TestExploreKeywordExpansion(t *testing.T) {
got := expandKeywords([]string{"电子秤补气失败", "补气", "abc"})
- want := []string{"电子秤补气失败", "电子", "子秤", "秤补", "补气", "气失", "失败", "补气", "abc"}
+ want := []string{"电子秤补气失败", "电子", "子秤", "秤补", "补气", "气失", "失败", "abc"}
if len(got) != len(want) {
t.Fatalf("展开数量: got=%d want=%d (%v)", len(got), len(want), got)
}
@@ -43,3 +48,53 @@
t.Errorf("超预算段落应跳过: %q", got)
}
}
+
+// TestExploreFallbackToExpanded 回归(审查 Important):单复合 CJK 词在大文档中
+// 原文不连续出现(bigram 召回入选,但原词在任何段落都不出现)时,
+// 段落截取必须回退到展开词,不能静默丢弃文档。
+func TestExploreFallbackToExpanded(t *testing.T) {
+ tmpDir := t.TempDir()
+ dbPath := filepath.Join(tmpDir, "test.db")
+ store, err := index.Open(dbPath)
+ if err != nil {
+ t.Fatalf("Open failed: %v", err)
+ }
+ defer store.Close()
+
+ // 大文档(> 2000 字节预算):某段落只含「电子秤补气」片段,不含完整原词「电子秤补气失败」
+ filler := strings.Repeat("填充段落内容,用于把文档撑过预算。\n", 60)
+ content := "# 称重故障排查\n\n" + filler + "\n## 称重故障\n\n电子秤补气 时先检查阀门,失败则断电重启。\n"
+
+ n := &graph.Node{
+ ID: 1,
+ Path: "FAQ/称重/001-test.md",
+ Title: "称重故障排查",
+ Section: "FAQ",
+ Content: content,
+ }
+ if _, err := store.InsertNode(n); err != nil {
+ t.Fatalf("InsertNode failed: %v", err)
+ }
+ if err := store.CreateFTS(); err != nil {
+ t.Fatalf("CreateFTS failed: %v", err)
+ }
+ if err := store.PopulateFTS(); err != nil {
+ t.Fatalf("PopulateFTS failed: %v", err)
+ }
+
+ // 原词提取必为空(内容里没有完整的「电子秤补气失败」)
+ if b := extractRelevantParagraphs(content, []string{"电子秤补气失败"}, 2000); b != "" {
+ t.Fatalf("前提不成立:原词提取应为空,got=%q", b)
+ }
+
+ res, err := Explore(store, []string{"电子秤补气失败"}, ExploreOptions{Budget: 2000, TopN: 5})
+ if err != nil {
+ t.Fatalf("Explore failed: %v", err)
+ }
+ if len(res.Docs) != 1 {
+ t.Fatalf("应召回 1 篇文档(bigram 命中),got=%d", len(res.Docs))
+ }
+ if !strings.Contains(res.Docs[0].Body, "电子秤补气") {
+ t.Errorf("回退展开词后应提取含片段段落: body=%q", res.Docs[0].Body)
+ }
+}
--
Gitblit v1.10.0