Files

66 lines
1.5 KiB
MySQL
Raw Permalink Normal View History

2025-10-06 22:08:23 +02:00
create table item (
2025-10-23 08:45:54 +02:00
id bigint primary key,
2025-10-06 22:08:23 +02:00
data jsonb
);
2025-10-24 20:07:54 +02:00
create index idx_item_data
on item using gin (data jsonb_path_ops);
2025-10-23 08:45:54 +02:00
2025-10-24 20:07:54 +02:00
alter table item
add column description_tsv tsvector generated always as (
to_tsvector('french', data->>'description')
) stored;
create index idx_item_description_tsv
on item
using gin (description_tsv);
alter table item
add column search_tsv tsvector generated always as (
setweight(to_tsvector('french', coalesce(data->>'description', '')), 'A') ||
setweight(to_tsvector('french', coalesce(data->>'nom', '')), 'B') ||
setweight(to_tsvector('french', coalesce(data->>'categorie', '')), 'C')
) STORED;
CREATE INDEX idx_item_search_tsv
ON item
USING gin (search_tsv);
/*
SELECT
id,
ts_rank(search_tsv, plainto_tsquery('french', 'miel lavande')) AS rank
FROM item
WHERE search_tsv @@ plainto_tsquery('french', 'miel lavande')
ORDER BY rank DESC;
*/
truncate table item;
2025-10-23 08:45:54 +02:00
2025-09-17 01:37:38 +02:00
DO $$
DECLARE
2025-10-05 19:16:58 +02:00
f TEXT;
2025-09-17 01:37:38 +02:00
BEGIN
2025-10-05 19:16:58 +02:00
FOR f IN SELECT pg_catalog.pg_ls_dir('/tmp/json')
LOOP
IF right(f, 5) = '.json' THEN
BEGIN
RAISE NOTICE 'Import du fichier : %', f;
2025-10-06 22:08:23 +02:00
INSERT INTO item (id, data)
2025-10-05 19:16:58 +02:00
VALUES (
replace(f, '.json','')::bigint,
pg_read_file('/tmp/json/' || f)::jsonb
)
ON CONFLICT (id) DO UPDATE
2025-10-06 22:08:23 +02:00
SET data = EXCLUDED.data;
2025-10-05 19:16:58 +02:00
EXCEPTION WHEN OTHERS THEN
RAISE WARNING 'Erreur lors de l''import du fichier % : %', f, SQLERRM;
END;
END IF;
END LOOP;
2025-09-17 01:37:38 +02:00
END;
$$ LANGUAGE plpgsql;