implements Orama sorting API

This commit is contained in:
Michele Riva 2023-10-16 11:43:59 +02:00
parent 0d801c1ea1
commit fdf0dbad66
2 changed files with 21 additions and 2 deletions

View File

@ -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,
}) })

View File

@ -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!
@ -13,4 +14,21 @@ export function trimDescription(description: string, maxSize = 80) {
return `${description.substring(0, maxSize)}...` return `${description.substring(0, maxSize)}...`
} }
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
}
} }