Skip to content

Prevent property not allowed in terms query #383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

- Filter Extension - patch terms value to prevent 'property reference not allowed in terms query' 500

### Added

### Changed
Expand Down
20 changes: 17 additions & 3 deletions stac_fastapi/core/stac_fastapi/core/extensions/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@
}


def _get_value_for_terms_query(value: dict):
"""Return property value for terms queries."""
if isinstance(value, dict):
if "property" in value:
return value["property"]
return value


def _replace_like_patterns(match: re.Match) -> str:
pattern = match.group()
try:
Expand Down Expand Up @@ -159,9 +167,15 @@ def to_es(queryables_mapping: Dict[str, Any], query: Dict[str, Any]) -> Dict[str
return {"range": {field: {range_op[query["op"]]: value}}}
else:
if query["op"] == ComparisonOp.EQ:
return {"term": {field: value}}
return {"term": {field: _get_value_for_terms_query(value)}}
elif query["op"] == ComparisonOp.NEQ:
return {"bool": {"must_not": [{"term": {field: value}}]}}
return {
"bool": {
"must_not": [
{"term": {field: _get_value_for_terms_query(value)}}
]
}
}
else:
return {"range": {field: {range_op[query["op"]]: value}}}

Expand All @@ -183,7 +197,7 @@ def to_es(queryables_mapping: Dict[str, Any], query: Dict[str, Any]) -> Dict[str
values = query["args"][1]
if not isinstance(values, list):
raise ValueError(f"Arg {values} is not a list")
return {"terms": {field: values}}
return {"terms": {field: [_get_value_for_terms_query(x) for x in values]}}

elif query["op"] == AdvancedComparisonOp.LIKE:
field = to_es_field(queryables_mapping, query["args"][0]["property"])
Expand Down
2 changes: 1 addition & 1 deletion stac_fastapi/core/stac_fastapi/core/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"""library version."""
__version__ = "4.2.0"
__version__ = "4.2.1"
2 changes: 1 addition & 1 deletion stac_fastapi/elasticsearch/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
desc = f.read()

install_requires = [
"stac-fastapi-core==4.2.0",
"stac-fastapi-core==4.2.1",
"elasticsearch[async]~=8.18.0",
"uvicorn~=0.23.0",
"starlette>=0.35.0,<0.36.0",
Expand Down
2 changes: 1 addition & 1 deletion stac_fastapi/opensearch/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
desc = f.read()

install_requires = [
"stac-fastapi-core==4.2.0",
"stac-fastapi-core==4.2.1",
"opensearch-py~=2.8.0",
"opensearch-py[async]~=2.8.0",
"uvicorn~=0.23.0",
Expand Down
18 changes: 18 additions & 0 deletions stac_fastapi/tests/extensions/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,24 @@ async def test_search_filter_ext_and_get_cql2text_id(app_client, ctx):
assert len(resp.json()["features"]) == 1


@pytest.mark.asyncio
async def test_search_filter_ext_cql_property_terms_query(app_client, ctx):
"""Tests for terms queries not supporting property reference."""
landsat_row = ctx.item["properties"]["landsat:row"]

cql2_text_property_queries = {
f'properties.landsat:row="{landsat_row}"': 1,
f'properties.landsat:row!="{landsat_row}"': 0,
f'properties.landsat:row IN ("{landsat_row}")': 1,
}

for filter, result in cql2_text_property_queries.items():
resp = await app_client.get(f"/search?filter-lang=cql2-text&filter={filter}")
content = resp.json()
assert resp.status_code == 200
assert len(content["features"]) == result


@pytest.mark.asyncio
async def test_search_filter_ext_and_get_cql2text_cloud_cover(app_client, ctx):
collection = ctx.item["collection"]
Expand Down