use 1 table for catalog data + search

pull/1/head
Adam Veldhousen 1 year ago
parent 5221c50814
commit 7970ff99b0
Signed by: adam
GPG Key ID: 6DB29003C6DD1E4B

@ -15,23 +15,17 @@ CREATE TABLE IF NOT EXISTS catalog.upcoming_auctions (
sourcesitename VARCHAR(256) NOT NULL,
sourceurl VARCHAR(1024) NOT NULL,
country VARCHAR(64) NOT NULL,
province VARCHAR(128) NOT NULL
);
CREATE TABLE IF NOT EXISTS catalog.upcoming_auctions_fts (
id SERIAL PRIMARY KEY,
auctionid INTEGER REFERENCES CATALOG.UPCOMING_AUCTIONS,
title VARCHAR(1024) NOT NULL,
sourcesitename VARCHAR(256) NOT NULL,
description TEXT NOT NULL,
province VARCHAR(128) NOT NULL,
ts tsvector GENERATED ALWAYS AS ((
setweight(to_tsvector('english', (sourcesitename)::TEXT), 'A') ||
setweight(to_tsvector('english', (title)::TEXT), 'A') ||
setweight(to_tsvector('english', (description)::TEXT), 'C')
setweight(to_tsvector('english', (description)::TEXT), 'B') ||
setweight(to_tsvector('english', (province)::TEXT), 'C') ||
setweight(to_tsvector('english', (country)::TEXT), 'C')
)) STORED
);
CREATE INDEX IF NOT EXISTS ts_idx ON catalog.upcoming_auctions_fts USING GIN(ts);
CREATE INDEX IF NOT EXISTS ts_idx ON catalog.upcoming_auctions USING GIN(ts);
-- +goose StatementBegin
CREATE OR REPLACE FUNCTION catalog.bh_import_auction(
@ -79,13 +73,6 @@ BEGIN
p_country,
p_province
) RETURNING id INTO auction_id;
INSERT INTO catalog.upcoming_auctions_fts (
auctionid,
title,
description,
sourcesitename
) VALUES ( auction_id, p_title, p_description, p_sourcesitename );
ELSE
-- 0 means there is a duplicate auction ID
auction_id = 0;

@ -1,18 +1,29 @@
-- name: GetUpcoming :many
SELECT ua.*
SELECT
ua.id,
ua.fingerprint,
ua.title,
ua.description,
ua.startts,
ua.endts,
ua.itemcount,
ua.sourcesiteurl,
ua.sourcesitename,
ua.sourceurl,
ua.country,
ua.province
FROM catalog.upcoming_auctions ua
LEFT JOIN catalog.upcoming_auctions_fts fts on ua.id = fts.auctionid
WHERE
ua.endts >= DATE(NOW()) AND
(case when sqlc.arg(searchTerm) = '' then
ua.id > 0
else
fts.ts @@ websearch_to_tsquery(sqlc.arg(searchTerm))
ua.ts @@ websearch_to_tsquery(sqlc.arg(searchTerm))
end)
ORDER BY
ua.endts ASC,
(case when sqlc.arg(searchTerm) != '' then
ts_rank(fts.ts, websearch_to_tsquery(sqlc.arg(searchTerm)))
ts_rank(ua.ts, websearch_to_tsquery(sqlc.arg(searchTerm)))
end) DESC
OFFSET (sqlc.arg(page)::INTEGER * sqlc.arg(pageSize)::INTEGER)
LIMIT sqlc.arg(pageSize)::INTEGER;
@ -20,13 +31,12 @@ LIMIT sqlc.arg(pageSize)::INTEGER;
-- name: GetTotals :one
SELECT COUNT(*) as total,
(SELECT COUNT(*) FROM catalog.upcoming_auctions ua
LEFT JOIN catalog.upcoming_auctions_fts fts on ua.id = fts.auctionid
WHERE
ua.endts >= DATE(NOW()) AND
(case when sqlc.arg(searchTerm) = '' then
ua.id > 0
else
fts.ts @@ websearch_to_tsquery(sqlc.arg(searchTerm))
ua.ts @@ websearch_to_tsquery(sqlc.arg(searchTerm))
end)) as found
FROM catalog.upcoming_auctions
WHERE endts >= DATE(NOW());

@ -16,7 +16,7 @@ type PGCatalogStorage struct {
}
func (ps *PGCatalogStorage) GetUpcoming(ctx context.Context, q domain.UpcomingQuery) (results []domain.Auction, total int64, found int64, err error) {
var pgResults []postgres.CatalogUpcomingAuction
var pgResults []postgres.GetUpcomingRow
if pgResults, err = ps.Queries.GetUpcoming(ctx, postgres.GetUpcomingParams{
Searchterm: q.Term,

Loading…
Cancel
Save