Documentation
¶
Index ¶
- func BuildPrefixQuery(userInput string) string
- func RegisterRoutes(g *echo.Group, db *bun.DB)
- func SanitizeFTSQuery(input string) string
- type Affected
- type BookSearchResult
- type GlobalSearchQuery
- type GlobalSearchResponse
- type GlobalSearchSections
- type PersonSearchResult
- type SeriesSearchResult
- type Service
- func (svc *Service) CollectAffected(ctx context.Context, a Affected) *Affected
- func (svc *Service) DeleteFromBookIndex(ctx context.Context, bookID int) error
- func (svc *Service) DeleteFromGenreIndex(ctx context.Context, genreID int) error
- func (svc *Service) DeleteFromPersonIndex(ctx context.Context, personID int) error
- func (svc *Service) DeleteFromPublisherIndex(ctx context.Context, publisherID int) error
- func (svc *Service) DeleteFromSeriesIndex(ctx context.Context, seriesID int) error
- func (svc *Service) DeleteFromTagIndex(ctx context.Context, tagID int) error
- func (svc *Service) GlobalSearch(ctx context.Context, libraryID int, query string, ...) (*GlobalSearchResponse, error)
- func (svc *Service) IndexBook(ctx context.Context, book *models.Book) error
- func (svc *Service) IndexGenre(ctx context.Context, genre *models.Genre) error
- func (svc *Service) IndexPerson(ctx context.Context, person *models.Person) error
- func (svc *Service) IndexPublisher(ctx context.Context, publisher *models.Publisher) error
- func (svc *Service) IndexSeries(ctx context.Context, series *models.Series) error
- func (svc *Service) IndexTag(ctx context.Context, tag *models.Tag) error
- func (svc *Service) RebuildAllIndexes(ctx context.Context) error
- func (svc *Service) ReindexAffected(ctx context.Context, a *Affected)
- func (svc *Service) ReindexBookByID(ctx context.Context, bookID int) error
- func (svc *Service) ReindexSeriesByID(ctx context.Context, seriesID int) error
- func (svc *Service) SearchBooks(ctx context.Context, libraryID int, query string, fileTypes []string, ...) ([]BookSearchResult, int, error)
- func (svc *Service) SearchPeople(ctx context.Context, libraryID int, query string, limit, offset int) ([]PersonSearchResult, int, error)
- func (svc *Service) SearchSeries(ctx context.Context, libraryID int, query string, limit, offset int) ([]SeriesSearchResult, int, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildPrefixQuery ¶
BuildPrefixQuery creates an FTS5 query for typeahead/prefix search. It sanitizes the input and appends a wildcard for prefix matching.
func RegisterRoutes ¶ added in v0.1.0
RegisterRoutes registers search routes on a group the server has already configured with authentication.
func SanitizeFTSQuery ¶
SanitizeFTSQuery escapes FTS5 special characters and wraps in quotes for literal matching. FTS5 has its own query language with operators (AND, OR, NOT, *, NEAR(), :, ", etc.). Even with parameterized queries, the FTS5 engine interprets these. This function ensures user input is treated as a literal phrase.
Types ¶
type Affected ¶ added in v0.1.0
type Affected struct {
BookIDs []int
SeriesIDs []int
PersonIDs []int
GenreIDs []int
TagIDs []int
PublisherIDs []int
}
Affected names the entities a mutation touched. ReindexAffected expands it to every entity whose FTS row copies one of their columns and rewrites each of those rows once:
- a Person expands to the Books it authors or narrates (books_fts authors and narrators);
- a Series expands to its Books (books_fts series_names);
- a Book, given or expanded, expands to every Series holding it (series_fts book_titles and book_authors).
Genres, Tags, and Publishers reindex their own rows only, because no other FTS table copies them.
type BookSearchResult ¶
type BookSearchResult struct {
ID int `json:"id"`
Title string `json:"title"`
Subtitle *string `json:"subtitle"`
Authors string `json:"authors"` // Comma-separated author names
FileTypes []string `json:"file_types"` // Unique file types for this book (e.g., ["epub", "m4b"])
LibraryID int `json:"library_id"`
}
BookSearchResult represents a book in search results.
type GlobalSearchQuery ¶
type GlobalSearchQuery struct {
Query string `query:"q" json:"q" validate:"required,min=1,max=100"`
LibraryID int `query:"library_id" json:"library_id" validate:"required,min=1"`
}
GlobalSearchQuery represents the query parameters for global search.
type GlobalSearchResponse ¶
type GlobalSearchResponse struct {
Books []BookSearchResult `json:"books"`
Series []SeriesSearchResult `json:"series"`
People []PersonSearchResult `json:"people"`
}
GlobalSearchResponse represents the response from global search. Returns up to 5 results per resource type for popover display.
type GlobalSearchSections ¶ added in v0.1.0
GlobalSearchSections picks the optional sections GlobalSearch fills. Books are always searched because the search route requires books:read; a section left out comes back as an empty array, not a missing key.
type PersonSearchResult ¶
type PersonSearchResult struct {
ID int `json:"id"`
Name string `json:"name"`
SortName string `json:"sort_name"`
LibraryID int `json:"library_id"`
}
PersonSearchResult represents a person in search results.
type SeriesSearchResult ¶
type SeriesSearchResult struct {
ID int `json:"id"`
Name string `json:"name"`
BookCount int `json:"book_count"`
LibraryID int `json:"library_id"`
}
SeriesSearchResult represents a series in search results.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
func NewService ¶
func (*Service) CollectAffected ¶ added in v0.1.0
CollectAffected expands a over the links as they stand now and returns the result for ReindexAffected. Call it before the mutation: a delete or a relink drops links (CASCADE removes a deleted Book's book_series rows), and the entities on the far side of a dropped link are found only through the links read here. The result is a pointer so ids learned during the mutation, such as a Book it creates, can be added to it before the deferred ReindexAffected runs.
A lookup failure is logged and the ids given are kept, so the reindex still covers them.
func (*Service) DeleteFromBookIndex ¶
DeleteFromBookIndex removes a book from the FTS index.
func (*Service) DeleteFromGenreIndex ¶
DeleteFromGenreIndex removes a genre from the FTS index.
func (*Service) DeleteFromPersonIndex ¶
DeleteFromPersonIndex removes a person from the FTS index.
func (*Service) DeleteFromPublisherIndex ¶ added in v0.0.44
DeleteFromPublisherIndex removes a publisher from the FTS index.
func (*Service) DeleteFromSeriesIndex ¶
DeleteFromSeriesIndex removes a series from the FTS index.
func (*Service) DeleteFromTagIndex ¶
DeleteFromTagIndex removes a tag from the FTS index.
func (*Service) GlobalSearch ¶
func (svc *Service) GlobalSearch(ctx context.Context, libraryID int, query string, sections GlobalSearchSections) (*GlobalSearchResponse, error)
GlobalSearch searches a library's books, plus the series and people that sections asks for. Returns up to 5 results per resource type for popover display.
func (*Service) IndexGenre ¶
IndexGenre adds or updates a genre in the FTS index.
func (*Service) IndexPerson ¶
IndexPerson adds or updates a person in the FTS index.
func (*Service) IndexPublisher ¶ added in v0.0.44
IndexPublisher adds or updates a publisher in the FTS index.
func (*Service) IndexSeries ¶
IndexSeries adds or updates a series in the FTS index.
func (*Service) RebuildAllIndexes ¶
RebuildAllIndexes rebuilds all FTS indexes from scratch in one transaction, so no search sees half-empty tables and a failure part way through leaves the previous index in place. The database pool has a single connection (pkg/database), so every other query waits until the transaction commits. A scan job calls it when it finishes or fails, and the worker calls it at startup when the last scan was cut short by shutdown.
func (*Service) ReindexAffected ¶ added in v0.1.0
ReindexAffected rewrites the FTS row of every entity in a and in its expansion (see Affected), reading the links as they stand after the mutation. A row whose entity no longer exists is deleted. Callers defer it right after CollectAffected, so it runs after the mutation commits, on the error paths too: a handler that fails after committing part of its writes still leaves the index matching the database.
It ignores ctx cancellation, because the writes it follows have already committed, and logs each failure and moves on, so a search index problem never fails the request or job that made the change.
func (*Service) ReindexBookByID ¶ added in v0.0.43
ReindexBookByID rewrites one book's books_fts row from the database, or deletes it when the book no longer exists.
func (*Service) ReindexSeriesByID ¶ added in v0.1.0
ReindexSeriesByID rewrites one series' series_fts row from the database, or deletes it when the series no longer exists.
func (*Service) SearchBooks ¶
func (svc *Service) SearchBooks(ctx context.Context, libraryID int, query string, fileTypes []string, limit, offset int) ([]BookSearchResult, int, error)
SearchBooks searches books with optional file type filter.