From fdf0dbad66c46f700e5a4fc692a562704d414bbd Mon Sep 17 00:00:00 2001 From: Michele Riva Date: Mon, 16 Oct 2023 11:43:59 +0200 Subject: [PATCH] implements Orama sorting API --- app/search/page.tsx | 5 +++-- lib/orama/index.ts | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/app/search/page.tsx b/app/search/page.tsx index 34eb55747..9690ed595 100644 --- a/app/search/page.tsx +++ b/app/search/page.tsx @@ -1,6 +1,6 @@ import Grid from 'components/grid'; import ProductGridItems from 'components/layout/product-grid-items'; -import { orama } from 'lib/orama'; +import { orama, parseSorting } from 'lib/orama'; import { Product } from 'lib/shopify/types'; export const runtime = 'edge'; @@ -16,12 +16,13 @@ export default async function SearchPage({ searchParams?: { [key: string]: string | string[] | undefined }; }) { const { sort, q: searchValue } = searchParams as { [key: string]: string }; - // const { sortKey, reverse } = sorting.find((item) => item.slug === sort) || defaultSort; + const products = await orama.search({ term: searchValue, boost: { title: 2 }, + sortBy: parseSorting(sort), limit: 50, }) diff --git a/lib/orama/index.ts b/lib/orama/index.ts index 6356660d4..0f8838d8f 100644 --- a/lib/orama/index.ts +++ b/lib/orama/index.ts @@ -1,3 +1,4 @@ +import type { SorterParams } from '@orama/orama' import { OramaClient } from '@oramacloud/client' const ORAMA_API_KEY = process.env.NEXT_PUBLIC_ORAMA_API_KEY! @@ -13,4 +14,21 @@ export function trimDescription(description: string, maxSize = 80) { return `${description.substring(0, maxSize)}...` } return description +} + +export function parseSorting(sorting: string | undefined): SorterParams | undefined { + switch (sorting) { + case 'price-asc': + return { + property: 'priceRange.max', + order: 'ASC' + } + case 'price-desc': + return { + property: 'priceRange.max', + order: 'DESC' + } + default: + return undefined + } } \ No newline at end of file