postgres: rework search

This commit is contained in:
nkanaev
2026-06-13 15:27:08 +01:00
parent 3b42d8c703
commit c554650db9
2 changed files with 9 additions and 23 deletions

View File

@@ -10,6 +10,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/nkanaev/yarr/src/content/htmlutil"
"github.com/nkanaev/yarr/src/storage/model" "github.com/nkanaev/yarr/src/storage/model"
) )
@@ -46,16 +47,19 @@ func (s *PostgresStorage) CreateItems(items []model.Item) bool {
}) })
for _, item := range items { for _, item := range items {
searchText := item.Title + " " + htmlutil.ExtractText(item.Content)
_, err = tx.Exec(` _, err = tx.Exec(`
insert into items ( insert into items (
guid, feed_id, title, link, date, guid, feed_id, title, link, date,
content, media_links, content, media_links,
date_arrived, last_arrived, status date_arrived, last_arrived, status,
search
) )
values ( values (
$1, $2, $3, $4, $5, $1, $2, $3, $4, $5,
$6, $7, $6, $7,
$8, $9, $10 $8, $9, $10,
to_tsvector('simple', $11)
) )
on conflict (feed_id, guid) do update set on conflict (feed_id, guid) do update set
last_arrived = excluded.last_arrived`, last_arrived = excluded.last_arrived`,
@@ -69,6 +73,7 @@ func (s *PostgresStorage) CreateItems(items []model.Item) bool {
now, now,
now, now,
model.UNREAD, model.UNREAD,
searchText,
) )
if err != nil { if err != nil {
log.Print(err) log.Print(err)

View File

@@ -95,33 +95,14 @@ func m01_initial(tx *sql.Tx) error {
date_arrived timestamptz, date_arrived timestamptz,
last_arrived timestamptz, last_arrived timestamptz,
status integer, 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_feed_id on items(feed_id);
create index if not exists idx_item__date_id_status on items(date, id, status); 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); 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 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 ( create table if not exists settings (
key text primary key, key text primary key,
val jsonb val jsonb