From 8918a040b65b817139c8243ed275e4e427a45c35 Mon Sep 17 00:00:00 2001 From: Thad Miller Date: Tue, 17 Feb 2026 13:42:00 -0500 Subject: [PATCH] [feat] moved search to components --- scripts/preload-searchindex.ts | 25 +++++++++++++++---------- src/components/Card.astro | 2 +- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/scripts/preload-searchindex.ts b/scripts/preload-searchindex.ts index 8208c06..a93079d 100644 --- a/scripts/preload-searchindex.ts +++ b/scripts/preload-searchindex.ts @@ -5,6 +5,15 @@ import { client } from '../src/db/typesense.ts'; async function createCollection(client: Client) { + // Delete the collection if it already exists to ensure a clean slate + try { + const response = await client.collections('cards').delete(); + console.log(`Collection "cards" deleted successfully:`, response); + } catch (error) { + console.error(`Error deleting collection "cards":`, error); + } + + // Create the collection with the specified schema try { await client.collections('cards').retrieve(); console.log(chalk.yellow('Collection "cards" already exists.')); @@ -15,11 +24,11 @@ async function createCollection(client: Client) { fields: [ { name: 'productId', type: 'int32' }, { name: 'productName', type: 'string' }, - { name: 'productLineName', type: 'string' }, - { name: 'rarityName', type: 'string' }, - { name: 'setName', type: 'string' }, - { name: 'cardType', type: 'string' }, - { name: 'energyType', type: 'string' }, + { name: 'productLineName', type: 'string', facet: true }, + { name: 'rarityName', type: 'string', facet: true }, + { name: 'setName', type: 'string', facet: true }, + { name: 'cardType', type: 'string', facet: true }, + { name: 'energyType', type: 'string', facet: true }, { name: 'number', type: 'string' }, ], default_sorting_field: 'productId', @@ -35,11 +44,7 @@ async function createCollection(client: Client) { async function preloadSearchIndex() { const pokemon = await db.query.cards.findMany({ - where: { productLineName: "pokemon" }, - //limit: 320, - with: { - set: true, - } + with: { set: true, } }); // Ensure the collection exists before importing documents diff --git a/src/components/Card.astro b/src/components/Card.astro index a95f59f..9eb50f2 100644 --- a/src/components/Card.astro +++ b/src/components/Card.astro @@ -12,7 +12,7 @@ const searchResults = await client.collections('cards').documents().search({ query_by: 'productLineName,productName,setName,number,rarityName', per_page: 250, }); -const productIds = searchResults.hits?.map(hit => hit.document.productId) ?? []; +const productIds = searchResults.hits?.map((hit: any) => hit.document.productId) ?? []; // get pokemon data with prices and set info using searchResults and then query the database for each card to get the prices and set info const pokemon = await db.query.cards.findMany({