search

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package search provides full-text search capabilities similar to Laravel Scout. It supports multiple drivers: PostgreSQL full-text search (built-in), Meilisearch, Typesense, and Algolia.

Models implement the Searchable interface to enable indexing and searching.

Quick start

engine := search.NewPostgresEngine(db)
results, _ := engine.Search(ctx, "videos", "gopro 4k sunset", nil)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeleteRecord

func DeleteRecord(ctx context.Context, record Searchable) error

DeleteRecord removes a Searchable model from the default engine.

func IndexRecord

func IndexRecord(ctx context.Context, record Searchable) error

IndexRecord indexes a Searchable model using the default engine.

func Register

func Register(name string, engine Engine)

Register adds a named engine.

Types

type Engine

type Engine interface {
	// Index adds or updates a document in the search index.
	Index(ctx context.Context, index string, id string, data map[string]any) error
	// Delete removes a document from the search index.
	Delete(ctx context.Context, index string, id string) error
	// Search performs a full-text search query.
	Search(ctx context.Context, index string, query string, opts *Options) (*Results, error)
	// Flush removes all documents from an index.
	Flush(ctx context.Context, index string) error
}

Engine is the interface that all search drivers implement.

func Default

func Default() Engine

Default returns the default engine.

func Use

func Use(name string) (Engine, error)

Use returns the named engine.

type MeilisearchConfig

type MeilisearchConfig struct {
	Host   string // e.g. "http://localhost:7700"
	APIKey string // Master or search API key
}

MeilisearchConfig holds Meilisearch connection settings.

type MeilisearchEngine

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

MeilisearchEngine uses Meilisearch as the search backend.

func NewMeilisearchEngine

func NewMeilisearchEngine(cfg MeilisearchConfig) *MeilisearchEngine

NewMeilisearchEngine creates a Meilisearch-backed search engine.

func (*MeilisearchEngine) Delete

func (e *MeilisearchEngine) Delete(ctx context.Context, index string, id string) error

Delete removes a document from Meilisearch.

func (*MeilisearchEngine) Flush

func (e *MeilisearchEngine) Flush(ctx context.Context, index string) error

Flush removes all documents from an index in Meilisearch.

func (*MeilisearchEngine) Index

func (e *MeilisearchEngine) Index(ctx context.Context, index string, id string, data map[string]any) error

Index adds or updates a document in Meilisearch.

func (*MeilisearchEngine) Search

func (e *MeilisearchEngine) Search(ctx context.Context, index string, query string, opts *Options) (*Results, error)

Search performs a search query against Meilisearch.

type Options

type Options struct {
	// Page number (1-based).
	Page int
	// PerPage results per page.
	PerPage int
	// Filters are key=value filters applied before text search.
	Filters map[string]any
	// Sort column (e.g. "created_at", "-score" for descending).
	Sort string
}

Options configures a search query.

func DefaultOptions

func DefaultOptions() *Options

DefaultOptions returns sensible defaults.

type Plugin

type Plugin struct {
	nimbus.BasePlugin
	// contains filtered or unexported fields
}

Plugin wraps the search engine as a Nimbus plugin, making it installable via `nimbus plugin install scout` and selectable during `nimbus new`.

func NewPlugin

func NewPlugin(engine Engine) *Plugin

New creates a new Scout search plugin. Pass nil to auto-detect engine from the SEARCH_DRIVER env var (defaults to "postgres").

func (*Plugin) Boot

func (p *Plugin) Boot(app *nimbus.App) error

Boot is a no-op for Scout.

func (*Plugin) DefaultConfig

func (p *Plugin) DefaultConfig() map[string]any

DefaultConfig returns default scout configuration.

func (*Plugin) Engine

func (p *Plugin) Engine() Engine

Engine returns the underlying search engine.

func (*Plugin) Register

func (p *Plugin) Register(app *nimbus.App) error

Register binds the search engine into the container and sets it as default.

type PostgresEngine

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

PostgresEngine uses PostgreSQL's built-in tsvector full-text search. It stores searchable data in a `search_index` table with a GIN index.

func NewPostgresEngine

func NewPostgresEngine(db *lucid.DB) *PostgresEngine

NewPostgresEngine creates a PostgreSQL-backed search engine. It auto-migrates the search_index table.

func (*PostgresEngine) Delete

func (e *PostgresEngine) Delete(ctx context.Context, index string, id string) error

Delete removes a document from the search index.

func (*PostgresEngine) Flush

func (e *PostgresEngine) Flush(ctx context.Context, index string) error

Flush removes all documents from an index.

func (*PostgresEngine) Index

func (e *PostgresEngine) Index(ctx context.Context, index string, id string, data map[string]any) error

Index adds or updates a document in the search index.

func (*PostgresEngine) Search

func (e *PostgresEngine) Search(ctx context.Context, index string, query string, opts *Options) (*Results, error)

Search performs a full-text search using PostgreSQL's to_tsquery.

type Result

type Result struct {
	ID    string         `json:"id"`
	Score float64        `json:"score"`
	Data  map[string]any `json:"data"`
}

Result represents a single search result.

type Results

type Results struct {
	Hits    []Result `json:"hits"`
	Total   int64    `json:"total"`
	Page    int      `json:"page"`
	PerPage int      `json:"per_page"`
	Query   string   `json:"query"`
}

Results holds search results and metadata.

func Query

func Query(ctx context.Context, index, query string, opts *Options) (*Results, error)

Query performs a search using the default engine.

type Searchable

type Searchable interface {
	// SearchIndex returns the index/table name for search.
	SearchIndex() string
	// SearchID returns the unique identifier for this record.
	SearchID() string
	// SearchData returns the data to index (map of field→value).
	SearchData() map[string]any
}

Searchable is implemented by models that should be indexed for search.

type TypesenseConfig

type TypesenseConfig struct {
	Host   string // e.g. "http://localhost:8108"
	APIKey string
}

TypesenseConfig holds Typesense connection settings.

type TypesenseEngine

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

TypesenseEngine uses Typesense as the search backend.

func NewTypesenseEngine

func NewTypesenseEngine(cfg TypesenseConfig) *TypesenseEngine

NewTypesenseEngine creates a Typesense-backed search engine.

func (*TypesenseEngine) Delete

func (e *TypesenseEngine) Delete(ctx context.Context, index string, id string) error

Delete removes a document from Typesense.

func (*TypesenseEngine) Flush

func (e *TypesenseEngine) Flush(ctx context.Context, index string) error

Flush removes all documents from an index in Typesense (drops and recreates).

func (*TypesenseEngine) Index

func (e *TypesenseEngine) Index(ctx context.Context, index string, id string, data map[string]any) error

Index adds or updates a document in Typesense.

func (*TypesenseEngine) Search

func (e *TypesenseEngine) Search(ctx context.Context, index string, query string, opts *Options) (*Results, error)

Search performs a search query against Typesense.

Jump to

Keyboard shortcuts

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