search

package
v0.10.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 19, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package search provides full-text search using bleve for ranked results.

Index Management

The Index type wraps a bleve index with markdown-post-aware document mapping. Indexes are persisted to disk and rebuilt only when content changes (detected via content hash).

Usage

idx, err := search.Open(dir)
if err != nil {
    idx, err = search.Build(dir, posts)
}
results, err := idx.Search("golang", search.QueryOptions{Limit: 10})

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContentHash

func ContentHash(posts []*models.Post) string

ContentHash computes a hash of all post paths and content for cache invalidation.

func DefaultDir

func DefaultDir(cacheDir string) string

DefaultDir returns the default index directory inside the cache dir.

func NamedDir

func NamedDir(cacheDir, name string) string

NamedDir returns an index directory with a process-specific suffix. Use this when multiple processes may share the same cache directory (e.g., serve and search-server running simultaneously).

func NamedHashFile

func NamedHashFile(cacheDir, name string) string

NamedHashFile returns a hash file path with a process-specific suffix.

func PostsByPath

func PostsByPath(posts []*models.Post) map[string]*models.Post

PostsByPath builds a lookup map from posts for result resolution.

Types

type Document

type Document struct {
	Title       string    `json:"title"`
	Content     string    `json:"content,omitempty"`
	Description string    `json:"description,omitempty"`
	Tags        []string  `json:"tags,omitempty"`
	Path        string    `json:"path"`
	Slug        string    `json:"slug,omitempty"`
	Href        string    `json:"href,omitempty"`
	Date        time.Time `json:"date,omitempty"`
	Published   bool      `json:"published"`
	Private     bool      `json:"private,omitempty"`
	WordCount   int       `json:"word_count,omitempty"`
	MediaURL    string    `json:"media_url,omitempty"`
	MediaType   string    `json:"media_type,omitempty"`
	PosterURL   string    `json:"poster_url,omitempty"`
	VideoMIME   string    `json:"video_mime,omitempty"`
	DocJSON     string    `json:"doc_json,omitempty"`
}

Document is the stored bleve document used to answer search results safely. It contains only fields that are allowed to be exposed by search.

type Index

type Index struct {
	// contains filtered or unexported fields
}

Index wraps a bleve index with post-aware operations.

func Build

func Build(dir string, posts []*models.Post) (*Index, error)

Build creates a new bleve index from posts. If dir already exists, it is removed and rebuilt.

func BuildIfNeeded

func BuildIfNeeded(cacheDir string, posts []*models.Post) (*Index, error)

BuildIfNeeded creates or opens an index, rebuilding only if the content hash changed.

func BuildIfNeededAt

func BuildIfNeededAt(dir, hashPath string, posts []*models.Post) (*Index, error)

BuildIfNeededAt creates or opens an index at an explicit path, rebuilding only if the content hash changed.

func BuildIfNeededNamed

func BuildIfNeededNamed(cacheDir, name string, posts []*models.Post) (*Index, error)

BuildIfNeededNamed creates or opens a named index, allowing multiple processes to maintain separate indexes in the same cache directory.

func Open

func Open(dir string) (*Index, error)

Open opens an existing bleve index. Returns an error if no index exists.

func (*Index) Close

func (si *Index) Close() error

Close closes the index.

func (*Index) Search

func (si *Index) Search(query string, opts QueryOptions, postsByPath map[string]*models.Post) ([]Result, error)

Search performs a ranked full-text search.

type QueryOptions

type QueryOptions struct {
	Limit     int
	Fuzzy     bool
	Fuzziness int
	Fields    []string // empty = all fields
	DateFrom  *time.Time
	DateTo    *time.Time
	Tags      []string
	Published *bool
}

QueryOptions configures a search query.

type Result

type Result struct {
	Post  *models.Post
	Doc   Document
	Score float64
}

Result represents a single search hit with relevance score.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL