Skip to content

Commit b097027

Browse files
authored
Merge pull request #36 from stac-utils/no_indices
Create better error messaging when indices haven't been created yet
2 parents 41e7b44 + fc63010 commit b097027

File tree

1 file changed

+18
-8
lines changed
  • stac_fastapi/elasticsearch/stac_fastapi/elasticsearch

1 file changed

+18
-8
lines changed

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,12 @@ def _lookup_id(id: str, table, session):
5252
def all_collections(self, **kwargs) -> Collections:
5353
"""Read all collections from the database."""
5454
base_url = str(kwargs["request"].base_url)
55-
collections = self.client.search(
56-
index="stac_collections", doc_type="_doc", query={"match_all": {}}
57-
)
55+
try:
56+
collections = self.client.search(
57+
index="stac_collections", doc_type="_doc", query={"match_all": {}}
58+
)
59+
except elasticsearch.exceptions.NotFoundError:
60+
raise NotFoundError("No collections exist")
5861
serialized_collections = [
5962
self.collection_serializer.db_to_stac(
6063
collection["_source"], base_url=base_url
@@ -99,15 +102,16 @@ def item_collection(
99102
"""Read an item collection from the database."""
100103
links = []
101104
base_url = str(kwargs["request"].base_url)
102-
103105
search = Search(using=self.client, index="stac_items")
104106

105107
collection_filter = Q(
106108
"bool", should=[Q("match_phrase", **{"collection": collection_id})]
107109
)
108110
search = search.query(collection_filter)
109-
110-
count = search.count()
111+
try:
112+
count = search.count()
113+
except elasticsearch.exceptions.NotFoundError:
114+
raise NotFoundError("No items exist")
111115
# search = search.sort({"id.keyword" : {"order" : "asc"}})
112116
search = search.query()[0:limit]
113117
collection_children = search.execute().to_dict()
@@ -138,7 +142,9 @@ def get_item(self, item_id: str, collection_id: str, **kwargs) -> Item:
138142
try:
139143
item = self.client.get(index="stac_items", id=item_id)
140144
except elasticsearch.exceptions.NotFoundError:
141-
raise NotFoundError
145+
raise NotFoundError(
146+
f"Item {item_id} does not exist in Collection {collection_id}"
147+
)
142148
return self.item_serializer.db_to_stac(item["_source"], base_url)
143149

144150
def _return_date(self, datetime):
@@ -316,7 +322,11 @@ def post_search(
316322
field = sort.field + ".keyword"
317323
search = search.sort({field: {"order": sort.direction}})
318324

319-
count = search.count()
325+
try:
326+
count = search.count()
327+
except elasticsearch.exceptions.NotFoundError:
328+
raise NotFoundError("No items exist")
329+
320330
# search = search.sort({"id.keyword" : {"order" : "asc"}})
321331
search = search.query()[0 : search_request.limit]
322332
response = search.execute().to_dict()

0 commit comments

Comments
 (0)