mirror of
https://github.com/vercel/commerce.git
synced 2025-05-19 16:07:01 +00:00
implements Orama sorting API
This commit is contained in:
parent
0d801c1ea1
commit
fdf0dbad66
@ -1,6 +1,6 @@
|
|||||||
import Grid from 'components/grid';
|
import Grid from 'components/grid';
|
||||||
import ProductGridItems from 'components/layout/product-grid-items';
|
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';
|
import { Product } from 'lib/shopify/types';
|
||||||
|
|
||||||
export const runtime = 'edge';
|
export const runtime = 'edge';
|
||||||
@ -16,12 +16,13 @@ export default async function SearchPage({
|
|||||||
searchParams?: { [key: string]: string | string[] | undefined };
|
searchParams?: { [key: string]: string | string[] | undefined };
|
||||||
}) {
|
}) {
|
||||||
const { sort, q: searchValue } = searchParams as { [key: string]: string };
|
const { sort, q: searchValue } = searchParams as { [key: string]: string };
|
||||||
// const { sortKey, reverse } = sorting.find((item) => item.slug === sort) || defaultSort;
|
|
||||||
const products = await orama.search({
|
const products = await orama.search({
|
||||||
term: searchValue,
|
term: searchValue,
|
||||||
boost: {
|
boost: {
|
||||||
title: 2
|
title: 2
|
||||||
},
|
},
|
||||||
|
sortBy: parseSorting(sort),
|
||||||
limit: 50,
|
limit: 50,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import type { SorterParams } from '@orama/orama'
|
||||||
import { OramaClient } from '@oramacloud/client'
|
import { OramaClient } from '@oramacloud/client'
|
||||||
|
|
||||||
const ORAMA_API_KEY = process.env.NEXT_PUBLIC_ORAMA_API_KEY!
|
const ORAMA_API_KEY = process.env.NEXT_PUBLIC_ORAMA_API_KEY!
|
||||||
@ -14,3 +15,20 @@ export function trimDescription(description: string, maxSize = 80) {
|
|||||||
}
|
}
|
||||||
return description
|
return description
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function parseSorting(sorting: string | undefined): SorterParams<any> | undefined {
|
||||||
|
switch (sorting) {
|
||||||
|
case 'price-asc':
|
||||||
|
return {
|
||||||
|
property: 'priceRange.max',
|
||||||
|
order: 'ASC'
|
||||||
|
}
|
||||||
|
case 'price-desc':
|
||||||
|
return {
|
||||||
|
property: 'priceRange.max',
|
||||||
|
order: 'DESC'
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user