use bigserial for user_id on entries, update SimilarEntries to include headline

This commit is contained in:
Aaron Bieber 2020-04-02 06:29:59 -06:00
parent aa44137660
commit 9ed951eda0
2 changed files with 11 additions and 7 deletions

View File

@ -19,10 +19,14 @@ DELETE FROM entries
WHERE entry_id = $1; WHERE entry_id = $1;
-- name: SimilarEntries :many -- name: SimilarEntries :many
SELECT entry_id, similarity(body, $1) AS sml SELECT entry_id, similarity(body, $2) as similarity,
FROM entries ts_headline('english', body, q) as headline,
WHERE body % $1 title from entries,
ORDER BY sml DESC, body; plainto_tsquery($2) q
WHERE user_id = $1 and
similarity(body, $2) > 0.0
order by similarity DESC
LIMIT 10;
-- name: CreateUser :one -- name: CreateUser :one
INSERT INTO users ( INSERT INTO users (

View File

@ -18,15 +18,15 @@ CREATE TABLE users (
); );
CREATE TABLE entries ( CREATE TABLE entries (
entry_id UUID NOT NULL default gen_random_uuid() PRIMARY KEY, entry_id UUID NOT NULL default gen_random_uuid() PRIMARY KEY UNIQUE,
user_id int NOT NULL REFERENCES users ON DELETE CASCADE, user_id BIGSERIAL NOT NULL REFERENCES users ON DELETE CASCADE,
created_at timestamp NOT NULL DEFAULT NOW(), created_at timestamp NOT NULL DEFAULT NOW(),
updated_at timestamp, updated_at timestamp,
title text NOT NULL DEFAULT '', title text NOT NULL DEFAULT '',
body text NOT NULL DEFAULT '' body text NOT NULL DEFAULT ''
); );
CREATE INDEX body_idx ON entries USING GIN (body gin_trgm_ops); CREATE INDEX body_trgm_idx ON entries USING gist (body gist_trgm_ops);
CREATE OR REPLACE FUNCTION hash(password text) RETURNS text AS $$ CREATE OR REPLACE FUNCTION hash(password text) RETURNS text AS $$
SELECT crypt(password, gen_salt('bf', 10)); SELECT crypt(password, gen_salt('bf', 10));