Documentation
¶
Overview ¶
Package search implements GoatFlow's search backends: the application database (default) and an Elasticsearch-compatible index (Zinc or Elasticsearch) kept in sync with the database.
Index ¶
- Constants
- Variables
- type DatabaseBackend
- type ExternalBackend
- func (b *ExternalBackend) GetBackendName() string
- func (b *ExternalBackend) HealthCheck(ctx context.Context) error
- func (b *ExternalBackend) Rebuild(ctx context.Context, db *sql.DB) error
- func (b *ExternalBackend) RebuildStatus() RebuildStatus
- func (b *ExternalBackend) Search(ctx context.Context, query SearchQuery) (*SearchResults, error)
- func (b *ExternalBackend) StartRebuild(db *sql.DB) bool
- func (b *ExternalBackend) Status(ctx context.Context) (IndexStatus, error)
- func (b *ExternalBackend) Sync(ctx context.Context, db *sql.DB) (SyncStats, error)
- type ExternalConfig
- type IndexStatus
- type RebuildStatus
- type SearchBackend
- type SearchHit
- type SearchQuery
- type SearchResults
- type ServiceError
- type SyncStats
Constants ¶
const ( FlavorZinc = "zinc" FlavorElasticsearch = "elasticsearch" )
External backend flavors.
const DefaultIndexPrefix = "goatflow_"
DefaultIndexPrefix prefixes the names of the indices GoatFlow creates.
Variables ¶
var ( // ErrInvalidQuery: the query or one of its filters is malformed. ErrInvalidQuery = errors.New("invalid search query") // ErrIndexNotReady: the external index has not been built yet (or was // removed); searching it would return incomplete results. ErrIndexNotReady = errors.New("search index has not been built yet") )
Functions ¶
This section is empty.
Types ¶
type DatabaseBackend ¶ added in v0.10.0
type DatabaseBackend struct {
// contains filtered or unexported fields
}
DatabaseBackend implements SearchBackend by querying the GoatFlow tables directly with portable SQL (MySQL/MariaDB and PostgreSQL).
Matching: the query is split into words and every word must occur (case-insensitive substring) in at least one searched column. Relevance is a CASE expression: exact ticket number / title / subject / login matches rank above partial title matches, which rank above body-only matches.
func NewDatabaseBackend ¶ added in v0.10.0
func NewDatabaseBackend() (*DatabaseBackend, error)
NewDatabaseBackend creates a search backend over the application database.
func (*DatabaseBackend) GetBackendName ¶ added in v0.10.0
func (db *DatabaseBackend) GetBackendName() string
GetBackendName returns the backend name.
func (*DatabaseBackend) HealthCheck ¶ added in v0.10.0
func (db *DatabaseBackend) HealthCheck(ctx context.Context) error
HealthCheck verifies the database connection.
func (*DatabaseBackend) Search ¶ added in v0.10.0
func (db *DatabaseBackend) Search(ctx context.Context, query SearchQuery) (*SearchResults, error)
Search searches the requested entity types. Hits are concatenated in the order of query.Types, each type ordered by relevance, then paginated.
type ExternalBackend ¶ added in v0.10.0
type ExternalBackend struct {
// contains filtered or unexported fields
}
ExternalBackend searches an Elasticsearch-compatible index (Zinc's /es API or Elasticsearch). The index holds one document per ticket, article and customer user; Sync and Rebuild keep it in step with the database.
Every ticket and article hit is re-checked against the database before it is returned: hits whose row is gone, or whose ticket is now in a queue the caller may not read, are dropped. Results are therefore never wider than the database backend's, even while the index lags behind.
func NewExternalBackend ¶ added in v0.10.0
func NewExternalBackend(cfg ExternalConfig) *ExternalBackend
NewExternalBackend returns a backend for cfg.
func (*ExternalBackend) GetBackendName ¶ added in v0.10.0
func (b *ExternalBackend) GetBackendName() string
GetBackendName returns "zinc" or "elasticsearch".
func (*ExternalBackend) HealthCheck ¶ added in v0.10.0
func (b *ExternalBackend) HealthCheck(ctx context.Context) error
HealthCheck verifies the service is reachable with the configured credentials and the index has been built.
func (*ExternalBackend) Rebuild ¶ added in v0.10.0
Rebuild indexes every ticket, article and customer user and removes documents whose row no longer exists. The index stays searchable while it runs once it has been built before.
func (*ExternalBackend) RebuildStatus ¶ added in v0.10.0
func (b *ExternalBackend) RebuildStatus() RebuildStatus
RebuildStatus returns the state of the latest rebuild.
func (*ExternalBackend) Search ¶ added in v0.10.0
func (b *ExternalBackend) Search(ctx context.Context, query SearchQuery) (*SearchResults, error)
Search searches the requested entity types. As in the database backend, hits are concatenated in the order of query.Types, each type ordered by relevance, then paginated.
func (*ExternalBackend) StartRebuild ¶ added in v0.10.0
func (b *ExternalBackend) StartRebuild(db *sql.DB) bool
StartRebuild starts a full rebuild in the background. It returns false when one is already running in this process.
func (*ExternalBackend) Status ¶ added in v0.10.0
func (b *ExternalBackend) Status(ctx context.Context) (IndexStatus, error)
Status returns the index state and document counts.
func (*ExternalBackend) Sync ¶ added in v0.10.0
Sync brings the index up to date with the database. It builds the index when it has never been built, was built for another schema version, or has not been synced for longer than the window covers. Otherwise it reindexes tickets whose ticket or article rows changed or that gained history entries and customer users that changed, and periodically removes documents of deleted rows.
type ExternalConfig ¶ added in v0.10.0
type ExternalConfig struct {
Flavor string // FlavorZinc or FlavorElasticsearch
Endpoint string // base URL, e.g. http://zinc:4080
Username string // empty: no authentication
Password string
IndexPrefix string // empty: DefaultIndexPrefix
}
ExternalConfig configures an Elasticsearch-compatible search service.
func ConfigFromEnv ¶ added in v0.10.0
func ConfigFromEnv() (*ExternalConfig, error)
ConfigFromEnv reads the search backend configuration. It returns nil (and no error) when the database backend is selected.
SEARCH_BACKEND "" or "database", "zinc", "elasticsearch" ZINC_ENDPOINT, ZINC_USER, ZINC_PASSWORD ELASTICSEARCH_ENDPOINT, ELASTICSEARCH_USERNAME, ELASTICSEARCH_PASSWORD SEARCH_INDEX_PREFIX index name prefix (default goatflow_)
type IndexStatus ¶ added in v0.10.0
type IndexStatus struct {
Ready bool `json:"ready"`
LastSync string `json:"last_sync,omitempty"`
Documents map[string]int `json:"documents,omitempty"`
}
IndexStatus describes the index for the health endpoint.
type RebuildStatus ¶ added in v0.10.0
type RebuildStatus struct {
Running bool `json:"running"`
StartedAt *time.Time `json:"started_at,omitempty"`
FinishedAt *time.Time `json:"finished_at,omitempty"`
Tickets int `json:"tickets"`
Customers int `json:"customers"`
Error string `json:"error,omitempty"`
}
RebuildStatus describes the most recent full rebuild started by this process.
type SearchBackend ¶
type SearchBackend interface {
// Search performs a search across the requested entity types.
Search(ctx context.Context, query SearchQuery) (*SearchResults, error)
// HealthCheck reports whether the backend can serve searches.
HealthCheck(ctx context.Context) error
// GetBackendName returns the name of the search backend.
GetBackendName() string
}
SearchBackend searches tickets, articles and customers.
type SearchHit ¶
type SearchHit struct {
ID string `json:"id"`
Type string `json:"type"`
Score float64 `json:"score"`
Title string `json:"title"`
Content string `json:"content"`
Highlights map[string][]string `json:"highlights,omitempty"`
Metadata map[string]interface{} `json:"metadata"`
}
SearchHit represents a single search result.
type SearchQuery ¶
type SearchQuery struct {
Query string `json:"query"` // The search query string
Types []string `json:"types"` // Entity types to search (ticket, article, customer)
Filters map[string]string `json:"filters"` // Ticket filters: queue_id, state_id
Offset int `json:"offset"` // Pagination offset
Limit int `json:"limit"` // Results per page
Highlight bool `json:"highlight"` // Enable result highlighting
// RestrictQueues limits ticket and article hits to tickets in QueueIDs (an
// empty QueueIDs then matches none). Set by the server from the caller's
// permissions, never from the request body.
RestrictQueues bool `json:"-"`
QueueIDs []int `json:"-"`
}
SearchQuery represents a search request.
type SearchResults ¶
type SearchResults struct {
Query string `json:"query"`
TotalHits int `json:"total_hits"`
Took int64 `json:"took_ms"` // Time taken in milliseconds
Hits []SearchHit `json:"hits"`
}
SearchResults contains search results.
type ServiceError ¶ added in v0.10.0
ServiceError reports that the external search service could not serve a request. Reason is safe to show to users; Err carries the detail.
func (*ServiceError) Error ¶ added in v0.10.0
func (e *ServiceError) Error() string
func (*ServiceError) Unwrap ¶ added in v0.10.0
func (e *ServiceError) Unwrap() error