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>
89 lines
2.5 KiB
Go
89 lines
2.5 KiB
Go
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package search
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"log/slog"
|
|
"testing"
|
|
)
|
|
|
|
func newIndex(t *testing.T) *Index {
|
|
t.Helper()
|
|
x, err := OpenMemory(nil, slog.New(slog.NewTextHandler(io.Discard, nil)))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = x.Close() })
|
|
return x
|
|
}
|
|
|
|
func TestQueryScopesAndRanks(t *testing.T) {
|
|
x := newIndex(t)
|
|
put := func(id, ws, space, title, text string) {
|
|
if err := x.Put(id, doc{Workspace: ws, Space: space, Title: title, Text: text}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
put("p1", "w1", "s1", "Deployment guide", "How we ship the server to production.")
|
|
put("p2", "w1", "s1", "Meeting notes", "We talked about the deployment window.")
|
|
put("p3", "w1", "s2", "Deployment secrets", "Private space.")
|
|
put("p4", "w2", "s9", "Deployment elsewhere", "Another workspace.")
|
|
|
|
ctx := context.Background()
|
|
hits, total, err := x.Query(ctx, "w1", []string{"s1"}, "deployment", 10)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if total != 2 || len(hits) != 2 {
|
|
t.Fatalf("hits = %+v (total %d)", hits, total)
|
|
}
|
|
if hits[0].ID != "p1" {
|
|
t.Errorf("a title match must rank first: %+v", hits)
|
|
}
|
|
for _, h := range hits {
|
|
if h.ID == "p3" || h.ID == "p4" {
|
|
t.Errorf("%s is outside the caller's spaces", h.ID)
|
|
}
|
|
}
|
|
|
|
// Typing: the last word matches as a prefix.
|
|
if hits, _, _ := x.Query(ctx, "w1", []string{"s1"}, "meet", 10); len(hits) != 1 || hits[0].ID != "p2" {
|
|
t.Errorf("prefix = %+v", hits)
|
|
}
|
|
// One wrong letter is forgiven.
|
|
if hits, _, _ := x.Query(ctx, "w1", []string{"s1"}, "deploymant", 10); len(hits) == 0 {
|
|
t.Error("fuzzy match failed")
|
|
}
|
|
if hits, _, _ := x.Query(ctx, "w1", nil, "deployment", 10); len(hits) != 0 {
|
|
t.Error("no visible spaces must find nothing")
|
|
}
|
|
|
|
if err := x.Remove("p1"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if hits, _, _ := x.Query(ctx, "w1", []string{"s1"}, "guide", 10); len(hits) != 0 {
|
|
t.Errorf("removed page still found: %+v", hits)
|
|
}
|
|
}
|
|
|
|
func TestSnippet(t *testing.T) {
|
|
text := "Zażółć gęślą jaźń. The quick brown fox jumps over the lazy dog."
|
|
start := len("Zażółć gęślą jaźń. The quick ")
|
|
snippet, marks := Snippet(text, [][2]int{{start, start + len("brown")}}, 30)
|
|
if len(marks) != 1 {
|
|
t.Fatalf("marks = %v (%q)", marks, snippet)
|
|
}
|
|
runes := []rune(snippet)
|
|
if got := string(runes[marks[0][0]:marks[0][1]]); got != "brown" {
|
|
t.Errorf("mark covers %q in %q", got, snippet)
|
|
}
|
|
if s, m := Snippet("", nil, 10); s != "" || len(m) != 0 {
|
|
t.Error("empty text")
|
|
}
|
|
if s, _ := Snippet("short", nil, 100); s != "short" {
|
|
t.Errorf("short = %q", s)
|
|
}
|
|
}
|