web3.news/app/routes/api.search.ts

41 lines
940 B
TypeScript
Raw Normal View History

2023-12-23 10:51:12 +00:00
import { type LoaderFunctionArgs } from "@remix-run/node";
import search from "@/lib/search.js";
export async function loader({ request }: LoaderFunctionArgs) {
const searchParams = new URL(request.url).searchParams;
const query = searchParams.get("q");
if (!query || !query.length) {
throw new Response("Invalid query", {
status: 400,
});
}
2023-12-23 11:42:46 +00:00
const site = searchParams.get("site");
2023-12-23 12:10:18 +00:00
const time = searchParams.get("time");
let filters = [];
if (site) {
filters.push(`site = ${site}`);
2023-12-23 12:10:18 +00:00
}
if (time) {
filters.push(`createdTimestamp >= ${parseInt(time).toString()}`);
}
2023-12-23 11:42:46 +00:00
const results = await search.index("articles").search(query, {
2023-12-23 12:10:18 +00:00
filter: filters.length ? filters.join(" AND ") : undefined,
2023-12-23 11:42:46 +00:00
});
2023-12-23 10:51:12 +00:00
return results.hits.map((item) => {
return {
id: item.id,
timestamp: item.createdAt,
title: item.title,
description: "",
slug: item.slug,
};
});
}