Skip to content

Create better error messaging when indices haven't been created yet #36

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

Merged
merged 6 commits into from
Mar 2, 2022
Merged
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
26 changes: 18 additions & 8 deletions stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@ def _lookup_id(id: str, table, session):
def all_collections(self, **kwargs) -> Collections:
"""Read all collections from the database."""
base_url = str(kwargs["request"].base_url)
collections = self.client.search(
index="stac_collections", doc_type="_doc", query={"match_all": {}}
)
try:
collections = self.client.search(
index="stac_collections", doc_type="_doc", query={"match_all": {}}
)
except elasticsearch.exceptions.NotFoundError:
raise NotFoundError("No collections exist")
serialized_collections = [
self.collection_serializer.db_to_stac(
collection["_source"], base_url=base_url
Expand Down Expand Up @@ -99,15 +102,16 @@ def item_collection(
"""Read an item collection from the database."""
links = []
base_url = str(kwargs["request"].base_url)

search = Search(using=self.client, index="stac_items")

collection_filter = Q(
"bool", should=[Q("match_phrase", **{"collection": collection_id})]
)
search = search.query(collection_filter)

count = search.count()
try:
count = search.count()
except elasticsearch.exceptions.NotFoundError:
raise NotFoundError("No items exist")
# search = search.sort({"id.keyword" : {"order" : "asc"}})
search = search.query()[0:limit]
collection_children = search.execute().to_dict()
Expand Down Expand Up @@ -138,7 +142,9 @@ def get_item(self, item_id: str, collection_id: str, **kwargs) -> Item:
try:
item = self.client.get(index="stac_items", id=item_id)
except elasticsearch.exceptions.NotFoundError:
raise NotFoundError
raise NotFoundError(
f"Item {item_id} does not exist in Collection {collection_id}"
)
return self.item_serializer.db_to_stac(item["_source"], base_url)

def _return_date(self, datetime):
Expand Down Expand Up @@ -316,7 +322,11 @@ def post_search(
field = sort.field + ".keyword"
search = search.sort({field: {"order": sort.direction}})

count = search.count()
try:
count = search.count()
except elasticsearch.exceptions.NotFoundError:
raise NotFoundError("No items exist")

# search = search.sort({"id.keyword" : {"order" : "asc"}})
search = search.query()[0 : search_request.limit]
response = search.execute().to_dict()
Expand Down