The minimalistic document archiving platform.
feat(search): add `has:tags` filter to check for document tags presence or absence (#785)
Corentin Thomasset45534a0ce729
diff --git a/.changeset/plenty-chairs-send.md b/.changeset/plenty-chairs-send.mdnew file mode 100644index 00000000..8171b6be--- /dev/null+++ b/.changeset/plenty-chairs-send.md@@ -0,0 +1,5 @@+---+"@papra/docker": patch+---++Added the `has:tags` search filter to check for the presence or absence (`-has:tags` or `NOT has:tags`) of tags on documents.diff --git a/apps/docs/src/content/docs/03-guides/08-advanced-search.mdx b/apps/docs/src/content/docs/03-guides/08-advanced-search.mdxindex 3ab15723..6d02cc3e 100644--- a/apps/docs/src/content/docs/03-guides/08-advanced-search.mdx+++ b/apps/docs/src/content/docs/03-guides/08-advanced-search.mdx@@ -33,6 +33,7 @@ Use filters to search specific document properties. Current supported filters:- `content:` - Search in document content- `tag:` - Search by tag name or ID- `created:` - Filter by document creation date+- `has:tags` - Check if documents have any tags, it can be negated with `-has:tags` or `NOT has:tags` to find untagged documents### Filter Examples@@ -49,6 +50,12 @@ tag:urgent# Finds documents created after January 1st, 2024created:>2024++# Finds documents that have at least one tag+has:tags++# Finds documents without any tags+-has:tags```@@ -56,6 +63,29 @@ created:>2024The `name:` and `content:` filters use **contains** matching, not exact equality. So `name:foo` will match documents named "foo", "foobar", "my-foo-document", etc.:::+### Existence Filter++The `has:` filter checks whether documents have certain properties:++- `has:tags` - Documents with at least one tag+- `-has:tags` or `NOT has:tags` - Documents without any tags++#### Existence Filter Examples++```yaml+# Find all untagged documents+-has:tags++# Find tagged invoices+has:tags invoice++# Find untagged documents from 2024+-has:tags created:>=2024++# Find tagged documents that are not archived+has:tags NOT tag:archived+```+### Date FilterThe `created:` filter supports date comparisons with the following operators:diff --git a/apps/papra-server/src/modules/documents/document-search/database-fts5/database-fts5.repository.test.ts b/apps/papra-server/src/modules/documents/document-search/database-fts5/database-fts5.repository.test.tsindex 5808793a..09479e4f 100644--- a/apps/papra-server/src/modules/documents/document-search/database-fts5/database-fts5.repository.test.ts+++ b/apps/papra-server/src/modules/documents/document-search/database-fts5/database-fts5.repository.test.ts@@ -325,6 +325,26 @@ describe('database-fts5 repository', () => {searchQuery: 'created:>=2026-01-12 created:<=2026-01-13',expectedDocumentsIds: ['doc_3', 'doc_4'],},+ {+ searchQuery: 'has:tags',+ expectedDocumentsIds: ['doc_1', 'doc_2', 'doc_3', 'doc_4'],+ },+ {+ searchQuery: '-has:tags',+ expectedDocumentsIds: ['doc_5'],+ },+ {+ searchQuery: 'NOT has:tags',+ expectedDocumentsIds: ['doc_5'],+ },+ {+ searchQuery: '-has:tags invoice',+ expectedDocumentsIds: [],+ },+ {+ searchQuery: 'has:tags created:>=2026-01-12',+ expectedDocumentsIds: ['doc_3', 'doc_4'],+ },];for (const { searchQuery, expectedDocumentsIds } of searches) {diff --git a/apps/papra-server/src/modules/documents/document-search/database-fts5/query-builder/query-builder.test.ts b/apps/papra-server/src/modules/documents/document-search/database-fts5/query-builder/query-builder.test.tsindex 3f10a5b3..874aad5b 100644--- a/apps/papra-server/src/modules/documents/document-search/database-fts5/query-builder/query-builder.test.ts+++ b/apps/papra-server/src/modules/documents/document-search/database-fts5/query-builder/query-builder.test.ts@@ -2,7 +2,18 @@ import type { SQL } from 'drizzle-orm';import { SQLiteSyncDialect } from 'drizzle-orm/sqlite-core';import { describe, expect, test } from 'vitest';import { createInMemoryDatabase } from '../../../../app/database/database.test-utils';-import { handleAndExpression, handleContentFilter, handleCreatedFilter, handleEmptyExpression, handleNameFilter, handleNotExpression, handleOrExpression, handleTextExpression, handleUnsupportedExpression } from './query-builder';+import {+ handleAndExpression,+ handleContentFilter,+ handleCreatedFilter,+ handleEmptyExpression,+ handleHasTagsFilter,+ handleNameFilter,+ handleNotExpression,+ handleOrExpression,+ handleTextExpression,+ handleUnsupportedExpression,+} from './query-builder';describe('query-builder', async () => {const { db } = await createInMemoryDatabase();@@ -438,6 +449,38 @@ describe('query-builder', async () => {});});+ describe('handleHasTagsFilter', () => {+ test('builds query to find documents with tags', () => {+ const { sqlQuery, issues } = handleHasTagsFilter({+ expression: { type: 'filter', field: 'has', value: 'tags', operator: '=' },+ organizationId: 'org_1',+ db,+ });++ expect(issues).to.eql([]);++ expect(getSqlString(sqlQuery)).to.eql({+ sql: `"documents"."id" in (select distinct "documents_tags"."document_id" from "documents_tags" inner join "tags" on "documents_tags"."tag_id" = "tags"."id" where "tags"."organization_id" = ?)`,+ params: ['org_1'],+ });+ });++ test('just the equality operator is supported ("=") for has filters, other operators return an issue and a no-op SQL query that evaluates to false', () => {+ const { sqlQuery, issues } = handleHasTagsFilter({+ expression: { type: 'filter', field: 'has', value: 'tags', operator: '>' },+ organizationId: 'org_1',+ db,+ });++ expect(issues).to.eql([{+ message: 'Unsupported operator ">" for has filter',+ code: 'UNSUPPORTED_FILTER_OPERATOR',+ }]);++ expect(getSqlString(sqlQuery)).to.eql({ sql: `0`, params: [] });+ });+ });+describe('handleUnsupportedExpression', () => {test('returns error for unsupported expression types', () => {const { sqlQuery, issues } = handleUnsupportedExpression();diff --git a/apps/papra-server/src/modules/documents/document-search/database-fts5/query-builder/query-builder.ts b/apps/papra-server/src/modules/documents/document-search/database-fts5/query-builder/query-builder.tsindex 15528bfb..17f55e97 100644--- a/apps/papra-server/src/modules/documents/document-search/database-fts5/query-builder/query-builder.ts+++ b/apps/papra-server/src/modules/documents/document-search/database-fts5/query-builder/query-builder.ts@@ -152,6 +152,28 @@ export function handleTagFilter({ expression, organizationId, db }: { expression};}+export function handleHasTagsFilter({ expression, organizationId, db }: { expression: FilterExpression; organizationId: string; db: Database }): QueryResult {+ const { operator, field } = expression;++ if (operator !== '=') {+ return {+ sqlQuery: sql`0`,+ issues: [createUnsupportedOperatorIssue({ operator, field })],+ };+ }++ return {+ sqlQuery: inArray(+ documentsTable.id,+ db.selectDistinct({ documentId: documentsTagsTable.documentId })+ .from(documentsTagsTable)+ .innerJoin(tagsTable, eq(documentsTagsTable.tagId, tagsTable.id))+ .where(eq(tagsTable.organizationId, organizationId)),+ ),+ issues: [],+ };+}+export function handleCreatedFilter({ expression }: { expression: FilterExpression }): QueryResult {const { value, operator } = expression;@@ -189,6 +211,18 @@ export function handleUnsupportedExpression(): QueryResult {};}+export function handleInvalidHasValue({ expression }: { expression: FilterExpression }): QueryResult {+ return {+ sqlQuery: sql`0`,+ issues: [+ {+ message: `Unsupported value "${expression.value}" for has filter`,+ code: 'UNSUPPORTED_HAS_VALUE',+ },+ ],+ };+}+export function buildQueryFromExpression({ expression, organizationId, db }: { expression: Expression; organizationId: string; db: Database }): QueryResult {// I usually prefer a LUT, but for type narrowing, switch is more convenient and avoids castingswitch (expression.type) {@@ -203,6 +237,11 @@ export function buildQueryFromExpression({ expression, organizationId, db }: { ecase 'name': return handleNameFilter({ expression, organizationId, db });case 'content': return handleContentFilter({ expression, organizationId, db });case 'created': return handleCreatedFilter({ expression });+ case 'has':+ switch (expression.value) {+ case 'tags': return handleHasTagsFilter({ expression, organizationId, db });+ default: return handleInvalidHasValue({ expression });+ }default: return handleUnsupportedExpression();}default: return handleUnsupportedExpression();