From c554650db961bea412382c8bad93c132f20db74a Mon Sep 17 00:00:00 2001 From: nkanaev Date: Sat, 13 Jun 2026 15:27:08 +0100 Subject: [PATCH] postgres: rework search --- src/storage/postgres/item.go | 9 +++++++-- src/storage/postgres/migration.go | 23 ++--------------------- 2 files changed, 9 insertions(+), 23 deletions(-) diff --git a/src/storage/postgres/item.go b/src/storage/postgres/item.go index a01838f..252a57b 100644 --- a/src/storage/postgres/item.go +++ b/src/storage/postgres/item.go @@ -10,6 +10,7 @@ import ( "strings" "time" + "github.com/nkanaev/yarr/src/content/htmlutil" "github.com/nkanaev/yarr/src/storage/model" ) @@ -46,16 +47,19 @@ func (s *PostgresStorage) CreateItems(items []model.Item) bool { }) for _, item := range items { + searchText := item.Title + " " + htmlutil.ExtractText(item.Content) _, err = tx.Exec(` insert into items ( guid, feed_id, title, link, date, content, media_links, - date_arrived, last_arrived, status + date_arrived, last_arrived, status, + search ) values ( $1, $2, $3, $4, $5, $6, $7, - $8, $9, $10 + $8, $9, $10, + to_tsvector('simple', $11) ) on conflict (feed_id, guid) do update set last_arrived = excluded.last_arrived`, @@ -69,6 +73,7 @@ func (s *PostgresStorage) CreateItems(items []model.Item) bool { now, now, model.UNREAD, + searchText, ) if err != nil { log.Print(err) diff --git a/src/storage/postgres/migration.go b/src/storage/postgres/migration.go index 6ffa411..2698e2c 100644 --- a/src/storage/postgres/migration.go +++ b/src/storage/postgres/migration.go @@ -95,33 +95,14 @@ func m01_initial(tx *sql.Tx) error { date_arrived timestamptz, last_arrived timestamptz, status integer, - media_links jsonb + media_links jsonb, + search tsvector ); create index if not exists idx_item_feed_id on items(feed_id); create index if not exists idx_item__date_id_status on items(date, id, status); create unique index if not exists idx_item_guid on items(feed_id, guid); - - alter table items add column if not exists search tsvector; create index if not exists idx_item_search on items using gin(search); - create or replace function items_search_update() returns trigger as $$ - begin - new.search := to_tsvector('english', - coalesce(new.title, '') || ' ' || - coalesce(regexp_replace(new.content, '<[^>]+>', '', 'g'), '') - ); - return new; - end; - $$ language plpgsql; - - create trigger if not exists trg_items_search_insert - before insert on items - for each row execute function items_search_update(); - - create trigger if not exists trg_items_search_update - before update of title, content on items - for each row execute function items_search_update(); - create table if not exists settings ( key text primary key, val jsonb