Search runs on every Ctrl+K keystroke that settles. It has no database and no Elasticsearch behind it: just a slice of SearchDoc values and a scorer that walks them.

The weights

FieldWeight
title50
tags30
summary20
body10

Title hits dominate on purpose. If you search norg and a post has "Norg" in its title, you almost certainly want that post, ahead of five others that mention the word once.

The scorer

Each field gets lowercased once per document, then every query token checks it.

Go
func scoreDoc(doc content.SearchDoc, tokens []string) int {
    title := strings.ToLower(doc.Title)
    summary := strings.ToLower(doc.Summary)
    body := strings.ToLower(doc.Body)
    tags := strings.ToLower(strings.Join(doc.Tags, " "))

    score := 0
    for _, token := range tokens {
        if strings.Contains(title, token) {
            score += 50
        }
        if strings.Contains(tags, token) {
            score += 30
        }
        if strings.Contains(summary, token) {
            score += 20
        }
        if strings.Contains(body, token) {
            score += 10
        }
    }
    return score
}

Ties go to the newer document.

Where it falls short

  • type filter prefix (blog ..., projects ...)
  • parent slug so devlog results link to /projects/{parent}/{slug}
  • query cap: anything over 256 bytes gets a 400 (added in July)
  • phrase matching, so bento grid counts as one phrase
  • stemming, so rebuild matches rebuilt
  • a cap on tag boosts, so a post tagged with the same word five times cannot run away

Phrase matching comes first. The indexer splits the query on whitespace and scores each token alone, so bento grid ranks anything that contains bento or grid, even when the two words never meet.

A scorer this small is easy to read and just as easy to get wrong. I check it against real queries before I trust the numbers.