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 ¶
- func DeleteRecord(ctx context.Context, record Searchable) error
- func IndexRecord(ctx context.Context, record Searchable) error
- func Register(name string, engine Engine)
- type Engine
- type MeilisearchConfig
- type MeilisearchEngine
- func (e *MeilisearchEngine) Delete(ctx context.Context, index string, id string) error
- func (e *MeilisearchEngine) Flush(ctx context.Context, index string) error
- func (e *MeilisearchEngine) Index(ctx context.Context, index string, id string, data map[string]any) error
- func (e *MeilisearchEngine) Search(ctx context.Context, index string, query string, opts *Options) (*Results, error)
- type Options
- type Plugin
- type PostgresEngine
- func (e *PostgresEngine) Delete(ctx context.Context, index string, id string) error
- func (e *PostgresEngine) Flush(ctx context.Context, index string) error
- func (e *PostgresEngine) Index(ctx context.Context, index string, id string, data map[string]any) error
- func (e *PostgresEngine) Search(ctx context.Context, index string, query string, opts *Options) (*Results, error)
- type Result
- type Results
- type Searchable
- type TypesenseConfig
- type TypesenseEngine
- func (e *TypesenseEngine) Delete(ctx context.Context, index string, id string) error
- func (e *TypesenseEngine) Flush(ctx context.Context, index string) error
- func (e *TypesenseEngine) Index(ctx context.Context, index string, id string, data map[string]any) error
- func (e *TypesenseEngine) Search(ctx context.Context, index string, query string, opts *Options) (*Results, error)
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.
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.
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) Flush ¶
func (e *MeilisearchEngine) Flush(ctx context.Context, index string) error
Flush removes all documents from an index in 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.
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 ¶
New creates a new Scout search plugin. Pass nil to auto-detect engine from the SEARCH_DRIVER env var (defaults to "postgres").
func (*Plugin) DefaultConfig ¶
DefaultConfig returns default scout configuration.
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) Flush ¶
func (e *PostgresEngine) Flush(ctx context.Context, index string) error
Flush removes all documents from an index.
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.
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) Flush ¶
func (e *TypesenseEngine) Flush(ctx context.Context, index string) error
Flush removes all documents from an index in Typesense (drops and recreates).