perf(wiki): subgraph API and interactive exploration for large graphs

Wikis with tens of thousands of pages used to crash the browser trying
to render the entire link graph at once (30MB+ JSON, 100k+ SVG elements).
This change moves the graph viewer from "fetch everything, render
everything" to "fetch a slice, expand on demand" with several matching
UX improvements.

Backend

* GET /wiki/graph now accepts `mode` (overview | ego), `center`, `depth`,
  `types`, `limit` query params. Default is overview top-500 by
  link_count, capped at 2000. Response includes a Meta object so the
  frontend can render a truncation hint and drive the UI.
* Pure helper `computeGraphSubset` extracted for testability; six unit
  tests cover overview truncation / type filter / ego BFS / missing
  center error.
* WikiLintService passes Limit=0 (uncapped) so link integrity checks
  still walk every page.
* Repo `Search` adds CASE-based relevance ranking (title 4, slug 3,
  summary 2, content 1) so full-text results put the most obvious
  matches first instead of whatever was updated most recently.

Frontend

* Graph viewer fetches overview on entry; ego pivot on double-click,
  search, wiki-link click, URL ?slug=, global issues jump.
* Shift+click or hover ⊕ button blooms neighbors onto the current
  canvas additively. Bloom tracks generations and LRU-evicts oldest
  when total exceeds 1500 nodes; ego center / selected node / latest
  anchor are always protected.
* "Grow frontier (N)" legend action expands every dashed-ring node in
  one click (6-way concurrency) while skipping Index/Log super-nodes
  that would otherwise dump the whole wiki onto the canvas.
* Node dashed expansion ring + drawer "X/Y neighbors shown" hint tell
  users which nodes still have neighbors to load.
* Search dropdown uses remote full-text search (debounced, sequence-
  numbered to drop stale responses) and falls back to the overview
  top-500 snapshot when the input is empty.
* Type filters now round-trip to the server so top-N is always
  computed from the user's active type set rather than hiding nodes
  client-side and shrinking the view.
* Status card replaces the cramped "centered on X · N hops · M nodes"
  line with a structured focus/overview summary, resolving slugs to
  page titles.
* Help popover in the legend lists every canvas shortcut.

i18n entries added for zh-CN / en-US / ko-KR / ru-RU.
This commit is contained in:
wizardchen
2026-05-08 13:29:19 +08:00
committed by lyingbug
parent fc5d16e331
commit df1e9a1bfd
13 changed files with 1962 additions and 137 deletions
+26 -1
View File
@@ -329,6 +329,19 @@ func escapeLikePattern(s string) string {
// Search performs case-insensitive POSIX regex search on wiki pages within a knowledge base.
// The query is interpreted as a PostgreSQL regular expression (via ~*).
//
// Results are ranked by where the query hit, highest-relevance first:
//
// title hit → rank 4 (most obvious intent: user typed what the page is called)
// slug hit → rank 3 (url-like identifiers, direct jump)
// summary hit → rank 2 (short authored abstract)
// content hit → rank 1 (body mention — often surfaces unrelated pages whose
// prose merely mentions the query as trivia)
//
// Without this ranking, a user searching for "王新" on a 4万-page wiki will
// see pages like "华为" or "Index" ahead of the actual 王新 page just
// because they mention 王新 in their body and were updated more recently.
// updated_at stays as the tiebreaker so same-rank ties stay deterministic.
func (r *wikiPageRepository) Search(ctx context.Context, kbID string, query string, limit int) ([]*types.WikiPage, error) {
if limit <= 0 {
limit = 10
@@ -337,12 +350,24 @@ func (r *wikiPageRepository) Search(ctx context.Context, kbID string, query stri
limit = 50
}
// CASE expression is evaluated per-row during SELECT; we order by the
// alias so the DB only computes the rank once. Parameterized four
// times with the same regex to avoid coupling to GORM's positional
// arg rewriting quirks.
rankExpr := "CASE " +
"WHEN title ~* ? THEN 4 " +
"WHEN slug ~* ? THEN 3 " +
"WHEN summary ~* ? THEN 2 " +
"WHEN content ~* ? THEN 1 " +
"ELSE 0 END AS match_rank"
var pages []*types.WikiPage
if err := r.db.WithContext(ctx).
Select("*, "+rankExpr, query, query, query, query).
Where("knowledge_base_id = ? AND (title ~* ? OR content ~* ? OR summary ~* ? OR slug ~* ?)",
kbID, query, query, query, query).
Where("status != ?", "archived").
Order("updated_at DESC").
Order("match_rank DESC, updated_at DESC").
Limit(limit).
Find(&pages).Error; err != nil {
return nil, err