From 1482ee148e541fd86f4947218f483f6d4177da6f Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 12 Feb 2025 16:13:44 -0600 Subject: [PATCH] Add missing indexes, fix order of results --- src/controllers/api/accounts.ts | 10 ++++++++-- src/db/migrations/049_author_stats_sorted.ts | 9 ++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/controllers/api/accounts.ts b/src/controllers/api/accounts.ts index 18433f1f..7b1b4216 100644 --- a/src/controllers/api/accounts.ts +++ b/src/controllers/api/accounts.ts @@ -112,7 +112,7 @@ const accountLookupController: AppController = async (c) => { const accountSearchQuerySchema = z.object({ q: z.string().transform(decodeURIComponent), - resolve: booleanParamSchema.optional().transform(Boolean), + resolve: booleanParamSchema.optional(), following: z.boolean().default(false), }); @@ -148,7 +148,13 @@ const accountSearchController: AppController = async (c) => { const following = viewerPubkey ? await getFollowedPubkeys(viewerPubkey) : new Set(); const authors = [...await getPubkeysBySearch(kysely, { q: query, limit, offset: 0, following })]; const profiles = await store.query([{ kinds: [0], authors, limit }], { signal }); - events.push(...profiles); + + for (const pubkey of authors) { + const profile = profiles.find((event) => event.pubkey === pubkey); + if (profile) { + events.push(profile); + } + } } const accounts = await hydrateEvents({ events, store, signal }) diff --git a/src/db/migrations/049_author_stats_sorted.ts b/src/db/migrations/049_author_stats_sorted.ts index 425cc7c8..6eca40cd 100644 --- a/src/db/migrations/049_author_stats_sorted.ts +++ b/src/db/migrations/049_author_stats_sorted.ts @@ -1,4 +1,4 @@ -import { Kysely } from 'kysely'; +import { Kysely, sql } from 'kysely'; export async function up(db: Kysely): Promise { await db.schema @@ -6,8 +6,15 @@ export async function up(db: Kysely): Promise { .materialized() .as(db.selectFrom('author_stats').select(['pubkey', 'followers_count', 'search']).orderBy('followers_count desc')) .execute(); + + await sql`CREATE INDEX top_authors_search_idx ON top_authors USING GIN (search gin_trgm_ops)`.execute(db); + + await db.schema.createIndex('top_authors_pubkey_idx').on('top_authors').column('pubkey').execute(); + + await db.schema.dropIndex('author_stats_search_idx').execute(); } export async function down(db: Kysely): Promise { await db.schema.dropView('top_authors').execute(); + await sql`CREATE INDEX author_stats_search_idx ON author_stats USING GIN (search gin_trgm_ops)`.execute(db); }