-
Notifications
You must be signed in to change notification settings - Fork 469
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5646a38
commit 11725ee
Showing
3 changed files
with
94 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { db } from "@/db"; | ||
import { categories, products, subcategories, subcollection } from "@/db/schema"; | ||
import { sql } from "drizzle-orm"; | ||
import { NextRequest } from "next/server"; | ||
|
||
export async function GET(request: NextRequest) { | ||
// format is /api/search?q=term | ||
const searchTerm = request.nextUrl.searchParams.get("q"); | ||
if (!searchTerm) { | ||
return []; | ||
} | ||
|
||
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 searchResults: ProductSearchResult = results.map((item) => { | ||
const href = `/products/${item.categories.slug}/${item.subcategories.slug}/${item.products.slug}`; | ||
return { | ||
...item.products, | ||
href, | ||
}; | ||
}); | ||
const response = Response.json(searchResults); | ||
// cache for 10 minutes | ||
response.headers.set("Cache-Control", "public, max-age=600"); | ||
return response; | ||
} | ||
|
||
export type ProductSearchResult = { | ||
href: string; | ||
name: string; | ||
slug: string; | ||
image_url: string | null; | ||
description: string; | ||
price: string; | ||
subcategory_slug: string; | ||
}[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters