Skip to content

A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search

perf: use tag-first lookup for positive tag searches (#3089)

Masa21b0fa8a4f7c
diff --git a/packages/trpc/lib/search.ts b/packages/trpc/lib/search.ts
index d0f529f5..4708f67a 100644
--- a/packages/trpc/lib/search.ts
+++ b/packages/trpc/lib/search.ts
@@ -100,6 +100,23 @@ async function getIds(
switch (matcher.type) {
case "tagName": {
+ if (!matcher.inverse) {
+ return db
+ .selectDistinct({ id: bookmarks.id })
+ .from(bookmarkTags)
+ .crossJoin(tagsOnBookmarks)
+ .crossJoin(bookmarks)
+ .where(
+ and(
+ eq(bookmarkTags.userId, userId),
+ eq(bookmarkTags.name, matcher.tagName),
+ eq(tagsOnBookmarks.tagId, bookmarkTags.id),
+ eq(bookmarks.id, tagsOnBookmarks.bookmarkId),
+ eq(bookmarks.userId, userId),
+ ),
+ );
+ }
+
const comp = matcher.inverse ? notExists : exists;
return db
.selectDistinct({ id: bookmarks.id })