feat: relevance-ordered search, suggest rows and journal:reindex
- Engine search asks Typesense for the first 250 candidates, re-gates them in SQL, counts the gated rows and pages over them in rank order; pages after the first are no longer empty. An explicit sort overrides the rank. - The settings row gates engine search, so it works straight after boot. - ?fields=suggest serves id, title, slug, a plain-text excerpt, featured_image_url and published_at. - Content is indexed as plain text; Post declares SearchIndexSchema. - journal:reindex [--fresh] syncs every post into the index.
This commit is contained in:
59
models/post_search_test.go
Normal file
59
models/post_search_test.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestSearchDocumentIsPlainText(t *testing.T) {
|
||||
past := time.Now().Add(-time.Hour)
|
||||
p := &Post{
|
||||
ID: 7,
|
||||
Title: "Kurki",
|
||||
Excerpt: "Zajawka",
|
||||
Content: "## Kurki",
|
||||
ContentHTML: `<h2 class="x">Kurki</h2><p>rosną w <a href="/las">lesie</a> & pachną</p>`,
|
||||
Published: true,
|
||||
PublishedAt: &past,
|
||||
}
|
||||
doc, err := p.ToSearchableArray(t.Context(), nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
content, _ := doc["content"].(string)
|
||||
if strings.ContainsAny(content, "<>") || strings.Contains(content, "href") || strings.Contains(content, "&") {
|
||||
t.Fatalf("content keeps markup: %q", content)
|
||||
}
|
||||
if content != "Kurki rosną w lesie & pachną" {
|
||||
t.Fatalf("content = %q", content)
|
||||
}
|
||||
|
||||
p.ContentHTML = ""
|
||||
doc, _ = p.ToSearchableArray(t.Context(), nil)
|
||||
if doc["content"] != "## Kurki" {
|
||||
t.Fatalf("content without content_html = %q", doc["content"])
|
||||
}
|
||||
}
|
||||
|
||||
// TestSearchSchemaMatchesDocument: the schema declares exactly the keys
|
||||
// ToSearchableArray emits.
|
||||
func TestSearchSchemaMatchesDocument(t *testing.T) {
|
||||
doc, err := (&Post{ID: 1}).ToSearchableArray(t.Context(), nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var docKeys, schemaKeys []string
|
||||
for k := range doc {
|
||||
docKeys = append(docKeys, k)
|
||||
}
|
||||
for _, f := range (Post{}).SearchIndexSchema()["fields"].([]map[string]any) {
|
||||
schemaKeys = append(schemaKeys, f["name"].(string))
|
||||
}
|
||||
sort.Strings(docKeys)
|
||||
sort.Strings(schemaKeys)
|
||||
if strings.Join(docKeys, ",") != strings.Join(schemaKeys, ",") {
|
||||
t.Fatalf("document keys %v, schema fields %v", docKeys, schemaKeys)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user