Skip to content

Commit

Permalink
feat: add remote software to global search
Browse files Browse the repository at this point in the history
  • Loading branch information
dmijatovic committed Jan 10, 2025
1 parent 4f72df3 commit f33951c
Show file tree
Hide file tree
Showing 6 changed files with 305 additions and 166 deletions.
133 changes: 2 additions & 131 deletions database/100-create-api-views.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
-- SPDX-FileCopyrightText: 2022 - 2023 Christian Meeßen (GFZ) <[email protected]>
-- SPDX-FileCopyrightText: 2022 - 2023 Helmholtz Centre Potsdam - GFZ German Research Centre for Geosciences
-- SPDX-FileCopyrightText: 2022 - 2024 Ewan Cahen (Netherlands eScience Center) <[email protected]>
-- SPDX-FileCopyrightText: 2022 - 2024 Netherlands eScience Center
-- SPDX-FileCopyrightText: 2023 - 2024 Dusan Mijatovic (Netherlands eScience Center)
-- SPDX-FileCopyrightText: 2022 - 2025 Netherlands eScience Center
-- SPDX-FileCopyrightText: 2023 - 2025 Dusan Mijatovic (Netherlands eScience Center)
--
-- SPDX-License-Identifier: Apache-2.0

Expand Down Expand Up @@ -829,135 +829,6 @@ $$
$$;


-- GLOBAL SEARCH
CREATE FUNCTION global_search(query VARCHAR) RETURNS TABLE(
slug VARCHAR,
name VARCHAR,
source TEXT,
is_published BOOLEAN,
rank INTEGER,
index_found INTEGER
) LANGUAGE sql STABLE AS
$$
-- SOFTWARE search item
SELECT
software.slug,
software.brand_name AS name,
'software' AS "source",
software.is_published,
(CASE
WHEN software.slug ILIKE query OR software.brand_name ILIKE query THEN 0
WHEN BOOL_OR(keyword.value ILIKE query) THEN 1
WHEN software.slug ILIKE CONCAT(query, '%') OR software.brand_name ILIKE CONCAT(query, '%') THEN 2
WHEN software.slug ILIKE CONCAT('%', query, '%') OR software.brand_name ILIKE CONCAT('%', query, '%') THEN 3
ELSE 4
END) AS rank,
(CASE
WHEN software.slug ILIKE query OR software.brand_name ILIKE query THEN 0
WHEN BOOL_OR(keyword.value ILIKE query) THEN 0
WHEN software.slug ILIKE CONCAT(query, '%') OR software.brand_name ILIKE CONCAT(query, '%') THEN 0
WHEN software.slug ILIKE CONCAT('%', query, '%') OR software.brand_name ILIKE CONCAT('%', query, '%')
THEN LEAST(NULLIF(POSITION(query IN software.slug), 0), NULLIF(POSITION(query IN software.brand_name), 0))
ELSE 0
END) AS index_found
FROM
software
LEFT JOIN keyword_for_software ON keyword_for_software.software = software.id
LEFT JOIN keyword ON keyword.id = keyword_for_software.keyword
GROUP BY software.id
HAVING
software.slug ILIKE CONCAT('%', query, '%')
OR
software.brand_name ILIKE CONCAT('%', query, '%')
OR
software.short_statement ILIKE CONCAT('%', query, '%')
OR
BOOL_OR(keyword.value ILIKE CONCAT('%', query, '%'))
UNION ALL
-- PROJECT search item
SELECT
project.slug,
project.title AS name,
'projects' AS "source",
project.is_published,
(CASE
WHEN project.slug ILIKE query OR project.title ILIKE query THEN 0
WHEN BOOL_OR(keyword.value ILIKE query) THEN 1
WHEN project.slug ILIKE CONCAT(query, '%') OR project.title ILIKE CONCAT(query, '%') THEN 2
WHEN project.slug ILIKE CONCAT('%', query, '%') OR project.title ILIKE CONCAT('%', query, '%') THEN 3
ELSE 4
END) AS rank,
(CASE
WHEN project.slug ILIKE query OR project.title ILIKE query THEN 0
WHEN BOOL_OR(keyword.value ILIKE query) THEN 0
WHEN project.slug ILIKE CONCAT(query, '%') OR project.title ILIKE CONCAT(query, '%') THEN 0
WHEN project.slug ILIKE CONCAT('%', query, '%') OR project.title ILIKE CONCAT('%', query, '%')
THEN LEAST(NULLIF(POSITION(query IN project.slug), 0), NULLIF(POSITION(query IN project.title), 0))
ELSE 0
END) AS index_found
FROM
project
LEFT JOIN keyword_for_project ON keyword_for_project.project = project.id
LEFT JOIN keyword ON keyword.id = keyword_for_project.keyword
GROUP BY project.id
HAVING
project.slug ILIKE CONCAT('%', query, '%')
OR
project.title ILIKE CONCAT('%', query, '%')
OR
project.subtitle ILIKE CONCAT('%', query, '%')
OR
BOOL_OR(keyword.value ILIKE CONCAT('%', query, '%'))
UNION ALL
-- ORGANISATION search item
SELECT
organisation.slug,
organisation."name",
'organisations' AS "source",
TRUE AS is_published,
(CASE
WHEN organisation.slug ILIKE query OR organisation."name" ILIKE query THEN 0
WHEN organisation.slug ILIKE CONCAT(query, '%') OR organisation."name" ILIKE CONCAT(query, '%') THEN 2
ELSE 3
END) AS rank,
(CASE
WHEN organisation.slug ILIKE query OR organisation."name" ILIKE query THEN 0
WHEN organisation.slug ILIKE CONCAT(query, '%') OR organisation."name" ILIKE CONCAT(query, '%') THEN 0
ELSE
LEAST(NULLIF(POSITION(query IN organisation.slug), 0), NULLIF(POSITION(query IN organisation."name"), 0))
END) AS index_found
FROM
organisation
WHERE
-- ONLY TOP LEVEL ORGANISATIONS
organisation.parent IS NULL
AND
(organisation.slug ILIKE CONCAT('%', query, '%') OR organisation."name" ILIKE CONCAT('%', query, '%'))
UNION ALL
-- COMMUNITY search item
SELECT
community.slug,
community."name",
'communities' AS "source",
TRUE AS is_published,
(CASE
WHEN community.slug ILIKE query OR community."name" ILIKE query THEN 0
WHEN community.slug ILIKE CONCAT(query, '%') OR community."name" ILIKE CONCAT(query, '%') THEN 2
ELSE 3
END) AS rank,
(CASE
WHEN community.slug ILIKE query OR community."name" ILIKE query THEN 0
WHEN community.slug ILIKE CONCAT(query, '%') OR community."name" ILIKE CONCAT(query, '%') THEN 0
ELSE
LEAST(NULLIF(POSITION(query IN community.slug), 0), NULLIF(POSITION(query IN community."name"), 0))
END) AS index_found
FROM
community
WHERE
community.slug ILIKE CONCAT('%', query, '%') OR community."name" ILIKE CONCAT('%', query, '%');
$$;


