Starting to work on search combined.

This commit is contained in:
Dessalines 2024-12-12 19:13:31 -05:00
parent 17d2de8691
commit dada964df7
6 changed files with 120 additions and 0 deletions

View file

@ -868,3 +868,35 @@ CALL r.create_inbox_combined_trigger ('person_post_mention');
CALL r.create_inbox_combined_trigger ('private_message'); CALL r.create_inbox_combined_trigger ('private_message');
-- search: (post, comment, community, person)
CREATE PROCEDURE r.create_search_combined_trigger (table_name text)
LANGUAGE plpgsql
AS $a$
BEGIN
EXECUTE replace($b$ CREATE FUNCTION r.search_combined_thing_insert ( )
RETURNS TRIGGER
LANGUAGE plpgsql
AS $$
BEGIN
-- TODO need to figure out how to do the other columns here
INSERT INTO search_combined (published, thing_id)
VALUES (NEW.published, NEW.id);
RETURN NEW;
END $$;
CREATE TRIGGER search_combined
AFTER INSERT ON thing
FOR EACH ROW
EXECUTE FUNCTION r.search_combined_thing_insert ( );
$b$,
'thing',
table_name);
END;
$a$;
CALL r.create_search_combined_trigger ('post');
CALL r.create_search_combined_trigger ('comment');
CALL r.create_search_combined_trigger ('community');
CALL r.create_search_combined_trigger ('person');

View file

@ -216,6 +216,12 @@ pub struct ModlogCombinedId(i32);
/// The inbox combined id /// The inbox combined id
pub struct InboxCombinedId(i32); pub struct InboxCombinedId(i32);
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
#[cfg_attr(feature = "full", derive(DieselNewType, TS))]
#[cfg_attr(feature = "full", ts(export))]
/// The search combined id
pub struct SearchCombinedId(i32);
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)] #[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)]
#[cfg_attr(feature = "full", derive(DieselNewType, TS))] #[cfg_attr(feature = "full", derive(DieselNewType, TS))]
#[cfg_attr(feature = "full", ts(export))] #[cfg_attr(feature = "full", ts(export))]

View file

@ -955,6 +955,17 @@ diesel::table! {
} }
} }
diesel::table! {
search_combined (id) {
id -> Int4,
published -> Timestamptz,
post_id -> Nullable<Int4>,
comment_id -> Nullable<Int4>,
community_id -> Nullable<Int4>,
person_id -> Nullable<Int4>,
}
}
diesel::table! { diesel::table! {
secret (id) { secret (id) {
id -> Int4, id -> Int4,
@ -1140,6 +1151,10 @@ diesel::joinable!(registration_application -> person (admin_id));
diesel::joinable!(report_combined -> comment_report (comment_report_id)); diesel::joinable!(report_combined -> comment_report (comment_report_id));
diesel::joinable!(report_combined -> post_report (post_report_id)); diesel::joinable!(report_combined -> post_report (post_report_id));
diesel::joinable!(report_combined -> private_message_report (private_message_report_id)); diesel::joinable!(report_combined -> private_message_report (private_message_report_id));
diesel::joinable!(search_combined -> comment (comment_id));
diesel::joinable!(search_combined -> community (community_id));
diesel::joinable!(search_combined -> person (person_id));
diesel::joinable!(search_combined -> post (post_id));
diesel::joinable!(site -> instance (instance_id)); diesel::joinable!(site -> instance (instance_id));
diesel::joinable!(site_aggregates -> site (site_id)); diesel::joinable!(site_aggregates -> site (site_id));
diesel::joinable!(site_language -> language (language_id)); diesel::joinable!(site_language -> language (language_id));
@ -1214,6 +1229,7 @@ diesel::allow_tables_to_appear_in_same_query!(
registration_application, registration_application,
remote_image, remote_image,
report_combined, report_combined,
search_combined,
secret, secret,
sent_activity, sent_activity,
site, site,

View file

@ -3,3 +3,4 @@ pub mod modlog;
pub mod person_content; pub mod person_content;
pub mod person_saved; pub mod person_saved;
pub mod report; pub mod report;
pub mod search;

View file

@ -0,0 +1 @@
DROP TABLE search_combined;

View file

@ -0,0 +1,64 @@
-- Creates combined tables for
-- Search: (post, comment, community, person)
CREATE TABLE search_combined (
id serial PRIMARY KEY,
published timestamptz NOT NULL,
-- TODO Need to figure out all the possible sort types, unified into SearchSortType
-- This is difficult because other than published, there is no unified way to sort them.
--
-- All have published.
-- post and comment have top and time-limited scores and ranks.
-- persons have post and comment counts, and scores (not time-limited).
-- communities have subscribers, post and comment counts, and active users per X time.
--
-- I'm thinking just published and score (and use active_monthly users as the community score), is the best way to start.
post_id int UNIQUE REFERENCES post ON UPDATE CASCADE ON DELETE CASCADE,
comment_id int UNIQUE REFERENCES comment ON UPDATE CASCADE ON DELETE CASCADE,
community_id int UNIQUE REFERENCES community ON UPDATE CASCADE ON DELETE CASCADE,
person_id int UNIQUE REFERENCES person ON UPDATE CASCADE ON DELETE CASCADE,
-- Make sure only one of the columns is not null
CHECK (num_nonnulls (post_id, comment_id, community_id, person_id) = 1)
);
CREATE INDEX idx_search_combined_published ON search_combined (published DESC, id DESC);
CREATE INDEX idx_search_combined_published_asc ON search_combined (reverse_timestamp_sort (published) DESC, id DESC);
-- Updating the history
INSERT INTO search_combined (published, post_id, comment_id, community_id, person_id)
SELECT
published,
id,
NULL::int,
NULL::int,
NULL::int
FROM
post
UNION ALL
SELECT
published,
NULL::int,
id,
NULL::int,
NULL::int
FROM
comment
UNION ALL
SELECT
published,
NULL::int,
NULL::int,
id,
NULL::int
FROM
community
UNION ALL
SELECT
published,
NULL::int,
NULL::int,
NULL::int,
id
FROM
person;