feat(fts): t_2afae95e 新增 /api/v1/fts/query 取证端点(AC1 判据命令,观测词面 topN 命中排序)

This commit is contained in:
xiaowei
2026-10-14 13:45:50 +08:00
parent b69cc01d52
commit 808b9862c9
2 changed files with 63 additions and 0 deletions
+16
View File
@@ -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())
+47
View File
@@ -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() {