Skip to content

Commit

Permalink
add free-text search extension
Browse files Browse the repository at this point in the history
  • Loading branch information
hrodmn committed Oct 17, 2024
1 parent ee19252 commit e5393c3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions stac_fastapi/pgstac/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from stac_fastapi.extensions.core import (
FieldsExtension,
FilterExtension,
FreeTextExtension,
SortExtension,
TokenPaginationExtension,
TransactionExtension,
Expand Down Expand Up @@ -46,6 +47,7 @@
"fields": FieldsExtension(),
"pagination": TokenPaginationExtension(),
"filter": FilterExtension(client=FiltersClient()),
"free_text": FreeTextExtension(),
"bulk_transactions": BulkTransactionExtension(client=BulkTransactionsClient()),
}

Expand All @@ -55,6 +57,7 @@
"sort": SortExtension(),
"fields": FieldsExtension(),
"filter": FilterExtension(client=FiltersClient()),
"free_text": FreeTextExtension(),
}

enabled_extensions = (
Expand Down
8 changes: 8 additions & 0 deletions stac_fastapi/pgstac/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ async def all_collections( # noqa: C901
sortby: Optional[str] = None,
filter: Optional[str] = None,
filter_lang: Optional[str] = None,
q: Optional[List[str]] = None,
**kwargs,
) -> Collections:
"""Cross catalog search (GET).
Expand Down Expand Up @@ -79,6 +80,7 @@ async def all_collections( # noqa: C901
sortby=sortby,
filter_query=filter,
filter_lang=filter_lang,
q=q,
)

async with request.app.state.get_connection(request, "r") as conn:
Expand Down Expand Up @@ -451,6 +453,7 @@ async def get_search(
sortby: Optional[str] = None,
filter: Optional[str] = None,
filter_lang: Optional[str] = None,
q: Optional[List[str]] = None,
**kwargs,
) -> ItemCollection:
"""Cross catalog search (GET).
Expand Down Expand Up @@ -478,6 +481,7 @@ async def get_search(
sortby=sortby,
filter_query=filter,
filter_lang=filter_lang,
q=q,
)

# Do the request
Expand All @@ -499,6 +503,7 @@ def clean_search_args( # noqa: C901
sortby: Optional[str] = None,
filter_query: Optional[str] = None,
filter_lang: Optional[str] = None,
q: Optional[List[str]] = None,
) -> Dict[str, Any]:
"""Clean up search arguments to match format expected by pgstac"""
if filter_query:
Expand Down Expand Up @@ -541,6 +546,9 @@ def clean_search_args( # noqa: C901
includes.add(field)
base_args["fields"] = {"include": includes, "exclude": excludes}

if q:
base_args["q"] = " OR ".join(q)

# Remove None values from dict
clean = {}
for k, v in base_args.items():
Expand Down
8 changes: 8 additions & 0 deletions tests/resources/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,11 @@ async def test_get_collections_search(
"/collections",
)
assert len(resp.json()["collections"]) == 2

# free-text
resp = await app_client.get(
"/collections",
params={"q": "temperature"},
)
assert len(resp.json()["collections"]) == 1
assert resp.json()["collections"][0]["id"] == load_test2_collection.id

0 comments on commit e5393c3

Please sign in to comment.