[feat] tweaking sort and facet matching

This commit is contained in:
2026-02-27 09:29:47 -05:00
parent 538416c22a
commit 2a827fd4f6
3 changed files with 20 additions and 6 deletions

View File

@@ -2,6 +2,7 @@ import { Client } from 'typesense';
import chalk from 'chalk';
import { db, poolConnection } from '../src/db/index.ts';
import { client } from '../src/db/typesense.ts';
import { release } from 'node:os';
async function createCollection(client: Client) {
@@ -22,9 +23,10 @@ async function createCollection(client: Client) {
await client.collections().create({
name: 'cards',
fields: [
{ name: 'cardId', type: 'int32' },
{ name: 'productId', type: 'int32' },
{ name: 'variant', type: 'string' },
{ name: 'productName', type: 'string' },
{ name: 'variant', type: 'string', facet: true },
{ name: 'productName', type: 'string', sort: true },
{ name: 'productLineName', type: 'string', facet: true },
{ name: 'rarityName', type: 'string', facet: true },
{ name: 'setName', type: 'string', facet: true },
@@ -33,6 +35,7 @@ async function createCollection(client: Client) {
{ name: 'number', type: 'string' },
{ name: 'Artist', type: 'string' },
{ name: 'sealed', type: 'bool' },
{ name: 'releaseDate', type: 'int32'},
],
//default_sorting_field: 'productId',
});
@@ -47,13 +50,14 @@ async function createCollection(client: Client) {
async function preloadSearchIndex() {
const pokemon = await db.query.cards.findMany({
with: { set: true, }
with: { set: true, tcgdata: true },
});
// Ensure the collection exists before importing documents
await createCollection(client);
await client.collections('cards').documents().import(pokemon.map(card => ({
cardId: card.cardId,
productId: card.productId,
variant: card.variant,
productName: card.productName,
@@ -65,6 +69,7 @@ async function preloadSearchIndex() {
number: card.number,
Artist: card.Artist || "",
sealed: card.sealed,
releaseDate: card.tcgdata?.releaseDate ? Math.floor(new Date(card.tcgdata.releaseDate).getTime() / 1000) : 0,
})), { action: 'upsert' });
console.log(chalk.green('Search index preloaded with Pokémon cards.'));

View File

@@ -14,9 +14,16 @@ export const relations = defineRelations(schema, (r) => ({
from: r.cards.setId,
to: r.sets.setId,
}),
tcgdata: r.one.tcgcards({
from: r.cards.productId,
to: r.tcgcards.productId,
}),
},
sets: {
cards: r.many.cards(),
},
tcgcards: {
cards: r.many.cards(),
},
}));

View File

@@ -33,16 +33,17 @@ const searchResults = await client.collections('cards').documents().search({
filter_by: `sealed:false${filterBy ? ` && ${filterBy}` : ''}`,
query_by: 'productLineName,productName,setName,number,rarityName,Artist',
per_page: 20,
facet_by: 'productLineName,setName,rarityName,cardType,energyType',
facet_by: 'productLineName,setName,variant,rarityName,cardType,energyType',
page: Math.floor(start / 20) + 1,
sort_by: '_text_match:asc, releaseDate:desc, productName:asc',
});
const productIds = searchResults.hits?.map((hit: any) => hit.document.productId) ?? [];
const cardIds = searchResults.hits?.map((hit: any) => hit.document.cardId) ?? [];
const totalHits = searchResults.found;
const facets = searchResults.facet_counts;
// 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({
where: { productId: { in: productIds, }, },
where: { cardId: { in: cardIds, }, },
with: {
prices: true,
set: true,
@@ -74,6 +75,7 @@ const facetNames = (name:string) => {
return {
"productLineName": "Product Line",
"setName": "Set",
"variant": "Variant",
"rarityName": "Rarity",
"cardType": "Card Type",
"energyType": "Energy Type"