Skip to content

Commit

Permalink
Merge pull request #85 from ecmwf-projects/COPDS-2037-search-starred
Browse files Browse the repository at this point in the history
Starred search implementation
  • Loading branch information
keul authored Sep 12, 2024
2 parents 292f07c + bf61aa6 commit 73f9d6d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 9 deletions.
7 changes: 5 additions & 2 deletions cads_catalogue_api_service/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ def load_catalogue(
"""Return the whole catalogue as a serialized structure."""
query = session.query(self.collection_table).options(*database.deferred_columns)
query_results = search_utils.apply_filters(
session, query, q, None, portals=portals
session, query, q, kw=None, idx=None, portals=portals
).all()
all_collections = [
collection_serializer(
Expand All @@ -489,6 +489,7 @@ def all_datasets(
request: fastapi.Request,
q: str | None = None,
kw: list[str] | None = [],
idx: list[str] | None = [],
sortby: extensions.CatalogueSortCriterion = extensions.CatalogueSortCriterion.update_desc,
page: int = 0,
limit: int = 999,
Expand All @@ -507,7 +508,9 @@ def all_datasets(
search = session.query(self.collection_table).options(
*database.deferred_columns
)
search = search_utils.apply_filters(session, search, q, kw, portals=portals)
search = search_utils.apply_filters(
session, search, q, kw, idx, portals=portals
)
count = search.count()
search = apply_sorting_and_limit(
search=search, q=q, sortby=sortby, page=page, limit=limit
Expand Down
42 changes: 37 additions & 5 deletions cads_catalogue_api_service/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import attr
import fastapi
import pydantic
import stac_fastapi.api
import stac_fastapi.types.extension

Expand All @@ -38,6 +39,9 @@ def datasets_search(
kw: list[str] | None = fastapi.Query(
default=[], description="Filter by keyword(s)"
),
idx: list[str] | None = fastapi.Query(
default=[], description="Filter by dataset IDs"
),
sortby: CatalogueSortCriterion = fastapi.Query(
default=CatalogueSortCriterion.update_desc
),
Expand All @@ -53,6 +57,7 @@ def datasets_search(
request=request,
q=q,
kw=kw,
idx=idx,
sortby=sortby,
page=page,
limit=limit,
Expand All @@ -61,6 +66,32 @@ def datasets_search(
)


class FormData(pydantic.BaseModel):
"""Search datasets valid payload."""

q: str = ""
kw: list[str] | None = []
idx: list[str] | None = []
sortby: CatalogueSortCriterion = CatalogueSortCriterion.update_desc
page: int = 0
limit: int = config.MAX_LIMIT
search_stats: bool = True


def datasets_search_post(request: fastapi.Request, data: FormData) -> dict[str, Any]:
"""Filter datasets based on search parameters."""
return datasets_search(
request=request,
q=data.q,
kw=data.kw,
idx=data.idx,
sortby=data.sortby,
page=data.page,
limit=data.limit,
search_stats=data.search_stats,
)


@attr.s
class DatasetsSearchExtension(stac_fastapi.types.extension.ApiExtension):
"""Datasets filter extension.
Expand Down Expand Up @@ -96,13 +127,14 @@ def register(self, app: fastapi.FastAPI) -> None:
name="Datasets Search",
path="/datasets",
methods=["GET"],
# endpoint=stac_fastapi.api.routes.create_async_endpoint(
# datasets_search,
# stac_fastapi.api.models.EmptyRequest,
# self.response_class,
# ),
endpoint=datasets_search,
)
self.router.add_api_route(
name="Datasets Search",
path="/datasets",
methods=["POST"],
endpoint=datasets_search_post,
)
app.include_router(self.router, tags=["Datasets Search Extension"])


Expand Down
4 changes: 4 additions & 0 deletions cads_catalogue_api_service/search_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def apply_filters(
search: sa.orm.Query,
q: str | None,
kw: list | None,
idx: list | None,
portals: list[str] | None = None,
):
"""Apply allowed search filters to the running query.
Expand All @@ -99,6 +100,9 @@ def apply_filters(
if portals:
search = search.filter(cads_catalogue.database.Resource.portal.in_(portals))

if idx:
search = search.filter(cads_catalogue.database.Resource.resource_uid.in_(idx))

# Faceted search
if kw:
# Facetes search criteria is to run on OR in the same category, and AND between categories
Expand Down
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ channels:
dependencies:
- attrs
- brotli-asgi
- fastapi>=0.93.0
- fastapi>=0.113.0
- httpx
- pip
- pydantic<2
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ dependencies = [
"attrs",
"cads-catalogue@git+https://github.com/ecmwf-projects/cads-catalogue.git",
"cads-common@git+https://github.com/ecmwf-projects/cads-common.git",
"fastapi",
"fastapi>=0.113.0",
"python-dateutil",
"sqlalchemy>=2.0.9",
"stac_fastapi.api",
Expand Down

0 comments on commit 73f9d6d

Please sign in to comment.