-- Check whether user agreed on Terms of Service and read the Privacy Statement
CREATE FUNCTION user_agreements_stored(account_id UUID) RETURNS BOOLEAN LANGUAGE sql STABLE AS
$$
Expand Down
127 changes: 127 additions & 0 deletions database/125-global-search.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
-- SPDX-FileCopyrightText: 2025 Dusan Mijatovic (Netherlands eScience Center)
-- SPDX-FileCopyrightText: 2025 Netherlands eScience Center
--
-- SPDX-License-Identifier: Apache-2.0

-- GLOBAL SEARCH
-- depends on: aggregated_software_search
CREATE FUNCTION global_search(query VARCHAR) RETURNS TABLE(
slug VARCHAR,
domain VARCHAR,
name VARCHAR,
source TEXT,
is_published BOOLEAN,
rank INTEGER,
index_found INTEGER
) LANGUAGE sql STABLE AS
$$
-- AGGREGATED SOFTWARE search
SELECT
aggregated_software_search.slug,
aggregated_software_search.domain,
aggregated_software_search.brand_name AS name,
'software' AS "source",
aggregated_software_search.is_published,
(CASE
WHEN aggregated_software_search.slug ILIKE query OR aggregated_software_search.brand_name ILIKE query THEN 0
WHEN aggregated_software_search.keywords_text ILIKE CONCAT('%', query, '%') THEN 1
WHEN aggregated_software_search.slug ILIKE CONCAT(query, '%') OR aggregated_software_search.brand_name ILIKE CONCAT(query, '%') THEN 2
WHEN aggregated_software_search.slug ILIKE CONCAT('%', query, '%') OR aggregated_software_search.brand_name ILIKE CONCAT('%', query, '%') THEN 3
ELSE 4
END) AS rank,
(CASE
WHEN aggregated_software_search.slug ILIKE query OR aggregated_software_search.brand_name ILIKE query THEN 0
WHEN aggregated_software_search.keywords_text ILIKE CONCAT('%', query, '%') THEN 0
WHEN aggregated_software_search.slug ILIKE CONCAT(query, '%') OR aggregated_software_search.brand_name ILIKE CONCAT(query, '%') THEN 0
WHEN aggregated_software_search.slug ILIKE CONCAT('%', query, '%') OR aggregated_software_search.brand_name ILIKE CONCAT('%', query, '%')
THEN LEAST(NULLIF(POSITION(query IN aggregated_software_search.slug), 0), NULLIF(POSITION(query IN aggregated_software_search.brand_name), 0))
ELSE 0
END) AS index_found
FROM
aggregated_software_search(query)
UNION ALL
-- PROJECT search
SELECT
project.slug,
NULL AS domain,
project.title AS name,
'projects' AS "source",
project.is_published,
(CASE
WHEN project.slug ILIKE query OR project.title ILIKE query THEN 0
WHEN BOOL_OR(keyword.value ILIKE query) THEN 1
WHEN project.slug ILIKE CONCAT(query, '%') OR project.title ILIKE CONCAT(query, '%') THEN 2
WHEN project.slug ILIKE CONCAT('%', query, '%') OR project.title ILIKE CONCAT('%', query, '%') THEN 3
ELSE 4
END) AS rank,
(CASE
WHEN project.slug ILIKE query OR project.title ILIKE query THEN 0
WHEN BOOL_OR(keyword.value ILIKE query) THEN 0
WHEN project.slug ILIKE CONCAT(query, '%') OR project.title ILIKE CONCAT(query, '%') THEN 0
WHEN project.slug ILIKE CONCAT('%', query, '%') OR project.title ILIKE CONCAT('%', query, '%')
THEN LEAST(NULLIF(POSITION(query IN project.slug), 0), NULLIF(POSITION(query IN project.title), 0))
ELSE 0
END) AS index_found
FROM
project
LEFT JOIN keyword_for_project ON keyword_for_project.project = project.id
LEFT JOIN keyword ON keyword.id = keyword_for_project.keyword
GROUP BY project.id
HAVING
project.slug ILIKE CONCAT('%', query, '%')
OR
project.title ILIKE CONCAT('%', query, '%')
OR
project.subtitle ILIKE CONCAT('%', query, '%')
OR
BOOL_OR(keyword.value ILIKE CONCAT('%', query, '%'))
UNION ALL
-- ORGANISATION search
SELECT
organisation.slug,
NULL AS domain,
organisation."name",
'organisations' AS "source",
TRUE AS is_published,
(CASE
WHEN organisation.slug ILIKE query OR organisation."name" ILIKE query THEN 0
WHEN organisation.slug ILIKE CONCAT(query, '%') OR organisation."name" ILIKE CONCAT(query, '%') THEN 2
ELSE 3
END) AS rank,
(CASE
WHEN organisation.slug ILIKE query OR organisation."name" ILIKE query THEN 0
WHEN organisation.slug ILIKE CONCAT(query, '%') OR organisation."name" ILIKE CONCAT(query, '%') THEN 0
ELSE
LEAST(NULLIF(POSITION(query IN organisation.slug), 0), NULLIF(POSITION(query IN organisation."name"), 0))
END) AS index_found
FROM
organisation
WHERE
-- ONLY TOP LEVEL ORGANISATIONS
organisation.parent IS NULL
AND
(organisation.slug ILIKE CONCAT('%', query, '%') OR organisation."name" ILIKE CONCAT('%', query, '%'))
UNION ALL
-- COMMUNITY search
SELECT
community.slug,
NULL AS domain,
community."name",
'communities' AS "source",
TRUE AS is_published,
(CASE
WHEN community.slug ILIKE query OR community."name" ILIKE query THEN 0
WHEN community.slug ILIKE CONCAT(query, '%') OR community."name" ILIKE CONCAT(query, '%') THEN 2
ELSE 3
END) AS rank,
(CASE
WHEN community.slug ILIKE query OR community."name" ILIKE query THEN 0
WHEN community.slug ILIKE CONCAT(query, '%') OR community."name" ILIKE CONCAT(query, '%') THEN 0
ELSE
LEAST(NULLIF(POSITION(query IN community.slug), 0), NULLIF(POSITION(query IN community."name"), 0))
END) AS index_found
FROM
community
WHERE
community.slug ILIKE CONCAT('%', query, '%') OR community."name" ILIKE CONCAT('%', query, '%');
$$;
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
// SPDX-FileCopyrightText: 2023 Dusan Mijatovic (dv4all)
// SPDX-FileCopyrightText: 2023 dv4all
// SPDX-FileCopyrightText: 2024 Dusan Mijatovic (Netherlands eScience Center)
// SPDX-FileCopyrightText: 2024 - 2025 Dusan Mijatovic (Netherlands eScience Center)
// SPDX-FileCopyrightText: 2024 - 2025 Netherlands eScience Center
// SPDX-FileCopyrightText: 2024 Ewan Cahen (Netherlands eScience Center) <[email protected]>
// SPDX-FileCopyrightText: 2024 Netherlands eScience Center
//
// SPDX-License-Identifier: Apache-2.0

import logger from '~/utils/logger'
import {createJsonHeaders} from '~/utils/fetchHelpers'

export type GlobalSearchResults = {
domain: string | null
slug: string,
name: string,
source: 'software' | 'projects' | 'organisations' | 'communities',
Expand Down
Loading

0 comments on commit f33951c

Please sign in to comment.