searchapi

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: 11 Imported by: 0

Documentation

Overview

Package searchapi provides a read-only HTTP API for bleve full-text search.

Privacy

Private posts are indexed with a narrow safe subset. Only explicit frontmatter title, explicit frontmatter description, and navigational fields are exposed. Body content, tags, media, and derived content fields are never exposed by the search API. Draft and skipped posts are excluded entirely.

Endpoints

GET /api/search?q=<query>&fuzzy=true&limit=20&tags=go,cli&from=2024-01-01&to=2024-12-31

Response:

{
  "query": "search term",
  "total": 5,
  "results": [
    {
      "title": "Post Title",
      "path": "posts/example.md",
      "slug": "example",
      "href": "/example",
      "description": "A short description",
      "date": "2024-01-15T00:00:00Z",
      "tags": ["go", "cli"],
      "score": 1.234,
      "word_count": 500,
      "read_time": "3 min"
    }
  ]
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	DefaultLimit int
	MaxLimit     int
	DefaultFuzzy bool
	CORSOrigins  []string
	IndexName    string // process-specific index name to avoid conflicts
	IndexDir     string
	HashPath     string
	ReadOnly     bool
	Rebuild      bool
}

Config controls search API behavior.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a Config with sensible defaults.

type Handler

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

Handler serves the bleve search API. The index is built lazily on first search and reused across requests. It is only rebuilt when the search-visible post data changes.

func NewHandler

func NewHandler(posts []*models.Post, cacheDir string, cfg Config) *Handler

NewHandler creates a new search API handler. The bleve index is built lazily on the first search request, not at construction time, to avoid blocking server startup.

func NewReadOnlyHandler

func NewReadOnlyHandler(indexDir string, cfg Config) *Handler

NewReadOnlyHandler creates a handler that serves from an existing bleve index without loading or rebuilding site content.

func (*Handler) Close

func (h *Handler) Close()

Close releases the underlying search index.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP handles GET requests for search.

func (*Handler) UpdatePosts

func (h *Handler) UpdatePosts(posts []*models.Post)

UpdatePosts replaces the post list (e.g., after a rebuild). Only rebuilds the index if the post set actually changed.

type SearchResponse

type SearchResponse struct {
	Query   string         `json:"query"`
	Total   int            `json:"total"`
	Fuzzy   bool           `json:"fuzzy"`
	Limit   int            `json:"limit"`
	Results []SearchResult `json:"results"`
}

SearchResponse is the JSON response envelope.

type SearchResult

type SearchResult struct {
	Title       string   `json:"title"`
	Path        string   `json:"path"`
	Slug        string   `json:"slug"`
	Href        string   `json:"href"`
	MediaURL    string   `json:"media_url,omitempty"`
	MediaType   string   `json:"media_type,omitempty"`
	PosterURL   string   `json:"poster_url,omitempty"`
	VideoMIME   string   `json:"video_mime,omitempty"`
	Description string   `json:"description,omitempty"`
	Date        string   `json:"date,omitempty"`
	Tags        []string `json:"tags,omitempty"`
	Score       float64  `json:"score"`
	WordCount   int      `json:"word_count,omitempty"`
	ReadTime    string   `json:"read_time,omitempty"`
	Private     bool     `json:"private,omitempty"`
}

SearchResult is a single search hit in the API response.

Jump to

Keyboard shortcuts

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