Skip to content

Build your personal knowledge base with Trilium Notes

fix(ocr): keep batch OCR selection NULL-only to avoid perpetual reprocessing

Elian Doran0e20a79f835e
diff --git a/apps/server/src/services/ocr/ocr_service.spec.ts b/apps/server/src/services/ocr/ocr_service.spec.ts
index a6ad7d7b28..ac0d64891f 100644
--- a/apps/server/src/services/ocr/ocr_service.spec.ts
+++ b/apps/server/src/services/ocr/ocr_service.spec.ts
@@ -752,17 +752,18 @@ describe('OCRService', () => {
]);
});
- it('re-selects only PDF blobs whose previous OCR produced an empty result', () => {
+ it('selects only never-processed blobs, treating an empty result as terminal', () => {
mockSql.getRows.mockReturnValue([]);
ocrService.getBlobsNeedingOCR();
- // Empty text representations are re-processed only for PDFs (which gained
- // the scanned-page fallback). Other formats keep the NULL-only semantics,
- // otherwise every text-less image would be re-OCR'd on every batch run.
+ // A stored empty string means "processed, found no text" and must not be
+ // re-selected, otherwise a blank/text-less blob would be re-OCR'd on every
+ // batch run. Both the note and attachment queries filter on NULL only.
expect(mockSql.getRows.mock.calls).toHaveLength(2);
for (const [sqlText] of mockSql.getRows.mock.calls) {
- expect(sqlText).toContain("application/pdf' AND b.textRepresentation = ''");
+ expect(sqlText).toContain('textRepresentation IS NULL');
+ expect(sqlText).not.toContain("textRepresentation = ''");
}
});
diff --git a/apps/server/src/services/ocr/ocr_service.ts b/apps/server/src/services/ocr/ocr_service.ts
index 05113c442e..6a1885c9c1 100644
--- a/apps/server/src/services/ocr/ocr_service.ts
+++ b/apps/server/src/services/ocr/ocr_service.ts
@@ -411,12 +411,11 @@ class OCRService {
/**
- * Get blobs that need OCR processing. A NULL text representation means "never
- * processed". An empty string means "processed, found no text"; that is only
- * re-selected for PDFs, which gained the scanned-page OCR fallback and so may
- * yield text now where an earlier run stored nothing. Images and Office files
- * behave as before, so their empty results are final and left alone — otherwise
- * every text-less image would be re-OCR'd on every batch run.
+ * Get blobs that need OCR processing (those without a stored text representation).
+ * A NULL text representation means "never processed"; an empty string means
+ * "processed, found no text" and is treated as done, so a blob is never re-OCR'd
+ * on every batch run. Existing scanned PDFs stored before the OCR fallback can be
+ * re-run individually via the manual "Process OCR" action (which forces reprocess).
*/
getBlobsNeedingOCR(): Array<{ blobId: string; mimeType: string; entityType: 'note' | 'attachment'; entityId: string }> {
try {
@@ -434,7 +433,7 @@ class OCRService {
WHERE (n.type = 'image' OR (n.type = 'file' AND n.mime IN (${placeholders})))
AND n.isDeleted = 0
AND n.blobId IS NOT NULL
- AND (b.textRepresentation IS NULL OR (n.mime = 'application/pdf' AND b.textRepresentation = ''))
+ AND b.textRepresentation IS NULL
`, supportedMimes);
const attachmentBlobs = sql.getRows<{
@@ -448,7 +447,7 @@ class OCRService {
WHERE (a.role = 'image' OR (a.role = 'file' AND a.mime IN (${placeholders})))
AND a.isDeleted = 0
AND a.blobId IS NOT NULL
- AND (b.textRepresentation IS NULL OR (a.mime = 'application/pdf' AND b.textRepresentation = ''))
+ AND b.textRepresentation IS NULL
`, supportedMimes);
// Combine results