Searching.ton
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

37 lines
1.1 KiB

import { NotFoundError } from "blitz"
import { resolver } from "@blitzjs/rpc"
import db from "db"
import { z } from "zod"
import Elastic from "services/modules/elastic"
import { SEARCH_PER_PAGE } from "services/commonConstants"
import upsertSearchRequest from "../mutations/upsertSearchRequest"
const GetSearchRequest = z.object({
// This accepts type of undefined, but is required at runtime
text: z.string(),
page: z.number(),
})
const bodyToDescription = (text: string, search) => {
const reg = new RegExp(`(${search}.{100})`)
const justText = new RegExp(`(.{100})`)
return reg.exec(text)?.[0] || justText.exec(text)?.[0]
}
const processResult = ({ bodyText, ...res }: Object, search: string) => {
return {
...res,
description: res.description || bodyToDescription(bodyText, search),
}
}
export default resolver.pipe(resolver.zod(GetSearchRequest), async ({ text, page }, c) => {
upsertSearchRequest({ text }, c).catch(console.log)
const result = await Elastic.search({ text, page })
return {
hits: result.hits.map((i) => processResult(i._source, text)),
pagesCount: Math.ceil(result.total / SEARCH_PER_PAGE),
}
})