Files
Docwell/Server/internal/directory/search.go
T
tajniak81andClaude Opus 5.5 25e7ca19a5 Add full-text search with Bleve and the Ctrl+K palette
docwell-server keeps a Bleve index of page titles and text in
DOCWELL_DATA_DIR. Instead of hooking every write, an indexer polls
PocketBase for pages whose "updated" moved (titles, saved content, trash
and restore all touch it) and sweeps out purged pages every ten minutes.
The index can be thrown away at any time: it rebuilds from the database,
and superadmins can trigger a rebuild.

Searches are limited to the spaces the caller can open, match the last
word as a prefix and forgive one wrong letter. Snippets come back as
plain text with match offsets, so clients never render server HTML.

In the web app, Ctrl+K or the sidebar's search box opens a palette that
shows recent pages until something is typed.

The server containers get a /data volume for the index.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 16:57:55 +01:00

58 lines
1.4 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package directory
import (
"context"
"docwell/server/internal/access"
"docwell/server/internal/search"
)
// Searcher is the part of search.Index this package uses.
type Searcher interface {
Query(ctx context.Context, workspace string, spaces []string, text string, limit int) ([]search.Hit, int, error)
}
// UseSearch enables Search.
func (s *Service) UseSearch(x Searcher) { s.search = x }
type SearchHit struct {
search.Hit
SpaceName string `json:"spaceName"`
}
type SearchResult struct {
Items []SearchHit `json:"items"`
Total int `json:"total"`
}
// Search finds pages in a workspace, only in the spaces c can open.
func (s *Service) Search(ctx context.Context, c access.Caller, wsID, text string, limit int) (SearchResult, error) {
spaces, err := s.ListSpaces(ctx, c, wsID)
if err != nil {
return SearchResult{}, err
}
out := SearchResult{Items: []SearchHit{}}
if s.search == nil || len(spaces) == 0 {
return out, nil
}
ids := make([]string, len(spaces))
names := make(map[string]string, len(spaces))
for i, sp := range spaces {
ids[i], names[sp.ID] = sp.ID, sp.Name
}
if limit <= 0 {
limit = 20
}
hits, total, err := s.search.Query(ctx, wsID, ids, text, limit)
if err != nil {
return SearchResult{}, err
}
for _, h := range hits {
out.Items = append(out.Items, SearchHit{Hit: h, SpaceName: names[h.Space]})
}
out.Total = total
return out, nil
}