Skip to content

Commit 4e12664

Browse files
authored
Merge pull request #136 from ahuarte47/main_fix-filters-with-floats
[FIX-135]: Fix automatic converstion of float values to int
2 parents 0085835 + 34dea4b commit 4e12664

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1212
### Fixed
1313

1414
- Corrected the closing of client connections in ES index management functions [#132](https://github.com/stac-utils/stac-fastapi-elasticsearch/issues/132)
15+
- Corrected the automatic converstion of float values to int when building Filter Clauses [#135](https://github.com/stac-utils/stac-fastapi-elasticsearch/issues/135)
1516

1617
## [v0.3.0]
1718

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/extensions/filter.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,23 @@ def to_es(self):
114114
return self.date.isoformat()
115115

116116

117+
class FloatInt(float):
118+
"""Representation of Float/Int."""
119+
120+
@classmethod
121+
def __get_validators__(cls):
122+
"""Return validator to use."""
123+
yield cls.validate
124+
125+
@classmethod
126+
def validate(cls, v):
127+
"""Validate input value."""
128+
if isinstance(v, float):
129+
return v
130+
else:
131+
return int(v)
132+
133+
117134
Arg = Union[
118135
"Clause",
119136
PropertyReference,
@@ -126,8 +143,7 @@ def to_es(self):
126143
Polygon,
127144
MultiPolygon,
128145
GeometryCollection,
129-
int,
130-
float,
146+
FloatInt,
131147
str,
132148
bool,
133149
]

stac_fastapi/elasticsearch/tests/extensions/test_filter.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,34 @@ async def test_search_filter_ext_and(app_client, ctx):
7878

7979
assert resp.status_code == 200
8080
assert len(resp.json()["features"]) == 1
81+
82+
83+
async def test_search_filter_extension_floats(app_client, ctx):
84+
sun_elevation = ctx.item["properties"]["view:sun_elevation"]
85+
86+
params = {
87+
"filter": {
88+
"op": "and",
89+
"args": [
90+
{"op": "=", "args": [{"property": "id"}, ctx.item["id"]]},
91+
{
92+
"op": ">",
93+
"args": [
94+
{"property": "properties.view:sun_elevation"},
95+
sun_elevation - 0.01,
96+
],
97+
},
98+
{
99+
"op": "<",
100+
"args": [
101+
{"property": "properties.view:sun_elevation"},
102+
sun_elevation + 0.01,
103+
],
104+
},
105+
],
106+
}
107+
}
108+
resp = await app_client.post("/search", json=params)
109+
110+
assert resp.status_code == 200
111+
assert len(resp.json()["features"]) == 1

0 commit comments

Comments
 (0)