feat: refined search
This commit is contained in:
parent
a141dd5ce0
commit
4893256340
5 changed files with 20 additions and 10 deletions
4
package-lock.json
generated
4
package-lock.json
generated
|
@ -1,12 +1,12 @@
|
||||||
{
|
{
|
||||||
"name": "coryd.dev",
|
"name": "coryd.dev",
|
||||||
"version": "1.5.20",
|
"version": "1.6.0",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "coryd.dev",
|
"name": "coryd.dev",
|
||||||
"version": "1.5.20",
|
"version": "1.6.0",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@cdransf/api-text": "^1.5.0",
|
"@cdransf/api-text": "^1.5.0",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "coryd.dev",
|
"name": "coryd.dev",
|
||||||
"version": "1.5.20",
|
"version": "1.6.0",
|
||||||
"description": "The source for my personal site. Built using 11ty (and other tools).",
|
"description": "The source for my personal site. Built using 11ty (and other tools).",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"engines": {
|
"engines": {
|
||||||
|
|
|
@ -13,7 +13,8 @@ CREATE OR REPLACE FUNCTION public.search_optimized_index(
|
||||||
genre_url text,
|
genre_url text,
|
||||||
type text,
|
type text,
|
||||||
total_plays integer,
|
total_plays integer,
|
||||||
rank real
|
rank real,
|
||||||
|
total_count bigint
|
||||||
) AS $$
|
) AS $$
|
||||||
BEGIN
|
BEGIN
|
||||||
RETURN QUERY
|
RETURN QUERY
|
||||||
|
@ -30,13 +31,17 @@ BEGIN
|
||||||
ts_rank_cd(
|
ts_rank_cd(
|
||||||
to_tsvector('english', s.title || ' ' || s.description || array_to_string(s.tags, ' ')),
|
to_tsvector('english', s.title || ' ' || s.description || array_to_string(s.tags, ' ')),
|
||||||
plainto_tsquery('english', search_query)
|
plainto_tsquery('english', search_query)
|
||||||
) AS rank
|
) AS rank,
|
||||||
|
COUNT(*) OVER () AS total_count
|
||||||
FROM optimized_search_index s
|
FROM optimized_search_index s
|
||||||
WHERE
|
WHERE
|
||||||
(types IS NULL OR s.type = ANY(types))
|
(types IS NULL OR s.type = ANY(types))
|
||||||
AND plainto_tsquery('english', search_query) @@
|
AND plainto_tsquery('english', search_query) @@
|
||||||
to_tsvector('english', s.title || ' ' || s.description || array_to_string(s.tags, ' '))
|
to_tsvector('english', s.title || ' ' || s.description || array_to_string(s.tags, ' '))
|
||||||
ORDER BY rank DESC
|
ORDER BY
|
||||||
|
s.type = 'post' DESC,
|
||||||
|
s.content_date DESC NULLS LAST,
|
||||||
|
rank DESC
|
||||||
LIMIT page_size OFFSET page_offset;
|
LIMIT page_size OFFSET page_offset;
|
||||||
END;
|
END;
|
||||||
$$ LANGUAGE plpgsql;
|
$$ LANGUAGE plpgsql;
|
|
@ -131,7 +131,7 @@ window.addEventListener("load", () => {
|
||||||
let currentPage = 1;
|
let currentPage = 1;
|
||||||
let currentResults = [];
|
let currentResults = [];
|
||||||
let total = 0;
|
let total = 0;
|
||||||
let debounceTimeout; // Declare debounceTimeout here
|
let debounceTimeout;
|
||||||
|
|
||||||
const parseMarkdown = (markdown) =>
|
const parseMarkdown = (markdown) =>
|
||||||
markdown
|
markdown
|
||||||
|
|
|
@ -25,10 +25,15 @@ export default {
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
console.error("Error fetching search data:", error);
|
console.error("Error fetching search data:", error);
|
||||||
return new Response(JSON.stringify({ results: [] }), { status: 500 });
|
return new Response(JSON.stringify({ results: [], total: 0 }), {
|
||||||
|
status: 500,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Response(JSON.stringify({ results: data }), {
|
const total = data.length > 0 ? data[0].total_count : 0;
|
||||||
|
const results = data.map(({ total_count, ...item }) => item);
|
||||||
|
|
||||||
|
return new Response(JSON.stringify({ results, total, page, pageSize }), {
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -36,4 +41,4 @@ export default {
|
||||||
return new Response("Internal Server Error", { status: 500 });
|
return new Response("Internal Server Error", { status: 500 });
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
Reference in a new issue