ranking y tags

This commit is contained in:
jlimolina 2025-10-17 04:10:09 +02:00
parent 0bfeb610a9
commit d29152a0f6
9 changed files with 344 additions and 48 deletions

24
init-db/06-tags.sql Normal file
View file

@ -0,0 +1,24 @@
-- init-db/06-tags.sql (modelo simple compatible con ner_worker.py)
-- Tabla de tags
CREATE TABLE IF NOT EXISTS tags (
id SERIAL PRIMARY KEY,
valor TEXT NOT NULL,
tipo TEXT NOT NULL, -- 'persona','organizacion','lugar', ...
UNIQUE (valor, tipo)
);
-- Relación tag <-> traducción
CREATE TABLE IF NOT EXISTS tags_noticia (
id SERIAL PRIMARY KEY,
traduccion_id INT NOT NULL REFERENCES traducciones(id) ON DELETE CASCADE,
tag_id INT NOT NULL REFERENCES tags(id) ON DELETE CASCADE,
UNIQUE (traduccion_id, tag_id)
);
-- Índices útiles
CREATE INDEX IF NOT EXISTS idx_tags_valor ON tags(valor);
CREATE INDEX IF NOT EXISTS idx_tags_tipo ON tags(tipo);
CREATE INDEX IF NOT EXISTS idx_tags_noticia_trid ON tags_noticia(traduccion_id);
CREATE INDEX IF NOT EXISTS idx_tags_noticia_tag ON tags_noticia(tag_id);

42
init-db/07-tags-views.sql Normal file
View file

@ -0,0 +1,42 @@
-- init-db/07-tags-views.sql
-- Vista de Top tags (24h) para el esquema:
-- tags(id, valor, tipo)
-- tags_noticia(id, traduccion_id, tag_id)
-- traducciones(id, noticia_id, lang_to, status, ...)
-- noticias(id, fecha, ...)
CREATE OR REPLACE VIEW public.v_tag_counts_24h AS
SELECT
tg.id,
tg.valor,
tg.tipo,
COUNT(*) AS apariciones
FROM public.tags tg
JOIN public.tags_noticia tn ON tn.tag_id = tg.id
JOIN public.traducciones t ON t.id = tn.traduccion_id
JOIN public.noticias n ON n.id = t.noticia_id
WHERE t.status = 'done'
AND t.lang_to = 'es'
AND n.fecha >= now() - INTERVAL '24 hours'
GROUP BY tg.id, tg.valor, tg.tipo
ORDER BY apariciones DESC, tg.valor;
-- Índices recomendados para acelerar la vista (idempotentes)
CREATE INDEX IF NOT EXISTS idx_noticias_fecha
ON public.noticias (fecha);
CREATE INDEX IF NOT EXISTS idx_traducciones_noticia_lang_status
ON public.traducciones (noticia_id, lang_to, status);
CREATE INDEX IF NOT EXISTS idx_tags_noticia_traduccion
ON public.tags_noticia (traduccion_id);
CREATE INDEX IF NOT EXISTS idx_tags_noticia_tag
ON public.tags_noticia (tag_id);
-- (Opcionales si no existen ya, pero ayudan en búsquedas ad hoc)
CREATE INDEX IF NOT EXISTS idx_tags_valor
ON public.tags (valor);
CREATE INDEX IF NOT EXISTS idx_tags_tipo
ON public.tags (tipo);