From 808b9862c94b2ef13ddef3d2823bba072fc54882 Mon Sep 17 00:00:00 2001 From: xiaowei Date: Wed, 14 Oct 2026 13:45:50 +0800 Subject: [PATCH] =?UTF-8?q?feat(fts):=20t=5F2afae95e=20=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=20/api/v1/fts/query=20=E5=8F=96=E8=AF=81=E7=AB=AF=E7=82=B9?= =?UTF-8?q?=EF=BC=88AC1=20=E5=88=A4=E6=8D=AE=E5=91=BD=E4=BB=A4=EF=BC=8C?= =?UTF-8?q?=E8=A7=82=E6=B5=8B=E8=AF=8D=E9=9D=A2=20topN=20=E5=91=BD?= =?UTF-8?q?=E4=B8=AD=E6=8E=92=E5=BA=8F=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go/internal/api/server.go | 16 +++++++++++ go/internal/storage/fts_index.go | 47 ++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/go/internal/api/server.go b/go/internal/api/server.go index 87f3ae2..181d1bb 100644 --- a/go/internal/api/server.go +++ b/go/internal/api/server.go @@ -497,6 +497,22 @@ func NewServer() http.Handler { storage.FtsRebuildNow() respondJSON(w, 200, map[string]interface{}{"status": "rebuild_started"}) }) + // 2026-10-14 (t_2afae95e):FTS 词面查询取证端点(AC1 判据命令): + // 返回该 query 的词面 topN 命中(id + 分 + 内容摘录),不走路过 recall 缓存。 + mux.HandleFunc("/api/v1/fts/query", func(w http.ResponseWriter, r *http.Request) { + var req struct { + Query string `json:"query"` + TopN int `json:"top_n"` + } + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + http.Error(w, "invalid body", http.StatusBadRequest) + return + } + if req.TopN <= 0 { + req.TopN = 10 + } + respondJSON(w, 200, storage.FtsQuery(req.Query, req.TopN)) + }) // ─── Prometheus /metrics ───────────────────── mux.Handle("/metrics", metrics.Handler()) diff --git a/go/internal/storage/fts_index.go b/go/internal/storage/fts_index.go index 3666711..c4c25d0 100644 --- a/go/internal/storage/fts_index.go +++ b/go/internal/storage/fts_index.go @@ -36,6 +36,7 @@ import ( "math" "os" "strings" + "sort" "sync" "time" @@ -306,6 +307,52 @@ func FtsRecord(id string) (models.MemoryRecord, bool) { return rec, ok } +// FtsQuery 词面查询取证(AC1 判据命令):返回 query 的词面 topN 命中 +// (id + 分 + 内容摘录 + 是否含该 query 的 ASCII 词),观测端点用,不走 recall。 +func FtsQuery(query string, topN int) map[string]interface{} { + scores := ftsInstance.FtsSearch(query, topN) + type hit struct { + ID string `json:"id"` + Score float64 `json:"score"` + Content string `json:"content"` + Category string `json:"category"` + Namespace string `json:"namespace"` + } + ascii, _ := tokenizeQueryV2(query) + hits := make([]hit, 0, len(scores)) + ftsInstance.mu.RLock() + for id, s := range scores { + rec, ok := ftsInstance.records[id] + if !ok { + continue + } + content := rec.Content + if len(content) > 120 { + content = content[:120] + "…" + } + hits = append(hits, hit{ID: rec.ID, Score: s, Content: content, Category: rec.Category, Namespace: rec.Namespace}) + } + ftsInstance.mu.RUnlock() + sort.Slice(hits, func(i, j int) bool { + if hits[i].Score != hits[j].Score { + return hits[i].Score > hits[j].Score + } + return hits[i].ID < hits[j].ID + }) + ftsInstance.mu.RLock() + nDocs := ftsInstance.nDocs + nTokens := len(ftsInstance.postings) + ftsInstance.mu.RUnlock() + return map[string]interface{}{ + "query": query, + "ascii_terms": ascii, + "n_hits": len(hits), + "hits": hits, + "index_docs": nDocs, + "index_tokens": nTokens, + } +} + // FtsInvalidate 写路径钩子:commit 落库后调(在 server 的 SearchCache Invalidate 同处), // 标脏让下次查询惰性重建。关不关都零成本(一次原子写)。 func FtsInvalidate() {