Skip to content

Build your personal knowledge base with Trilium Notes

fix(ocr): re-process blobs whose previous OCR produced no text

Elian Doran42525ac3c554
diff --git a/apps/server/src/services/ocr/ocr_service.spec.ts b/apps/server/src/services/ocr/ocr_service.spec.ts
index af97feae35..d769c66475 100644
--- a/apps/server/src/services/ocr/ocr_service.spec.ts
+++ b/apps/server/src/services/ocr/ocr_service.spec.ts
@@ -752,6 +752,21 @@ describe('OCRService', () => {
]);
});
+ it('re-selects blobs whose previous OCR produced an empty result', () => {
+ mockSql.getRows.mockReturnValue([]);
+
+ ocrService.getBlobsNeedingOCR();
+
+ // Both the note and attachment queries must match blobs with an empty
+ // text representation (a previous run that extracted nothing), not only
+ // never-processed (NULL) ones — otherwise scanned PDFs processed before
+ // OCR fallback existed would never be picked up by a batch run.
+ expect(mockSql.getRows.mock.calls).toHaveLength(2);
+ for (const [sqlText] of mockSql.getRows.mock.calls) {
+ expect(sqlText).toContain("textRepresentation = ''");
+ }
+ });
+
it('returns empty array and logs when the query fails', () => {
mockSql.getRows.mockImplementation(() => {
throw new Error('db down');
diff --git a/apps/server/src/services/ocr/ocr_service.ts b/apps/server/src/services/ocr/ocr_service.ts
index 8b9364afdd..a61dfba9ee 100644
--- a/apps/server/src/services/ocr/ocr_service.ts
+++ b/apps/server/src/services/ocr/ocr_service.ts
@@ -411,7 +411,10 @@ class OCRService {
/**
- * Get blobs that need OCR processing (those without text representation)
+ * Get blobs that need OCR processing: those with no stored text representation
+ * yet. An empty string counts as "not processed" (matching {@link hasStoredOCRResult}),
+ * so blobs a previous run extracted nothing from — e.g. scanned PDFs processed
+ * before OCR fallback existed — are picked up again on the next batch.
*/
getBlobsNeedingOCR(): Array<{ blobId: string; mimeType: string; entityType: 'note' | 'attachment'; entityId: string }> {
try {
@@ -429,7 +432,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
+ AND (b.textRepresentation IS NULL OR b.textRepresentation = '')
`, supportedMimes);
const attachmentBlobs = sql.getRows<{
@@ -443,7 +446,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
+ AND (b.textRepresentation IS NULL OR b.textRepresentation = '')
`, supportedMimes);
// Combine results