Add keyword search

This commit is contained in:
MartinBraquet
2025-09-10 22:50:20 +02:00
parent d1c480f23f
commit 2550453ee4
8 changed files with 43 additions and 27 deletions

View File

@@ -83,7 +83,7 @@ export const getLovers: APIHandler<'get-lovers'> = async (props, _auth) => {
where(`data->>'userDeleted' != 'true' or data->>'userDeleted' is null`),
name &&
where(`lower(users.name) ilike '%' || lower($(name)) || '%'`, { name }),
where(`lower(users.name) ilike '%' || lower($(name)) || '%' or lower(bio::text) ilike '%' || lower($(name)) || '%'`, { name }),
genders?.length && where(`gender = ANY($(gender))`, { gender: genders }),

View File

@@ -20,4 +20,7 @@ BEGIN;
\i backend/supabase/user_notifications.sql
\i backend/supabase/functions_others.sql
\i backend/supabase/reports.sql
\i backend/supabase/migrations/20250910_bio_to_jsonb.sql
\i backend/supabase/migrations/20250910_add_bio_text.sql
\i backend/supabase/migrations/20250910_pg_trgm.sql
COMMIT;

View File

@@ -1,23 +0,0 @@
-- This file is copied from https://github.com/manifoldmarkets/manifold/blob/main/backend/supabase/reports.sql
create table if not exists
reports (
content_id text not null,
content_owner_id text not null,
content_type text not null,
created_time timestamp with time zone default now(),
description text,
id text default uuid_generate_v4 () not null,
parent_id text,
parent_type text,
user_id text not null
);
-- Foreign Keys
alter table reports
add constraint reports_content_owner_id_fkey foreign key (content_owner_id) references users (id);
alter table reports
add constraint reports_user_id_fkey foreign key (user_id) references users (id);
-- Row Level Security
alter table reports enable row level security;

View File

@@ -0,0 +1,22 @@
ALTER TABLE lovers ADD COLUMN bio_text tsvector;
CREATE OR REPLACE FUNCTION lovers_bio_tsvector_update()
RETURNS trigger AS $$
BEGIN
new.bio_text := to_tsvector(
'english',
(
SELECT string_agg(trim(both '"' from x::text), ' ')
FROM jsonb_path_query(new.bio, '$.**.text'::jsonpath) AS x
)
);
RETURN new;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER lovers_bio_tsvector_trigger
BEFORE INSERT OR UPDATE OF bio ON lovers
FOR EACH ROW EXECUTE FUNCTION lovers_bio_tsvector_update();
create index on lovers using gin(bio_text);

View File

@@ -0,0 +1,5 @@
-- 1. Drop the old column
alter table lovers drop column if exists bio;
-- 2. Add the new column as jsonb
alter table lovers add column bio jsonb;

View File

@@ -0,0 +1,4 @@
create extension if not exists pg_trgm;
CREATE INDEX lovers_bio_trgm_idx
ON lovers USING gin ((bio::text) gin_trgm_ops);

View File

@@ -61,7 +61,7 @@ export const Search = (props: {
<Row className={'mb-2 justify-between gap-2'}>
<Input
value={filters.name ?? ''}
placeholder={'Search name'}
placeholder={'Search anything...'}
className={'w-full max-w-xs'}
onChange={(e) => {
updateFilter({ name: e.target.value })

View File

@@ -53,7 +53,11 @@ export function ProfilesHome() {
if (!user) return;
setIsReloading(true);
const current = ++id.current;
api('get-lovers', removeNullOrUndefinedProps({limit: 20, compatibleWithUserId: user?.id, ...filters}) as any)
api('get-lovers', removeNullOrUndefinedProps({
limit: 20,
compatibleWithUserId: user?.id,
...filters
}) as any)
.then(({lovers}) => {
if (current === id.current) setLovers(lovers);
})
@@ -74,7 +78,8 @@ export function ProfilesHome() {
const result = await api('get-lovers', removeNullOrUndefinedProps({
limit: 20,
compatibleWithUserId: user?.id,
after: lastLover?.id.toString(), ...filters
after: lastLover?.id.toString(),
...filters
}) as any);
if (result.lovers.length === 0) return false;
setLovers((prev) => (prev ? [...prev, ...result.lovers] : result.lovers));