Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanniser committed Oct 21, 2024
1 parent 68b11e5 commit 341950b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 54 deletions.
68 changes: 18 additions & 50 deletions src/app/api/search/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
subcategories,
subcollection,
} from "@/db/schema";
import { sql } from "drizzle-orm";
import { ilike, sql } from "drizzle-orm";
import { NextRequest } from "next/server";

export async function GET(request: NextRequest) {
Expand All @@ -15,55 +15,23 @@ export async function GET(request: NextRequest) {
return Response.json([]);
}

let results;

if (searchTerm.length <= 2) {
// If the search term is short (e.g., "W"), use ILIKE for prefix matching
results = await db
.select()
.from(products)
.where(sql`${products.name} ILIKE ${searchTerm + "%"}`) // Prefix match
.limit(5)
.innerJoin(
subcategories,
sql`${products.subcategory_slug} = ${subcategories.slug}`,
)
.innerJoin(
subcollection,
sql`${subcategories.subcollection_id} = ${subcollection.id}`,
)
.innerJoin(
categories,
sql`${subcollection.category_slug} = ${categories.slug}`,
);
} else {
// For longer search terms, use full-text search with tsquery
const formattedSearchTerm = searchTerm
.split(" ")
.filter((term) => term.trim() !== "") // Filter out empty terms
.map((term) => `${term}:*`)
.join(" & ");

results = await db
.select()
.from(products)
.where(
sql`to_tsvector('english', ${products.name}) @@ to_tsquery('english', ${formattedSearchTerm})`,
)
.limit(5)
.innerJoin(
subcategories,
sql`${products.subcategory_slug} = ${subcategories.slug}`,
)
.innerJoin(
subcollection,
sql`${subcategories.subcollection_id} = ${subcollection.id}`,
)
.innerJoin(
categories,
sql`${subcollection.category_slug} = ${categories.slug}`,
);
}
const results = await db
.select()
.from(products)
.where(ilike(products.name, `%${searchTerm}%`))
.limit(5)
.innerJoin(
subcategories,
sql`${products.subcategory_slug} = ${subcategories.slug}`,
)
.innerJoin(
subcollection,
sql`${subcategories.subcollection_id} = ${subcollection.id}`,
)
.innerJoin(
categories,
sql`${subcollection.category_slug} = ${categories.slug}`,
);

const searchResults: ProductSearchResult = results.map((item) => {
const href = `/products/${item.categories.slug}/${item.subcategories.slug}/${item.products.slug}`;
Expand Down
7 changes: 3 additions & 4 deletions src/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,9 @@ export const products = pgTable(
image_url: text("image_url"),
},
(table) => ({
nameSearchIndex: index("name_search_index").using(
"gin",
sql`to_tsvector('english', ${table.name})`,
),
nameSearchIndex: index("name_search_index")
.using("gin", sql`${table.name}`, sql`gi`, sql`n_trgm_ops`)
.concurrently(),
}),
);

Expand Down

0 comments on commit 341950b

Please sign in to comment.