diff --git a/sql/queries/queries.sql b/sql/queries/queries.sql index a18a646..7bd0553 100644 --- a/sql/queries/queries.sql +++ b/sql/queries/queries.sql @@ -19,10 +19,14 @@ DELETE FROM entries WHERE entry_id = $1; -- name: SimilarEntries :many -SELECT entry_id, similarity(body, $1) AS sml - FROM entries - WHERE body % $1 - ORDER BY sml DESC, body; +SELECT entry_id, similarity(body, $2) as similarity, + ts_headline('english', body, q) as headline, + title from entries, + plainto_tsquery($2) q +WHERE user_id = $1 and + similarity(body, $2) > 0.0 + order by similarity DESC + LIMIT 10; -- name: CreateUser :one INSERT INTO users ( diff --git a/sql/schema/schema.sql b/sql/schema/schema.sql index d897991..48ee1cb 100644 --- a/sql/schema/schema.sql +++ b/sql/schema/schema.sql @@ -18,15 +18,15 @@ CREATE TABLE users ( ); CREATE TABLE entries ( - entry_id UUID NOT NULL default gen_random_uuid() PRIMARY KEY, - user_id int NOT NULL REFERENCES users ON DELETE CASCADE, + entry_id UUID NOT NULL default gen_random_uuid() PRIMARY KEY UNIQUE, + user_id BIGSERIAL NOT NULL REFERENCES users ON DELETE CASCADE, created_at timestamp NOT NULL DEFAULT NOW(), updated_at timestamp, title 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 $$ SELECT crypt(password, gen_salt('bf', 10));