feat: index the text of attached PDFs

- jz_bm_material_file_texts caches the pdftotext output per file
- the River job jz.bm.extract_material_text extracts and resyncs
- the management multipart attach queues it inside its transaction
- file_texts in the document and a file snippet with its name
This commit is contained in:
Jakub Zych
2026-11-08 20:11:19 +01:00
parent dd13795e6f
commit 412821ec56
21 changed files with 747 additions and 24 deletions

View File

@@ -13,8 +13,11 @@ import (
"git.golem15.com/golem15/summercms/modules/backpack"
"git.golem15.com/golem15/summercms/modules/beachcomber"
"git.golem15.com/golem15/summercms/modules/beachcomber/typesense"
"git.golem15.com/golem15/summercms/modules/conga"
"git.golem15.com/golem15/summercms/modules/diary"
"git.golem15.com/golem15/summercms/modules/lagoon"
"git.golem15.com/golem15/summercms/modules/pact"
"git.golem15.com/jakub/sm-bm-plugin/jobs"
"git.golem15.com/jakub/sm-bm-plugin/models"
"gorm.io/gorm"
)
@@ -92,6 +95,9 @@ type MaterialIndexer struct {
Service func() *beachcomber.Service
// Log is where failures are logged; nil means slog.Default.
Log *slog.Logger
// Enqueue inserts a background job, inside tx when tx is in a
// transaction. Nil means jobs are not queued.
Enqueue func(ctx context.Context, tx *gorm.DB, args pact.JobArgs) error
}
// NewMaterialIndexer returns the indexer of app. The service is resolved on
@@ -109,6 +115,27 @@ func NewMaterialIndexer(app *backpack.App) *MaterialIndexer {
return svc
},
Log: diary.For("jz.bm"),
Enqueue: func(ctx context.Context, tx *gorm.DB, args pact.JobArgs) error {
m, err := conga.From(app)
if err != nil {
return err
}
return m.Enqueue(ctx, tx, args, conga.EnqueueOpts{})
},
}
}
// QueueExtract enqueues the file text job of material id inside tx, so the
// job exists only when the attach commits. The job extracts the text of the
// material's new PDFs and then syncs its document. A failure is logged and
// not returned: an attach never fails because of the job, and
// bm:materials-reindex backfills the text.
func (ix *MaterialIndexer) QueueExtract(ctx context.Context, tx *gorm.DB, id uint) {
if ix == nil || ix.Enqueue == nil || id == 0 {
return
}
if err := ix.Enqueue(ctx, tx, jobs.ExtractMaterialTextArgs{MaterialID: id}); err != nil {
ix.log().Warn("jz.bm: could not queue the file text job", "material_id", id, "err", err)
}
}