From 8023119ebdf44d114657307b6301d169636cdd8e Mon Sep 17 00:00:00 2001 From: Hung Ta Date: Tue, 10 Dec 2024 10:35:19 +0700 Subject: [PATCH 1/2] [f] 248 - Related snippets update --- .../database/sql/search_related_snippets.sql | 27 +++++++------------ 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/supabase/database/sql/search_related_snippets.sql b/supabase/database/sql/search_related_snippets.sql index 429eabd..845edde 100644 --- a/supabase/database/sql/search_related_snippets.sql +++ b/supabase/database/sql/search_related_snippets.sql @@ -1,7 +1,8 @@ CREATE OR REPLACE FUNCTION search_related_snippets( snippet_id uuid, + p_language TEXT DEFAULT 'english', match_threshold float DEFAULT 0.7, - match_count int DEFAULT 10 + match_count int DEFAULT 3 ) RETURNS jsonb SECURITY DEFINER AS $$ @@ -31,8 +32,12 @@ BEGIN s.id, s.title, a.radio_station_name, + a.radio_station_code, a.location_state, - s.summary, + CASE + WHEN p_language = 'spanish' THEN s.summary ->> 'spanish' + ELSE s.summary ->> 'english' + END AS summary, s.recorded_at, s.comment_count, 1 - (se.embedding <=> source_embedding) as similarity @@ -40,7 +45,7 @@ BEGIN JOIN snippets s ON s.id = se.snippet JOIN audio_files a ON a.id = s.audio_file WHERE - se.snippet != snippet_id -- Exclude the source snippet + se.snippet != snippet_id AND 1 - (se.embedding <=> source_embedding) > match_threshold ORDER BY se.embedding <=> source_embedding LIMIT match_count @@ -50,22 +55,10 @@ BEGIN 'id', ss.id, 'title', ss.title, 'radio_station_name', ss.radio_station_name, + 'radio_station_code', ss.radio_station_code, 'location_state', ss.location_state, 'summary', ss.summary, - 'labels', COALESCE( - ( - SELECT jsonb_agg( - jsonb_build_object( - 'text', l.text, - 'text_spanish', l.text_spanish - ) - ) - FROM snippet_labels sl - JOIN labels l ON l.id = sl.label - WHERE sl.snippet = ss.id - ), - '[]'::jsonb - ), + 'labels', get_snippet_labels(ss.id, p_language) -> 'labels', 'recorded_at', ss.recorded_at, 'comment_count', ss.comment_count, 'similarity', ss.similarity From deda7f7d2d2be33e3adc8acf3a1f2e7bbc762b8c Mon Sep 17 00:00:00 2001 From: Hung Ta Date: Tue, 10 Dec 2024 10:59:03 +0700 Subject: [PATCH 2/2] Revert comment for the sql --- supabase/database/sql/search_related_snippets.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/supabase/database/sql/search_related_snippets.sql b/supabase/database/sql/search_related_snippets.sql index 845edde..54a9d3c 100644 --- a/supabase/database/sql/search_related_snippets.sql +++ b/supabase/database/sql/search_related_snippets.sql @@ -45,7 +45,7 @@ BEGIN JOIN snippets s ON s.id = se.snippet JOIN audio_files a ON a.id = s.audio_file WHERE - se.snippet != snippet_id + se.snippet != snippet_id -- Exclude the source snippet AND 1 - (se.embedding <=> source_embedding) > match_threshold ORDER BY se.embedding <=> source_embedding LIMIT match_count