Skip to content

Commit af98395

Browse files
add free-text search extension (#162)
* add free-text search extension * add free text extension to test fixture * add free-text test to test_api.py * move tests * remove free-text from extensions map * Update changelog * Remove free-text from conftest extensions map * remove q from item search get request method --------- Co-authored-by: vincentsarago <vincent.sarago@gmail.com>
1 parent b65cd97 commit af98395

File tree

5 files changed

+33
-2
lines changed

5 files changed

+33
-2
lines changed

CHANGES.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
### Changed
66

7-
- Handle `next` and `dev` tokens now returned as links from pgstac>=0.9.0 (author @zstatmanweil, https://github.com/stac-utils/stac-fastapi-pgstac/pull/140)
7+
- Handle `next` and `dev` tokens now returned as links from pgstac>=0.9.0 (author @zstatmanweil, <https://github.com/stac-utils/stac-fastapi-pgstac/pull/140>)
88
- Add collection search extension ([#139](https://github.com/stac-utils/stac-fastapi-pgstac/pull/139))
99
- keep `/search` and `/collections` extensions separate ([#158](https://github.com/stac-utils/stac-fastapi-pgstac/pull/158))
1010
- update `pypgstac` requirement to `>=0.8,<0.10`
@@ -15,6 +15,7 @@
1515
- changed `datetime` input type to `string` in GET endpoint methods
1616
- renamed `filter` to `filter_expr` input attributes in GET endpoint methods
1717
- delete `utils.format_datetime_range` function
18+
- add [free-text extension](https://github.com/stac-api-extensions/freetext-search) to collection search extensions ([#162](https://github.com/stac-utils/stac-fastapi-pgstac/pull/162))
1819

1920
### Fixed
2021

@@ -23,7 +24,7 @@
2324

2425
## [3.0.1] - 2024-11-14
2526

26-
- Enable runtime `CORS` configuration using environment variables (`CORS_ORIGIN="https://...,https://..."`, `CORS_METHODS="PUT,OPTIONS"`) (https://github.com/stac-utils/stac-fastapi-pgstac/pull/168)
27+
- Enable runtime `CORS` configuration using environment variables (`CORS_ORIGIN="https://...,https://..."`, `CORS_METHODS="PUT,OPTIONS"`) (<https://github.com/stac-utils/stac-fastapi-pgstac/pull/168>)
2728

2829
## [3.0.0] - 2024-08-02
2930

stac_fastapi/pgstac/app.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from stac_fastapi.extensions.core import (
2525
FieldsExtension,
2626
FilterExtension,
27+
FreeTextExtension,
2728
OffsetPaginationExtension,
2829
SortExtension,
2930
TokenPaginationExtension,
@@ -62,6 +63,7 @@
6263
"sort": SortExtension(),
6364
"fields": FieldsExtension(),
6465
"filter": FilterExtension(client=FiltersClient()),
66+
"free_text": FreeTextExtension(),
6567
"pagination": OffsetPaginationExtension(),
6668
}
6769

stac_fastapi/pgstac/core.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ async def all_collections( # noqa: C901
5454
sortby: Optional[str] = None,
5555
filter_expr: Optional[str] = None,
5656
filter_lang: Optional[str] = None,
57+
q: Optional[List[str]] = None,
5758
**kwargs,
5859
) -> Collections:
5960
"""Cross catalog search (GET).
@@ -85,6 +86,7 @@ async def all_collections( # noqa: C901
8586
sortby=sortby,
8687
filter_query=filter_expr,
8788
filter_lang=filter_lang,
89+
q=q,
8890
)
8991

9092
async with request.app.state.get_connection(request, "r") as conn:
@@ -508,6 +510,7 @@ def _clean_search_args( # noqa: C901
508510
sortby: Optional[str] = None,
509511
filter_query: Optional[str] = None,
510512
filter_lang: Optional[str] = None,
513+
q: Optional[List[str]] = None,
511514
) -> Dict[str, Any]:
512515
"""Clean up search arguments to match format expected by pgstac"""
513516
if filter_query:
@@ -551,6 +554,9 @@ def _clean_search_args( # noqa: C901
551554

552555
base_args["fields"] = {"include": includes, "exclude": excludes}
553556

557+
if q:
558+
base_args["q"] = " OR ".join(q)
559+
554560
# Remove None values from dict
555561
clean = {}
556562
for k, v in base_args.items():

tests/conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
CollectionSearchExtension,
2828
FieldsExtension,
2929
FilterExtension,
30+
FreeTextExtension,
3031
OffsetPaginationExtension,
3132
SortExtension,
3233
TokenPaginationExtension,
@@ -148,6 +149,7 @@ def api_client(request, database):
148149
SortExtension(),
149150
FieldsExtension(),
150151
FilterExtension(client=FiltersClient()),
152+
FreeTextExtension(),
151153
OffsetPaginationExtension(),
152154
]
153155
collection_search_extension = CollectionSearchExtension.from_extensions(

tests/resources/test_collection.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,26 @@ async def test_get_collections_search(
307307
assert len(resp.json()["collections"]) == 2
308308

309309

310+
@requires_pgstac_0_9_2
311+
@pytest.mark.asyncio
312+
async def test_collection_search_freetext(
313+
app_client, load_test_collection, load_test2_collection
314+
):
315+
# free-text
316+
resp = await app_client.get(
317+
"/collections",
318+
params={"q": "temperature"},
319+
)
320+
assert len(resp.json()["collections"]) == 1
321+
assert resp.json()["collections"][0]["id"] == load_test2_collection.id
322+
323+
resp = await app_client.get(
324+
"/collections",
325+
params={"q": "nosuchthing"},
326+
)
327+
assert len(resp.json()["collections"]) == 0
328+
329+
310330
@requires_pgstac_0_9_2
311331
@pytest.mark.asyncio
312332
async def test_all_collections_with_pagination(app_client, load_test_data):

0 commit comments

Comments
 (0)