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

use a stable sort by default for all queries for Items #52

Merged
merged 14 commits into from
Mar 15, 2022
Merged
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Changed

- Elasticsearch index mappings updated to be more thorough.
- Endpoints that return items (e.g., /search) now sort the results by 'properties.datetime,id,collection'.
Previously, there was no sort order defined.

### Removed

Expand Down
30 changes: 20 additions & 10 deletions stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Item crud client."""
import json
import logging
from datetime import datetime
from datetime import datetime as datetime_type
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixes a warning about variables shadowing outer scope

from typing import List, Optional, Type, Union
from urllib.parse import urljoin

Expand Down Expand Up @@ -147,15 +147,16 @@ def get_item(self, item_id: str, collection_id: str, **kwargs) -> Item:
)
return self.item_serializer.db_to_stac(item["_source"], base_url)

def _return_date(self, datetime):
datetime = datetime.split("/")
if len(datetime) == 1:
datetime = datetime[0][0:19] + "Z"
@staticmethod
def _return_date(interval_str):
intervals = interval_str.split("/")
if len(intervals) == 1:
datetime = intervals[0][0:19] + "Z"
return {"eq": datetime}
else:
start_date = datetime[0]
end_date = datetime[1]
if ".." not in datetime:
start_date = intervals[0]
end_date = intervals[1]
if ".." not in intervals:
start_date = start_date[0:19] + "Z"
end_date = end_date[0:19] + "Z"
elif start_date != "..":
Expand All @@ -175,7 +176,7 @@ def get_search(
collections: Optional[List[str]] = None,
ids: Optional[List[str]] = None,
bbox: Optional[List[NumType]] = None,
datetime: Optional[Union[str, datetime]] = None,
datetime: Optional[Union[str, datetime_type]] = None,
limit: Optional[int] = 10,
query: Optional[str] = None,
token: Optional[str] = None,
Expand Down Expand Up @@ -238,7 +239,16 @@ def post_search(
) -> ItemCollection:
"""POST search catalog."""
base_url = str(kwargs["request"].base_url)
search = Search(using=self.client, index="stac_items")
search = (
Search()
.using(self.client)
.index("stac_items")
.sort(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

applies a sort now.

{"properties.datetime": {"order": "desc"}},
{"id": {"order": "desc"}},
{"collection": {"order": "desc"}},
)
)

if search_request.query:
if type(search_request.query) == str:
Expand Down