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 ¶
- type Document
- type Option
- type Plugin
- func (p *Plugin) AfterSend(ctx context.Context, _ string, msg store.Message) error
- func (p *Plugin) BeforeSend(_ context.Context, _ string, _ store.DraftMessage) error
- func (p *Plugin) Close(ctx context.Context) error
- func (p *Plugin) Init(ctx context.Context) error
- func (p *Plugin) Name() string
- func (p *Plugin) OnDelete(ctx context.Context, _ any, evt mailbox.MessageDeletedEvent) error
- func (p *Plugin) OnMarkAllRead(ctx context.Context, _ any, evt mailbox.MarkAllReadEvent) error
- func (p *Plugin) OnMessageMoved(ctx context.Context, _ any, evt mailbox.MessageMovedEvent) error
- func (p *Plugin) OnMessageRead(ctx context.Context, _ any, evt mailbox.MessageReadEvent) error
- func (p *Plugin) OnMessageReceived(ctx context.Context, _ any, evt mailbox.MessageReceivedEvent) error
- type Provider
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 ¶
WithFallback controls whether a search provider error causes a fallback to the primary store's Search. Defaults to true.
func WithLogger ¶
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 ¶
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 ¶
AfterSend indexes the message in the search provider after a successful send.
func (*Plugin) BeforeSend ¶
BeforeSend satisfies mailbox.SendHook. No pre-send action is needed.
func (*Plugin) OnDelete ¶
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
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
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
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. |