-
Notifications
You must be signed in to change notification settings - Fork 0
CROSSLINK-234 Add full text search #489
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
12fe331
CROSSLINK-234 Add full text search
JanisSaldabols bad613c
CROSSLINK-234 Fix copilot comments
JanisSaldabols 144bce9
CROSSLINK-234 Add items in insert and update parameters
JanisSaldabols 794940d
CROSSLINK-234 Fix copilot comments
JanisSaldabols cbfef1d
CROSSLINK-234 Revert
JanisSaldabols fc42233
Merge branch 'main' into CROSSLINK-234
jakub-id 79b606b
Merge branch 'main' into CROSSLINK-234
JanisSaldabols d42d73d
CROSSLINK-234 Correct migration script name
JanisSaldabols 228d449
CROSSLINK-234 Use correct time format
JanisSaldabols File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| DROP TRIGGER IF EXISTS trigger_update_patron_request_search_tsvector ON patron_request; | ||
|
|
||
| DROP FUNCTION IF EXISTS update_patron_request_search_tsvector; | ||
|
|
||
| DROP INDEX IF EXISTS idx_patron_request_search; | ||
|
|
||
| DROP VIEW IF EXISTS patron_request_search_view; | ||
|
|
||
| ALTER TABLE patron_request DROP COLUMN IF EXISTS search; | ||
| ALTER TABLE patron_request DROP COLUMN IF EXISTS language; | ||
|
|
||
| DROP TRIGGER IF EXISTS trigger_update_patron_request_items ON item; | ||
|
|
||
| DROP FUNCTION IF EXISTS update_patron_request_items CASCADE; | ||
|
|
||
| ALTER TABLE patron_request DROP COLUMN IF EXISTS items; | ||
|
|
||
| CREATE OR REPLACE VIEW patron_request_search_view AS | ||
| SELECT | ||
| pr.*, | ||
| EXISTS ( | ||
| SELECT 1 | ||
| FROM notification n | ||
| WHERE n.pr_id = pr.id | ||
| ) AS has_notification, | ||
| EXISTS ( | ||
| SELECT 1 | ||
| FROM notification n | ||
| WHERE n.pr_id = pr.id and cost is not null | ||
| ) AS has_cost, | ||
| EXISTS ( | ||
| SELECT 1 | ||
| FROM notification n | ||
| WHERE n.pr_id = pr.id and acknowledged_at is null | ||
| ) AS has_unread_notification, | ||
| pr.ill_request -> 'serviceInfo' ->> 'serviceType' AS service_type, | ||
| pr.ill_request -> 'serviceInfo' -> 'serviceLevel' ->> '#text' AS service_level, | ||
| immutable_to_timestamp(pr.ill_request -> 'serviceInfo' ->> 'needBeforeDate') AS needed_at | ||
| FROM patron_request pr; | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| ALTER TABLE patron_request ADD COLUMN items JSONB NOT NULL DEFAULT '[]'::jsonb; | ||
|
|
||
| CREATE OR REPLACE FUNCTION update_patron_request_items() | ||
| RETURNS TRIGGER AS $$ | ||
| BEGIN | ||
| UPDATE patron_request | ||
| SET items = ( | ||
| SELECT jsonb_agg( | ||
| (to_jsonb(i) - 'pr_id') || | ||
| jsonb_build_object( | ||
| 'created_at', | ||
| to_char(i.created_at, 'YYYY-MM-DD"T"HH24:MI:SS.US') || to_char(i.created_at, 'TZH:TZM') | ||
| ) | ||
| ) | ||
| FROM item i | ||
| WHERE i.pr_id = NEW.pr_id | ||
| ) | ||
| WHERE id = NEW.pr_id; | ||
|
|
||
| RETURN NEW; | ||
| END; | ||
| $$ LANGUAGE plpgsql; | ||
|
|
||
| -- Create the trigger | ||
| CREATE TRIGGER trigger_update_patron_request_items | ||
| AFTER INSERT OR UPDATE ON item | ||
| FOR EACH ROW | ||
| EXECUTE FUNCTION update_patron_request_items(); | ||
|
|
||
|
|
||
| -- Add the search field as a tsvector column | ||
| ALTER TABLE patron_request | ||
| ADD COLUMN search tsvector, | ||
| ADD COLUMN language regconfig NOT NULL DEFAULT 'english'; | ||
|
|
||
| -- Create a trigger function to update the search tsvector | ||
| CREATE OR REPLACE FUNCTION update_patron_request_search_tsvector() | ||
| RETURNS TRIGGER AS $$ | ||
| BEGIN | ||
| -- Update the search tsvector column | ||
| NEW.search := to_tsvector(NEW.language, | ||
| COALESCE(NEW.requester_req_id, '') || ' ' || | ||
| COALESCE(NEW.patron, '') || ' ' || | ||
| COALESCE(NEW.ill_request->'patronInfo'->>'givenName', '') || ' ' || | ||
| COALESCE(NEW.ill_request->'patronInfo'->>'surname', '') || ' ' || | ||
| COALESCE(NEW.ill_request->'patronInfo'->>'patronId', '') || ' ' || | ||
| COALESCE(NEW.ill_request->'bibliographicInfo'->>'title', '') || ' ' || | ||
| COALESCE(NEW.ill_request->'bibliographicInfo'->>'author', '') || ' ' || | ||
| COALESCE( | ||
| (SELECT string_agg( | ||
| COALESCE(item->>'item_id', '') || ' ' || | ||
| COALESCE(item->>'barcode', '') || ' ' || | ||
| COALESCE(item->>'call_number', ''), ' ' | ||
| ) | ||
| FROM jsonb_array_elements(NEW.items) AS item), '' | ||
| ) | ||
| ); | ||
|
|
||
| RETURN NEW; | ||
| END; | ||
| $$ LANGUAGE plpgsql; | ||
|
|
||
| -- Create a trigger to update the search tsvector on insert or update | ||
| CREATE TRIGGER trigger_update_patron_request_search_tsvector | ||
| BEFORE INSERT OR UPDATE ON patron_request | ||
| FOR EACH ROW | ||
| EXECUTE FUNCTION update_patron_request_search_tsvector(); | ||
|
|
||
| CREATE INDEX idx_patron_request_search ON patron_request USING gin(search); | ||
|
|
||
| DROP VIEW IF EXISTS patron_request_search_view; | ||
| CREATE OR REPLACE VIEW patron_request_search_view AS | ||
| SELECT | ||
| pr.*, | ||
| EXISTS ( | ||
| SELECT 1 | ||
| FROM notification n | ||
| WHERE n.pr_id = pr.id | ||
| ) AS has_notification, | ||
| EXISTS ( | ||
| SELECT 1 | ||
| FROM notification n | ||
| WHERE n.pr_id = pr.id and cost is not null | ||
| ) AS has_cost, | ||
| EXISTS ( | ||
| SELECT 1 | ||
| FROM notification n | ||
| WHERE n.pr_id = pr.id and acknowledged_at is null | ||
| ) AS has_unread_notification, | ||
| pr.ill_request -> 'serviceInfo' ->> 'serviceType' AS service_type, | ||
| pr.ill_request -> 'serviceInfo' -> 'serviceLevel' ->> '#text' AS service_level, | ||
| immutable_to_timestamp(pr.ill_request -> 'serviceInfo' ->> 'needBeforeDate') AS needed_at | ||
| FROM patron_request pr; | ||
|
|
||
| -- One-time backfill of items for existing patron_request rows | ||
| UPDATE patron_request pr | ||
| SET items = COALESCE( | ||
| ( | ||
| SELECT jsonb_agg( | ||
| (to_jsonb(i) - 'pr_id') || | ||
| jsonb_build_object( | ||
| 'created_at', | ||
| to_char(i.created_at, 'YYYY-MM-DD"T"HH24:MI:SS.US') || to_char(i.created_at, 'TZH:TZM') | ||
| ) | ||
| ) | ||
| FROM item i | ||
| WHERE i.pr_id = pr.id | ||
| ), | ||
| '[]'::jsonb | ||
| ); | ||
| -- One-time backfill of search tsvector for existing patron_request rows | ||
| UPDATE patron_request pr | ||
| SET search = to_tsvector( | ||
| pr.language, | ||
| COALESCE(pr.requester_req_id, '') || ' ' || | ||
| COALESCE(pr.patron, '') || ' ' || | ||
| COALESCE(pr.ill_request->'patronInfo'->>'givenName', '') || ' ' || | ||
| COALESCE(pr.ill_request->'patronInfo'->>'surname', '') || ' ' || | ||
| COALESCE(pr.ill_request->'patronInfo'->>'patronId', '') || ' ' || | ||
| COALESCE(pr.ill_request->'bibliographicInfo'->>'title', '') || ' ' || | ||
| COALESCE(pr.ill_request->'bibliographicInfo'->>'author', '') || ' ' || | ||
| COALESCE( | ||
| ( | ||
| SELECT string_agg( | ||
| COALESCE(item->>'item_id', '') || ' ' || | ||
| COALESCE(item->>'barcode', '') || ' ' || | ||
| COALESCE(item->>'call_number', ''), | ||
| ' ' | ||
| ) | ||
| FROM jsonb_array_elements(pr.items) AS item | ||
| ), | ||
| '' | ||
| ) | ||
| ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.