Skip to content
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

Extending temporal search #182

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
2f8d666
Extending datetime search to include start_datetime and end_datetime.
rhysrevans3 Jan 5, 2024
7b33232
Removing _return_date bounding constants.
rhysrevans3 Jan 5, 2024
0a6616e
Setting elasticsearch hostname for dockercompose.
rhysrevans3 Jan 8, 2024
53d44ce
Adding extra test for start_datetime/end_datetime intersecting searches.
rhysrevans3 Jan 8, 2024
18d4be1
Fixing broken tests.
rhysrevans3 Jan 8, 2024
7eddfee
Updating apply_datetime_filter doc string.
rhysrevans3 Jan 8, 2024
65ebbea
Adding changes to change log.
rhysrevans3 Jan 8, 2024
b514f19
Merge branch 'main' into extend_datetime_search
jonhealy1 Feb 1, 2024
5143f00
Adding extra tests.
rhysrevans3 Feb 13, 2024
03c9701
Merge branch 'extend_datetime_search' of github.com:rhysrevans3/stac-…
rhysrevans3 Apr 9, 2024
7595509
Merge branch 'main' of github.com:stac-utils/stac-fastapi-elasticsear…
rhysrevans3 Apr 9, 2024
8ed0928
Adding datetime extension to opensearch.
rhysrevans3 Apr 9, 2024
0a6b9f1
Merge branch main
rhysrevans3 May 10, 2024
5212768
Remove unneeded required install.
rhysrevans3 May 13, 2024
0c1e08f
Update stac_pydantic requirement.
rhysrevans3 May 22, 2024
a2ec61e
Merge branch 'main' of github.com:stac-utils/stac-fastapi-elasticsear…
rhysrevans3 May 22, 2024
c69c9e5
Fixing datetime type issue.
rhysrevans3 May 23, 2024
705bcb1
Fix for datetime format.
rhysrevans3 May 23, 2024
d15173e
Pinning specific version of stac pydantic.
rhysrevans3 May 24, 2024
4f31ac7
Adding missing .git.
rhysrevans3 May 24, 2024
29ea9ba
Switch to https.
rhysrevans3 May 24, 2024
44fd039
Removing stac pydantic requirements.
rhysrevans3 May 24, 2024
ea7646c
Merge branch 'main' of github.com:stac-utils/stac-fastapi-elasticsear…
rhysrevans3 Jun 3, 2024
d003db1
Using deepcopy for multiple item creation.
rhysrevans3 Jun 3, 2024
0d89769
Fixing dt format error.
rhysrevans3 Jun 3, 2024
17818d3
Merge branch 'main' into extend_datetime_search
rhysrevans3 Jun 11, 2024
fb41c03
Merge branch 'main' into extend_datetime_search
rhysrevans3 Jun 18, 2024
8738bdc
Merge branch 'main' into extend_datetime_search
jonhealy1 Oct 6, 2024
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 @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Changed

