From 4e4b5eb3684b707a46a4df1fd2aa36a8327edc95 Mon Sep 17 00:00:00 2001 From: Stijn Caerts Date: Wed, 20 Dec 2023 09:03:13 +0100 Subject: [PATCH 1/2] #179: filter extension - set filter-lang=cql2-text as default for GET search --- .../stac_fastapi/elasticsearch/core.py | 6 +++--- .../tests/extensions/test_filter.py | 17 ++++++++++++++--- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py b/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py index 4fb9f174..12cc6b2c 100644 --- a/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py +++ b/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py @@ -380,12 +380,12 @@ async def get_search( base_args["sortby"] = sort_param if filter: - if filter_lang == "cql2-text": + if filter_lang == "cql2-json": base_args["filter-lang"] = "cql2-json" - base_args["filter"] = orjson.loads(to_cql2(parse_cql2_text(filter))) + base_args["filter"] = orjson.loads(unquote_plus(filter)) else: base_args["filter-lang"] = "cql2-json" - base_args["filter"] = orjson.loads(unquote_plus(filter)) + base_args["filter"] = orjson.loads(to_cql2(parse_cql2_text(filter))) if fields: includes = set() diff --git a/stac_fastapi/elasticsearch/tests/extensions/test_filter.py b/stac_fastapi/elasticsearch/tests/extensions/test_filter.py index d9db48cd..39fb8c25 100644 --- a/stac_fastapi/elasticsearch/tests/extensions/test_filter.py +++ b/stac_fastapi/elasticsearch/tests/extensions/test_filter.py @@ -101,6 +101,17 @@ async def test_search_filter_ext_and_get(app_client, ctx): assert len(resp.json()["features"]) == 1 +@pytest.mark.asyncio +async def test_search_filter_ext_and_get_id(app_client, ctx): + collection = ctx.item["collection"] + id = ctx.item["id"] + filter = f"id='{id}' AND collection='{collection}'" + resp = await app_client.get(f"/search?&filter={filter}") + + assert resp.status_code == 200 + assert len(resp.json()["features"]) == 1 + + @pytest.mark.asyncio async def test_search_filter_ext_and_get_cql2text_id(app_client, ctx): collection = ctx.item["collection"] @@ -162,21 +173,21 @@ async def test_search_filter_ext_and_post(app_client, ctx): @pytest.mark.asyncio async def test_search_filter_extension_floats_get(app_client, ctx): resp = await app_client.get( - """/search?filter={"op":"and","args":[{"op":"=","args":[{"property":"id"},"test-item"]},{"op":">","args":[{"property":"properties.view:sun_elevation"},"-37.30891534"]},{"op":"<","args":[{"property":"properties.view:sun_elevation"},"-37.30691534"]}]}""" + """/search?filter-lang=cql2-json&filter={"op":"and","args":[{"op":"=","args":[{"property":"id"},"test-item"]},{"op":">","args":[{"property":"properties.view:sun_elevation"},"-37.30891534"]},{"op":"<","args":[{"property":"properties.view:sun_elevation"},"-37.30691534"]}]}""" ) assert resp.status_code == 200 assert len(resp.json()["features"]) == 1 resp = await app_client.get( - """/search?filter={"op":"and","args":[{"op":"=","args":[{"property":"id"},"test-item-7"]},{"op":">","args":[{"property":"properties.view:sun_elevation"},"-37.30891534"]},{"op":"<","args":[{"property":"properties.view:sun_elevation"},"-37.30691534"]}]}""" + """/search?filter-lang=cql2-json&filter={"op":"and","args":[{"op":"=","args":[{"property":"id"},"test-item-7"]},{"op":">","args":[{"property":"properties.view:sun_elevation"},"-37.30891534"]},{"op":"<","args":[{"property":"properties.view:sun_elevation"},"-37.30691534"]}]}""" ) assert resp.status_code == 200 assert len(resp.json()["features"]) == 0 resp = await app_client.get( - """/search?filter={"op":"and","args":[{"op":"=","args":[{"property":"id"},"test-item"]},{"op":">","args":[{"property":"properties.view:sun_elevation"},"-37.30591534"]},{"op":"<","args":[{"property":"properties.view:sun_elevation"},"-37.30491534"]}]}""" + """/search?filter-lang=cql2-json&filter={"op":"and","args":[{"op":"=","args":[{"property":"id"},"test-item"]},{"op":">","args":[{"property":"properties.view:sun_elevation"},"-37.30591534"]},{"op":"<","args":[{"property":"properties.view:sun_elevation"},"-37.30491534"]}]}""" ) assert resp.status_code == 200 From 85eb50b04515d009746d91d53a0e02660a54426a Mon Sep 17 00:00:00 2001 From: Stijn Caerts Date: Wed, 20 Dec 2023 09:55:50 +0100 Subject: [PATCH 2/2] update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 94bec4de..eabf6e7c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Exclude unset fields in search response [#166](https://github.com/stac-utils/stac-fastapi-elasticsearch/issues/166) - Upgrade stac-fastapi to v2.4.9 [#172](https://github.com/stac-utils/stac-fastapi-elasticsearch/pull/172) +- Set correct default filter-lang for GET /search requests [#179](https://github.com/stac-utils/stac-fastapi-elasticsearch/issues/179) ## [v1.0.0]