[feat] tweaking sort and facet matching
This commit is contained in:
@@ -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.'));
|
||||
|
||||
Reference in New Issue
Block a user