- Extended Datetime Search to search on start_datetime and end_datetime as well as datetime fields. [#182](https://github.com/stac-utils/stac-fastapi-elasticsearch/pull/182)

- Elasticsearch drivers from 7.17.9 to 8.11.0 [#169](https://github.com/stac-utils/stac-fastapi-elasticsearch/pull/169)

### Fixed
Expand Down
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ services:
- RELOAD=true
- ENVIRONMENT=local
- WEB_CONCURRENCY=10
- ES_HOST=172.17.0.1
- ES_HOST=elasticsearch
- ES_PORT=9200
- ES_USE_SSL=false
- ES_VERIFY_CERTS=false
Expand All @@ -32,6 +32,7 @@ services:
elasticsearch:
container_name: es-container
image: docker.elastic.co/elasticsearch/elasticsearch:${ELASTICSEARCH_VERSION:-8.11.0}
hostname: elasticsearch
environment:
ES_JAVA_OPTS: -Xms512m -Xmx1g
volumes:
Expand Down
22 changes: 5 additions & 17 deletions stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,22 +289,10 @@ def _return_date(interval_str):
datetime = f"{intervals[0][0:19]}Z"
return {"eq": datetime}
else:
start_date = intervals[0]
end_date = intervals[1]
if ".." not in intervals:
start_date = f"{start_date[0:19]}Z"
end_date = f"{end_date[0:19]}Z"
elif start_date != "..":
start_date = f"{start_date[0:19]}Z"
end_date = "2200-12-01T12:31:12Z"
elif end_date != "..":
start_date = "1900-10-01T00:00:00Z"
end_date = f"{end_date[0:19]}Z"
else:
start_date = "1900-10-01T00:00:00Z"
end_date = "2200-12-01T12:31:12Z"
start_date = f"{intervals[0][0:19]}Z" if intervals[0] != ".." else None
end_date = f"{intervals[1][0:19]}Z" if intervals[1] != ".." else None

return {"lte": end_date, "gte": start_date}
return {"lte": end_date, "gte": start_date}

async def get_search(
self,
Expand Down Expand Up @@ -457,9 +445,9 @@ async def post_search(
)

if search_request.query:
for (field_name, expr) in search_request.query.items():
for field_name, expr in search_request.query.items():
field = "properties__" + field_name
for (op, value) in expr.items():
for op, value in expr.items():
search = self.database.apply_stacql_filter(
search=search, op=op, field=field, value=value
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,8 @@ def apply_collections_filter(search: Search, collection_ids: List[str]):
return search.filter("terms", collection=collection_ids)

@staticmethod
def apply_datetime_filter(search: Search, datetime_search):
"""Apply a filter to search based on datetime field.
def apply_datetime_filter(search: Search, datetime_search: dict):
"""Apply a filter to search on datetime, start_datetime, and end_datetime fields.

Args:
search (Search): The search object to filter.
Expand All @@ -388,17 +388,101 @@ def apply_datetime_filter(search: Search, datetime_search):
Returns:
Search: The filtered search object.
"""
should = []

if "eq" in datetime_search:
search = search.filter(
"term", **{"properties__datetime": datetime_search["eq"]}
should.extend(
[
Q(
"bool",
filter=[
Q(
"term",
properties__datetime=datetime_search["eq"],
),
],
),
Q(
"bool",
filter=[
Q(
"range",
properties__start_datetime={
"lte": datetime_search["eq"],
},
),
Q(
"range",
properties__end_datetime={
"gte": datetime_search["eq"],
},
),
],
),
]
)

else:
search = search.filter(
"range", properties__datetime={"lte": datetime_search["lte"]}
)
search = search.filter(
"range", properties__datetime={"gte": datetime_search["gte"]}
should.extend(
[
Q(
"bool",
filter=[
Q(
"range",
properties__datetime={
"gte": datetime_search["gte"],
"lte": datetime_search["lte"],
},
),
],
),
Q(
"bool",
filter=[
Q(
"range",
properties__start_datetime={
"gte": datetime_search["gte"],
"lte": datetime_search["lte"],
},
),
],
),
Q(
"bool",
filter=[
Q(
"range",
properties__end_datetime={
"gte": datetime_search["gte"],
"lte": datetime_search["lte"],
},
),
],
),
Q(
"bool",
filter=[
Q(
"range",
properties__start_datetime={
"lte": datetime_search["gte"]
},
),
Q(
"range",
properties__end_datetime={
"gte": datetime_search["lte"]
},
),
],
),
]
)

search = search.query(Q("bool", filter=[Q("bool", should=should)]))

return search

@staticmethod
Expand Down
8 changes: 7 additions & 1 deletion stac_fastapi/elasticsearch/tests/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,13 @@ async def test_app_fields_extension_return_all_properties(app_client, ctx, txn_c
feature = resp_json["features"][0]
assert len(feature["properties"]) >= len(item["properties"])
for expected_prop, expected_value in item["properties"].items():
if expected_prop in ("datetime", "created", "updated"):
if expected_prop in (
"datetime",
"start_datetime",
"end_datetime",
"created",
"updated",
):
assert feature["properties"][expected_prop][0:19] == expected_value[0:19]
else:
assert feature["properties"][expected_prop] == expected_value
Expand Down
2 changes: 2 additions & 0 deletions stac_fastapi/elasticsearch/tests/data/test_item.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
},
"properties": {
"datetime": "2020-02-12T12:30:22Z",
"start_datetime": "2020-02-08T12:30:22Z",
"end_datetime": "2020-02-16T12:30:22Z",
"landsat:scene_id": "LC82081612020043LGN00",
"landsat:row": "161",
"gsd": 15,
Expand Down
19 changes: 19 additions & 0 deletions stac_fastapi/elasticsearch/tests/resources/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,25 @@ async def test_item_search_temporal_window_post(app_client, ctx):
assert resp_json["features"][0]["id"] == test_item["id"]


@pytest.mark.asyncio
async def test_item_search_temporal_intersecting_window_post(app_client, ctx):
rhysrevans3 marked this conversation as resolved.
Show resolved Hide resolved
"""Test POST search with two-tailed spatio-temporal query (core)"""
test_item = ctx.item

item_date = rfc3339_str_to_datetime(test_item["properties"]["datetime"])
item_date_before = item_date - timedelta(days=10)
item_date_after = item_date - timedelta(days=2)

params = {
"collections": [test_item["collection"]],
"intersects": test_item["geometry"],
"datetime": f"{datetime_to_str(item_date_before)}/{datetime_to_str(item_date_after)}",
}
resp = await app_client.post("/search", json=params)
resp_json = resp.json()
assert resp_json["features"][0]["id"] == test_item["id"]


@pytest.mark.asyncio
@pytest.mark.skip(reason="KeyError: 'features")
async def test_item_search_temporal_open_window(app_client, ctx):
Expand Down
Loading