Skip to content

Commit d15ee85

Browse files
Prevent property not allowed in terms query
1 parent b0450eb commit d15ee85

File tree

5 files changed

+38
-6
lines changed

5 files changed

+38
-6
lines changed

stac_fastapi/core/stac_fastapi/core/extensions/filter.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@
2727
}
2828

2929

30+
def _get_value_for_terms_query(value: dict):
31+
"""Return property value for terms queries."""
32+
if isinstance(value, dict):
33+
if "property" in value:
34+
return value["property"]
35+
return value
36+
37+
3038
def _replace_like_patterns(match: re.Match) -> str:
3139
pattern = match.group()
3240
try:
@@ -159,9 +167,15 @@ def to_es(queryables_mapping: Dict[str, Any], query: Dict[str, Any]) -> Dict[str
159167
return {"range": {field: {range_op[query["op"]]: value}}}
160168
else:
161169
if query["op"] == ComparisonOp.EQ:
162-
return {"term": {field: value}}
170+
return {"term": {field: _get_value_for_terms_query(value)}}
163171
elif query["op"] == ComparisonOp.NEQ:
164-
return {"bool": {"must_not": [{"term": {field: value}}]}}
172+
return {
173+
"bool": {
174+
"must_not": [
175+
{"term": {field: _get_value_for_terms_query(value)}}
176+
]
177+
}
178+
}
165179
else:
166180
return {"range": {field: {range_op[query["op"]]: value}}}
167181

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

188202
elif query["op"] == AdvancedComparisonOp.LIKE:
189203
field = to_es_field(queryables_mapping, query["args"][0]["property"])
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
"""library version."""
2-
__version__ = "4.2.0"
2+
__version__ = "4.2.1"

stac_fastapi/elasticsearch/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
desc = f.read()
77

88
install_requires = [
9-
"stac-fastapi-core==4.2.0",
9+
"stac-fastapi-core==4.2.1",
1010
"elasticsearch[async]~=8.18.0",
1111
"uvicorn~=0.23.0",
1212
"starlette>=0.35.0,<0.36.0",

stac_fastapi/opensearch/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
desc = f.read()
77

88
install_requires = [
9-
"stac-fastapi-core==4.2.0",
9+
"stac-fastapi-core==4.2.1",
1010
"opensearch-py~=2.8.0",
1111
"opensearch-py[async]~=2.8.0",
1212
"uvicorn~=0.23.0",

stac_fastapi/tests/extensions/test_filter.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,24 @@ async def test_search_filter_ext_and_get_cql2text_id(app_client, ctx):
159159
assert len(resp.json()["features"]) == 1
160160

161161

162+
@pytest.mark.asyncio
163+
async def test_search_filter_ext_cql_property_terms_query(app_client, ctx):
164+
"""Tests for terms queries not supporting property reference."""
165+
landsat_row = ctx.item["properties"]["landsat:row"]
166+
167+
cql2_text_property_queries = {
168+
f'properties.landsat:row="{landsat_row}"': 1,
169+
f'properties.landsat:row!="{landsat_row}"': 0,
170+
f'properties.landsat:row IN ("{landsat_row}")': 1,
171+
}
172+
173+
for filter, result in cql2_text_property_queries.items():
174+
resp = await app_client.get(f"/search?filter-lang=cql2-text&filter={filter}")
175+
content = resp.json()
176+
assert resp.status_code == 200
177+
assert len(content["features"]) == result
178+
179+
162180
@pytest.mark.asyncio
163181
async def test_search_filter_ext_and_get_cql2text_cloud_cover(app_client, ctx):
164182
collection = ctx.item["collection"]

0 commit comments

Comments
 (0)