forked from ballerine-io/ballerine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: full text search (ballerine-io#2021)
- Loading branch information
1 parent
92cbe47
commit 23dd2f5
Showing
3 changed files
with
230 additions
and
92 deletions.
There are no files selected for viewing
This file contains 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 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
113 changes: 113 additions & 0 deletions
113
...lows-service/prisma/migrations/20240129082538_update_search_function_to_fts/migration.sql
This file contains 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,113 @@ | ||
CREATE OR REPLACE | ||
FUNCTION public.search_workflow_data( | ||
search_text TEXT, | ||
entity_type TEXT, | ||
workflow_ids TEXT[], | ||
statuses TEXT[], | ||
project_ids TEXT[], | ||
assignee_ids TEXT[], | ||
case_statuses TEXT[], | ||
include_unassigned boolean | ||
) | ||
RETURNS TABLE ( | ||
id TEXT | ||
) AS $$ | ||
DECLARE | ||
search_term TEXT := regexp_replace( | ||
trim( | ||
regexp_replace( | ||
regexp_replace(search_text, '[^a-zA-Z0-9\s]', '', 'g'), | ||
'\s{1,}', ' ', 'g' | ||
) | ||
), | ||
'\s+', ':*&', 'g' | ||
); | ||
|
||
BEGIN | ||
IF search_term != '' THEN | ||
search_term := concat( | ||
search_term, | ||
':*' | ||
); | ||
END IF; | ||
RETURN QUERY | ||
SELECT | ||
wrd."id" | ||
FROM | ||
public."WorkflowRuntimeData" wrd | ||
LEFT JOIN LATERAL jsonb_array_elements_text(wrd.tags) AS tag ON | ||
TRUE | ||
WHERE | ||
( | ||
search_term = '' | ||
OR | ||
( | ||
search_term <> '' | ||
AND | ||
to_tsquery('simple', search_term) @@ | ||
to_tsvector('simple', | ||
regexp_replace( | ||
lower(CONCAT_WS(' ', | ||
wrd.context->'entity'->>'id', | ||
wrd.context->'entity'->>'ballerineEntityId', | ||
wrd.context->'entity'->'data'->>'firstName', | ||
wrd.context->'entity'->'data'->>'middleName', | ||
wrd.context->'entity'->'data'->>'lastName', | ||
wrd.context->'entity'->'data'->>'email', | ||
wrd.context->'entity'->'data'->>'companyName', | ||
wrd.context->'entity'->'data'->>'phone')), | ||
'[^a-zA-Z0-9\s]', | ||
'', | ||
'g' | ||
) | ||
) | ||
) | ||
) | ||
AND | ||
( | ||
"workflowDefinitionId" = ANY(workflow_ids) | ||
OR | ||
array_length(workflow_ids, 1) IS NULL | ||
) | ||
AND | ||
( | ||
"status"::TEXT = ANY(statuses) | ||
OR | ||
array_length(statuses, 1) IS NULL | ||
) | ||
AND | ||
( | ||
"projectId" = ANY(project_ids) | ||
OR | ||
array_length(project_ids, 1) IS NULL | ||
) | ||
AND | ||
( | ||
CASE | ||
WHEN entity_type = 'individuals' THEN "endUserId" IS NOT NULL | ||
ELSE "businessId" IS NOT NULL | ||
END | ||
) | ||
AND | ||
( | ||
"assigneeId" = ANY(assignee_ids) | ||
OR | ||
( | ||
include_unassigned | ||
AND "assigneeId" IS NULL | ||
) | ||
OR | ||
( | ||
include_unassigned IS NOT TRUE | ||
AND array_length(assignee_ids, 1) IS NULL | ||
) | ||
) | ||
AND | ||
( | ||
tag = ANY(case_statuses) | ||
OR | ||
array_length(case_statuses, 1) IS NULL | ||
); | ||
END; | ||
|
||
$$ LANGUAGE plpgsql; |