search

package
v0.10.1 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package search provides a pluggable full-text search layer for the mailbox library.

Usage:

provider, _ := meilisearch.New(meilisearch.WithHost("http://localhost:7700"), meilisearch.WithAPIKey("key"))
plugin, wrappedStore := search.New(provider, myStore)
svc, _ := mailbox.New(cfg, mailbox.WithStore(wrappedStore), mailbox.WithPlugin(plugin))
svc.Events().MessageReceived.Subscribe(ctx, event.AsWorker(plugin.OnMessageReceived))
svc.Events().MessageDeleted.Subscribe(ctx, event.AsWorker(plugin.OnDelete))
svc.Events().MessageMoved.Subscribe(ctx, event.AsWorker(plugin.OnMessageMoved))
svc.Events().MessageRead.Subscribe(ctx, event.AsWorker(plugin.OnMessageRead))
svc.Events().MarkAllRead.Subscribe(ctx, event.AsWorker(plugin.OnMarkAllRead))

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Document

type Document struct {
	ID        string    `json:"id"`
	OwnerID   string    `json:"owner_id"`
	SenderID  string    `json:"sender_id"`
	Subject   string    `json:"subject"`
	Body      string    `json:"body"`
	FolderID  string    `json:"folder_id"`
	Tags      []string  `json:"tags"`
	IsRead    bool      `json:"is_read"`
	CreatedAt time.Time `json:"created_at"`
}

Document is a flat message representation for indexing.

type Option

type Option func(*options)

Option configures the search Plugin.

func WithFallback

func WithFallback(enabled bool) Option

WithFallback controls whether a search provider error causes a fallback to the primary store's Search. Defaults to true.

func WithLogger

func WithLogger(l *slog.Logger) Option

WithLogger sets the logger used by the plugin. Defaults to slog.Default().

type Plugin

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

Plugin wraps a Provider as a mailbox.Plugin and mailbox.SendHook. It exposes event handlers for message received, moved, read, mark-all-read, and deleted events, and overrides the store's Search to delegate to the provider.

func New

func New(provider Provider, st store.Store, opts ...Option) (*Plugin, store.Store)

New creates a search Plugin wrapping the given provider and primary store. The returned store.Store overrides Search to delegate to the provider; pass it as the store to mailbox.New. The plugin must also be registered via mailbox.WithPlugin so that Init/Close and AfterSend are called by the service.

Subscribe to events after the service is created:

svc.Events().MessageReceived.Subscribe(ctx, event.AsWorker(plugin.OnMessageReceived))
svc.Events().MessageDeleted.Subscribe(ctx, event.AsWorker(plugin.OnDelete))
svc.Events().MessageMoved.Subscribe(ctx, event.AsWorker(plugin.OnMessageMoved))
svc.Events().MessageRead.Subscribe(ctx, event.AsWorker(plugin.OnMessageRead))
svc.Events().MarkAllRead.Subscribe(ctx, event.AsWorker(plugin.OnMarkAllRead))

func (*Plugin) AfterSend

func (p *Plugin) AfterSend(ctx context.Context, _ string, msg store.Message) error

AfterSend indexes the message in the search provider after a successful send.

func (*Plugin) BeforeSend

func (p *Plugin) BeforeSend(_ context.Context, _ string, _ store.DraftMessage) error

BeforeSend satisfies mailbox.SendHook. No pre-send action is needed.

func (*Plugin) Close

func (p *Plugin) Close(ctx context.Context) error

Close releases provider resources.

func (*Plugin) Init

func (p *Plugin) Init(ctx context.Context) error

Init connects the provider (creates index / applies settings) then pings it.

func (*Plugin) Name

func (p *Plugin) Name() string

Name returns the plugin identifier.

func (*Plugin) OnDelete

func (p *Plugin) OnDelete(ctx context.Context, _ any, evt mailbox.MessageDeletedEvent) error

OnDelete removes a deleted message from the search index. Register this with svc.Events().MessageDeleted.Subscribe(ctx, event.AsWorker(p.OnDelete)).

func (*Plugin) OnMarkAllRead added in v0.7.8

func (p *Plugin) OnMarkAllRead(ctx context.Context, _ any, evt mailbox.MarkAllReadEvent) error

OnMarkAllRead re-indexes all messages in the folder whose is_read status changed. Register with svc.Events().MarkAllRead.Subscribe(ctx, event.AsWorker(p.OnMarkAllRead)).

func (*Plugin) OnMessageMoved added in v0.7.8

func (p *Plugin) OnMessageMoved(ctx context.Context, _ any, evt mailbox.MessageMovedEvent) error

OnMessageMoved re-indexes a moved message to update its folder_id in the index. Register with svc.Events().MessageMoved.Subscribe(ctx, event.AsWorker(p.OnMessageMoved)).

func (*Plugin) OnMessageRead added in v0.7.8

func (p *Plugin) OnMessageRead(ctx context.Context, _ any, evt mailbox.MessageReadEvent) error

OnMessageRead re-indexes a message whose read status changed. Register with svc.Events().MessageRead.Subscribe(ctx, event.AsWorker(p.OnMessageRead)).

func (*Plugin) OnMessageReceived

func (p *Plugin) OnMessageReceived(ctx context.Context, _ any, evt mailbox.MessageReceivedEvent) error

OnMessageReceived indexes a received message. Register this with svc.Events().MessageReceived.Subscribe(ctx, event.AsWorker(p.OnMessageReceived)).

type Provider

type Provider interface {
	// Name identifies the backend (e.g., "meilisearch", "elasticsearch").
	Name() string
	// Connect initializes the backend (creates index, applies settings).
	// Called by Plugin.Init; the ctx deadline controls the setup timeout.
	Connect(ctx context.Context) error
	// Index upserts a document. Called after send and on message received/moved/read.
	Index(ctx context.Context, doc Document) error
	// Delete removes a document by message ID.
	Delete(ctx context.Context, messageID string) error
	// Search returns message IDs matching q, in relevance order.
	// OwnerID is always set; implementations must scope results to it.
	Search(ctx context.Context, q store.SearchQuery) ([]string, error)
	// Ping checks backend connectivity. Called during Init after Connect.
	Ping(ctx context.Context) error
	// Close releases resources.
	Close(ctx context.Context) error
}

Provider is the search backend interface. Implementations must be safe for concurrent use.

Directories

Path Synopsis
Package elasticsearch provides an Elasticsearch-backed search.Provider.
Package elasticsearch provides an Elasticsearch-backed search.Provider.
Package meilisearch provides a Meilisearch-backed search.Provider.
Package meilisearch provides a Meilisearch-backed search.Provider.

Jump to

Keyboard shortcuts

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