Skip to content

Commit

Permalink
Use Postgres FTS for search
Browse files Browse the repository at this point in the history
  • Loading branch information
samwillis committed Oct 16, 2024
1 parent 08b6650 commit 8d1d1b6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
7 changes: 6 additions & 1 deletion demos/linearlite/db/migrations-client/01-create_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ CREATE TABLE IF NOT EXISTS "issue" (
"sent_to_server" BOOLEAN NOT NULL DEFAULT FALSE, -- Flag to track if the row has been sent to the server
"synced" BOOLEAN GENERATED ALWAYS AS (ARRAY_LENGTH(modified_columns, 1) IS NULL AND NOT deleted AND NOT new) STORED,
"backup" JSONB, -- JSONB column to store the backup of the row data for modified columns
"search_vector" tsvector GENERATED ALWAYS AS (
setweight(to_tsvector('simple', coalesce(title, '')), 'A') ||
setweight(to_tsvector('simple', coalesce(description, '')), 'B')
) STORED,
CONSTRAINT "issue_pkey" PRIMARY KEY ("id")
);

Expand Down Expand Up @@ -42,6 +46,7 @@ CREATE INDEX IF NOT EXISTS "issue_created_idx" ON "issue" ("created");
CREATE INDEX IF NOT EXISTS "issue_kanbanorder_idx" ON "issue" ("kanbanorder");
CREATE INDEX IF NOT EXISTS "issue_deleted_idx" ON "issue" ("deleted");
CREATE INDEX IF NOT EXISTS "issue_synced_idx" ON "issue" ("synced");
CREATE INDEX IF NOT EXISTS "issue_search_idx" ON "issue" USING GIN ("search_vector");

CREATE INDEX IF NOT EXISTS "comment_id_idx" ON "comment" ("id");
CREATE INDEX IF NOT EXISTS "comment_issue_id_idx" ON "comment" ("issue_id");
Expand Down Expand Up @@ -291,4 +296,4 @@ $$ LANGUAGE plpgsql;

-- Example usage:
-- SELECT revert_local_changes('issue', '123e4567-e89b-12d3-a456-426614174000');
-- SELECT revert_local_changes('comment', '123e4567-e89b-12d3-a456-426614174001');
-- SELECT revert_local_changes('comment', '123e4567-e89b-12d3-a456-426614174001');
16 changes: 8 additions & 8 deletions demos/linearlite/src/pglite-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ worker({
primaryKey: ['id'],
shapeKey: 'issues',
})
// await pg.sync.syncShapeToTable({
// shape: {
// url: `${ELECTRIC_URL}/v1/shape/comment`,
// },
// table: 'comment',
// primaryKey: ['id'],
// shapeKey: 'comments',
// })
await pg.sync.syncShapeToTable({
shape: {
url: `${ELECTRIC_URL}/v1/shape/comment`,
},
table: 'comment',
primaryKey: ['id'],
shapeKey: 'comments',
})
startWritePath(pg)
return pg
},
Expand Down
4 changes: 2 additions & 2 deletions demos/linearlite/src/utils/filterState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ export function filterStateToSql(filterState: FilterState) {
sqlParams.push(...filterState.priority)
}
if (filterState.query) {
sqlWhere.push(`title ILIKE $${i++}`)
sqlParams.push(`%${filterState.query}%`)
sqlWhere.push(`search_vector @@ plainto_tsquery('simple', $${i++})`)
sqlParams.push(filterState.query)
}
const sql = `
SELECT id, title, priority, status, modified, created, kanbanorder, username, synced
Expand Down

0 comments on commit 8d1d1b6

Please sign in to comment.