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>
50 lines
1.5 KiB
Go
50 lines
1.5 KiB
Go
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package httpapi
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"docwell/server/internal/access"
|
|
"docwell/server/internal/directory"
|
|
)
|
|
|
|
// SearchService is the part of directory.Service that searches pages.
|
|
type SearchService interface {
|
|
Search(ctx context.Context, c access.Caller, wsID, text string, limit int) (directory.SearchResult, error)
|
|
}
|
|
|
|
// SearchIndex is the index maintenance the admin panel can trigger.
|
|
type SearchIndex interface {
|
|
Rebuild(ctx context.Context) error
|
|
Count() (uint64, error)
|
|
}
|
|
|
|
func (a *api) searchRoutes(mux *routeMux) {
|
|
mux.HandleFunc("GET /api/v1/workspaces/{id}/search", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
|
|
q := r.URL.Query()
|
|
limit, _ := strconv.Atoi(q.Get("limit"))
|
|
res, err := a.dir.Search(r.Context(), caller(r), r.PathValue("id"), q.Get("q"), min(max(limit, 0), 50))
|
|
a.respond(w, res, err)
|
|
}))
|
|
mux.HandleFunc("POST /api/v1/admin/search/reindex", a.requireManager(func(w http.ResponseWriter, r *http.Request) {
|
|
if err := access.ManageInstance(caller(r)); err != nil {
|
|
a.writeError(w, err)
|
|
return
|
|
}
|
|
if a.index == nil {
|
|
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "unavailable"})
|
|
return
|
|
}
|
|
// Rebuilding can take a while; it runs on after the response.
|
|
go func() {
|
|
if err := a.index.Rebuild(context.WithoutCancel(r.Context())); err != nil {
|
|
a.log.Error("search: rebuild failed", "err", err)
|
|
}
|
|
}()
|
|
w.WriteHeader(http.StatusAccepted)
|
|
}))
|
|
}
|