search

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildPrefixQuery

func BuildPrefixQuery(userInput string) string

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

func RegisterRoutes(g *echo.Group, db *bun.DB)

RegisterRoutes registers search routes on a group the server has already configured with authentication.

func SanitizeFTSQuery

func SanitizeFTSQuery(input string) string

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

type GlobalSearchSections struct {
	Series bool
	People bool
}

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 NewService(db *bun.DB) *Service

func (*Service) CollectAffected added in v0.1.0

func (svc *Service) CollectAffected(ctx context.Context, a Affected) *Affected

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

func (svc *Service) DeleteFromBookIndex(ctx context.Context, bookID int) error

DeleteFromBookIndex removes a book from the FTS index.

func (*Service) DeleteFromGenreIndex

func (svc *Service) DeleteFromGenreIndex(ctx context.Context, genreID int) error

DeleteFromGenreIndex removes a genre from the FTS index.

func (*Service) DeleteFromPersonIndex

func (svc *Service) DeleteFromPersonIndex(ctx context.Context, personID int) error

DeleteFromPersonIndex removes a person from the FTS index.

func (*Service) DeleteFromPublisherIndex added in v0.0.44

func (svc *Service) DeleteFromPublisherIndex(ctx context.Context, publisherID int) error

DeleteFromPublisherIndex removes a publisher from the FTS index.

func (*Service) DeleteFromSeriesIndex

func (svc *Service) DeleteFromSeriesIndex(ctx context.Context, seriesID int) error

DeleteFromSeriesIndex removes a series from the FTS index.

func (*Service) DeleteFromTagIndex

func (svc *Service) DeleteFromTagIndex(ctx context.Context, tagID int) error

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) IndexBook

func (svc *Service) IndexBook(ctx context.Context, book *models.Book) error

IndexBook adds or updates a book in the FTS index.

func (*Service) IndexGenre

func (svc *Service) IndexGenre(ctx context.Context, genre *models.Genre) error

IndexGenre adds or updates a genre in the FTS index.

func (*Service) IndexPerson

func (svc *Service) IndexPerson(ctx context.Context, person *models.Person) error

IndexPerson adds or updates a person in the FTS index.

func (*Service) IndexPublisher added in v0.0.44

func (svc *Service) IndexPublisher(ctx context.Context, publisher *models.Publisher) error

IndexPublisher adds or updates a publisher in the FTS index.

func (*Service) IndexSeries

func (svc *Service) IndexSeries(ctx context.Context, series *models.Series) error

IndexSeries adds or updates a series in the FTS index.

func (*Service) IndexTag

func (svc *Service) IndexTag(ctx context.Context, tag *models.Tag) error

IndexTag adds or updates a tag in the FTS index.

func (*Service) RebuildAllIndexes

func (svc *Service) RebuildAllIndexes(ctx context.Context) error

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

func (svc *Service) ReindexAffected(ctx context.Context, a *Affected)

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

func (svc *Service) ReindexBookByID(ctx context.Context, bookID int) error

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

func (svc *Service) ReindexSeriesByID(ctx context.Context, seriesID int) error

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.

func (*Service) SearchPeople

func (svc *Service) SearchPeople(ctx context.Context, libraryID int, query string, limit, offset int) ([]PersonSearchResult, int, error)

SearchPeople searches people.

func (*Service) SearchSeries

func (svc *Service) SearchSeries(ctx context.Context, libraryID int, query string, limit, offset int) ([]SeriesSearchResult, int, error)

SearchSeries searches series.

Jump to

Keyboard shortcuts

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