From ecf9d107574f5b0a66c3fa2c2a6321240dea1703 Mon Sep 17 00:00:00 2001 From: jjrom Date: Tue, 1 Oct 2024 15:03:31 +0200 Subject: [PATCH] Replace /hashtags endpoint by q filtering on /catalogs endpoint --- app/resto/core/RestoCollection.php | 1 - app/resto/core/RestoModel.php | 2 +- app/resto/core/api/STACAPI.php | 27 ++++++----- .../core/dbfunctions/CatalogsFunctions.php | 11 +++-- .../03_resto_target_model.sql | 48 +------------------ 5 files changed, 24 insertions(+), 65 deletions(-) diff --git a/app/resto/core/RestoCollection.php b/app/resto/core/RestoCollection.php index e23efb1a..db7a8da0 100755 --- a/app/resto/core/RestoCollection.php +++ b/app/resto/core/RestoCollection.php @@ -890,7 +890,6 @@ public function setSummaries($summaries) $this->summaries = array(); - // Datetime is not stored in facet if ( isset($this->extent['temporal']['interval'][0][0]) && isset($this->extent['temporal']['interval'][0][1]) ) { $this->summaries['datetime'] = array( 'minimum' => $this->extent['temporal']['interval'][0][0], diff --git a/app/resto/core/RestoModel.php b/app/resto/core/RestoModel.php index e9165121..0819c296 100755 --- a/app/resto/core/RestoModel.php +++ b/app/resto/core/RestoModel.php @@ -64,7 +64,7 @@ abstract class RestoModel ); /* - * Facet hierarchy + * Auto cataloging hierarchy */ public $facetCategories = array( array( diff --git a/app/resto/core/api/STACAPI.php b/app/resto/core/api/STACAPI.php index 7807e833..5e03a3af 100644 --- a/app/resto/core/api/STACAPI.php +++ b/app/resto/core/api/STACAPI.php @@ -283,7 +283,7 @@ public function getCatalogs($params) 'description' => 'List of available catalogs', 'links' => array_merge( $this->getBaseLinks(), - $this->getRootCatalogLinks($this->context->core['catalogMinMatch']) + $this->getRootCatalogLinks($params) ) ); } @@ -674,7 +674,7 @@ public function getChildren($params) // Initialize router to process each children individually $router = new RestoRouter($this->context, $this->user); - $links = $this->getRootCatalogLinks($this->context->core['catalogMinMatch']); + $links = $this->getRootCatalogLinks($params); for ($i = 0, $ii = count($links); $i < $ii; $i++) { if ($links[$i]['rel'] == 'child') { try { @@ -1306,7 +1306,8 @@ private function processPath($segments, $params = array()) array_pop($segments); $catalogs = $this->catalogsFunctions->getCatalogs(array( - 'id' => join('/', $segments) + 'id' => join('/', $segments), + 'q' => $params['q'] ?? null )); if ( empty($catalogs) || !$catalogs[0]['hashtag'] ) { @@ -1328,7 +1329,7 @@ private function processPath($segments, $params = array()) } // The path is the catalog identifier - $parentAndChilds = $this->getParentAndChilds(join('/', $segments)); + $parentAndChilds = $this->getParentAndChilds(join('/', $segments), $params); return array( 'stac_version' => STACAPI::STAC_VERSION, 'id' => $segments[count($segments) -1 ], @@ -1453,18 +1454,19 @@ private function jsonQueryToKVP($jsonQuery) * Return catalog childs * * @param string $catalogId + * @param array $params Search parameters * @return array */ - private function getParentAndChilds($catalogId) + private function getParentAndChilds($catalogId, $params) { - // Get catalogs - first one is $catalogId, other its childs $catalogs = $this->catalogsFunctions->getCatalogs(array( - 'id' => $catalogId + 'id' => $catalogId, + 'q' => $params['q'] ?? null ), true); $parentAndChilds = array( - 'parent' => $catalogs[0], + 'parent' => $catalogs[0] ?? null, 'childs' => array() ); @@ -1541,9 +1543,10 @@ private function getBaseLinks($segments = array()) /** * Get root links * + * @param $params // Additional filtering parameters for catalog search on description and title * @return array */ - private function getRootCatalogLinks() + private function getRootCatalogLinks($params) { $links = array(); @@ -1557,10 +1560,11 @@ private function getRootCatalogLinks() $links[] = $stacLink; } } - + // Get first level catalog $catalogs = $this->catalogsFunctions->getCatalogs(array( - 'level' => 1 + 'level' => 1, + 'q' => $params['q'] ?? null )); for ($i = 0, $ii = count($catalogs); $i < $ii; $i++) { @@ -1576,6 +1580,7 @@ private function getRootCatalogLinks() 'title' => $catalogs[$i]['title'], 'description' => $catalogs[$i]['description'] ?? '', 'type' => RestoUtil::$contentTypes['json'], + 'resto:type' => $catalogs[$i]['rtype'], 'href' => $this->context->core['baseUrl'] . '/catalogs/' . rawurlencode($catalogs[$i]['id']), 'matched' => $catalogs[$i]['counters']['total'] ); diff --git a/app/resto/core/dbfunctions/CatalogsFunctions.php b/app/resto/core/dbfunctions/CatalogsFunctions.php index 9f0bf421..6022eed9 100755 --- a/app/resto/core/dbfunctions/CatalogsFunctions.php +++ b/app/resto/core/dbfunctions/CatalogsFunctions.php @@ -128,12 +128,13 @@ public function getCatalogs($params, $withChilds = false) $where[] = $_where; } - if ( isset($params['description']) ) { - $values[] = '%' . $params['description'] . '%'; - $where[] = 'public.normalize(description) LIKE public.normalize($' . count($values) . ')'; + // Filter on description / title + if ( isset($params['q']) ) { + $values[] = '%' . $params['q'] . '%'; + $where[] = '(public.normalize(description) ILIKE public.normalize($' . count($values) . ') OR public.normalize(hashtag) ILIKE public.normalize($' . count($values) . ') )'; } - - if ( isset($params['level']) ) { + // [IMPORTANT] Discard level if q is set + else if ( isset($params['level']) ) { $values[] = $params['level']; $where[] = 'level=$' . count($values); } diff --git a/resto-database-model/03_resto_target_model.sql b/resto-database-model/03_resto_target_model.sql index 84ab0105..f855878d 100644 --- a/resto-database-model/03_resto_target_model.sql +++ b/resto-database-model/03_resto_target_model.sql @@ -371,46 +371,6 @@ CREATE TABLE IF NOT EXISTS __DATABASE_TARGET_SCHEMA__.feature_optical ( ); --- --- Facets table --- -CREATE TABLE IF NOT EXISTS __DATABASE_TARGET_SCHEMA__.facet ( - - -- Identifier for the facet (unique in combination with collection id) - id TEXT NOT NULL, - - -- Collection id attached to the facet - collection TEXT NOT NULL, - - -- Facet value - value TEXT, - - -- Facet type (i.e. catalog, hashtag, region, state, location, etc.) - type TEXT, - - -- Parent identifier (i.e. 'europe' for facet 'france') - pid TEXT NOT NULL, - - -- Set to 1 if facet is a terminal leaf, 0 otherwise (used for STAC) - isleaf INTEGER, - - -- Number of appearance of this facet within the collection - counter INTEGER, - - -- Facet date of creation - created TIMESTAMP DEFAULT now(), - - -- Owner of the facet i.e. first user to create it - owner BIGINT, - - -- Description - description TEXT, - - -- The id, pid, collection pair should be unique - PRIMARY KEY (id, pid, collection) - -); - -- -- Catalog table -- @@ -491,16 +451,10 @@ ALTER TABLE ONLY __DATABASE_TARGET_SCHEMA__.osdescription ADD CONSTRAINT cl_coll CREATE INDEX IF NOT EXISTS idx_collection_osdescription ON __DATABASE_TARGET_SCHEMA__.osdescription (collection); -- CREATE INDEX IF NOT EXISTS idx_lang_osdescription ON __DATABASE_TARGET_SCHEMA__.osdescription (lang); --- [TABLE __DATABASE_TARGET_SCHEMA__.facet] -CREATE INDEX IF NOT EXISTS idx_id_facet ON __DATABASE_TARGET_SCHEMA__.facet (public.normalize(id)); -CREATE INDEX IF NOT EXISTS idx_pid_facet ON __DATABASE_TARGET_SCHEMA__.facet (public.normalize(pid)); -CREATE INDEX IF NOT EXISTS idx_type_facet ON __DATABASE_TARGET_SCHEMA__.facet (type); -CREATE INDEX IF NOT EXISTS idx_collection_facet ON __DATABASE_TARGET_SCHEMA__.facet (public.normalize(collection)); -CREATE INDEX IF NOT EXISTS idx_value_facet ON __DATABASE_TARGET_SCHEMA__.facet USING GIN (public.normalize(value) gin_trgm_ops); - -- [TABLE __DATABASE_TARGET_SCHEMA__.catalog] CREATE INDEX IF NOT EXISTS idx_id_catalog ON __DATABASE_TARGET_SCHEMA__.catalog (public.normalize(id)); CREATE INDEX IF NOT EXISTS idx_description_catalog ON __DATABASE_TARGET_SCHEMA__.catalog USING GIN (public.normalize(description) gin_trgm_ops); +CREATE INDEX IF NOT EXISTS idx_hashtag_catalog ON __DATABASE_TARGET_SCHEMA__.catalog USING GIN (public.normalize(hashtag) gin_trgm_ops); CREATE INDEX IF NOT EXISTS idx_level_catalog ON __DATABASE_TARGET_SCHEMA__.catalog USING btree (level); -- [TABLE __DATABASE_TARGET_SCHEMA__.hashtag]