35 lines
706 B
TypeScript
35 lines
706 B
TypeScript
import { Meilisearch, type SearchResponse } from 'meilisearch'
|
|
|
|
const MAX_HITS_PER_PAGE = 100
|
|
|
|
export async function createSearch<T extends Record<string, any>>({
|
|
query,
|
|
filter,
|
|
index,
|
|
page,
|
|
hitsPerPage = 25,
|
|
sort,
|
|
env
|
|
}: {
|
|
query?: string
|
|
filter?: string
|
|
index: string
|
|
page?: number
|
|
hitsPerPage?: number
|
|
sort: string[]
|
|
env: any
|
|
}): Promise<SearchResponse<T>> {
|
|
const host = env.MEILI_HOST
|
|
const apiKey = env.MEILI_API_KEY
|
|
const client = new Meilisearch({ host, apiKey })
|
|
const index_ = client.index<T>(index)
|
|
|
|
return index_.search(query, {
|
|
sort,
|
|
filter,
|
|
page,
|
|
hitsPerPage:
|
|
hitsPerPage > MAX_HITS_PER_PAGE ? MAX_HITS_PER_PAGE : hitsPerPage
|
|
})
|
|
}
|