From d3e6591cdb3c5aa2e4f46b3c27cec9ec14d4b1d9 Mon Sep 17 00:00:00 2001 From: jonhealy1 Date: Tue, 1 Mar 2022 00:03:01 -0800 Subject: [PATCH 1/5] fix all_collections missing index --- .../elasticsearch/stac_fastapi/elasticsearch/core.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py b/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py index 89ce4989..0faea23c 100644 --- a/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py +++ b/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py @@ -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(f"No collections exist in the database yet") serialized_collections = [ self.collection_serializer.db_to_stac( collection["_source"], base_url=base_url From 1262e89db2063c4ca0a467ab209ba4b2e2171995 Mon Sep 17 00:00:00 2001 From: jonhealy1 Date: Tue, 1 Mar 2022 00:13:11 -0800 Subject: [PATCH 2/5] fix item collection, single item error messages --- .../elasticsearch/stac_fastapi/elasticsearch/core.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py b/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py index 0faea23c..57815a20 100644 --- a/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py +++ b/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py @@ -102,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(f"No items exist for this collection yet") # search = search.sort({"id.keyword" : {"order" : "asc"}}) search = search.query()[0:limit] collection_children = search.execute().to_dict() @@ -141,7 +142,7 @@ 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): From 75423980f835dc9df55a8d8527cf09ca902e394c Mon Sep 17 00:00:00 2001 From: jonhealy1 Date: Tue, 1 Mar 2022 01:39:31 -0800 Subject: [PATCH 3/5] Add error message to post search --- .../elasticsearch/stac_fastapi/elasticsearch/core.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py b/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py index 57815a20..863a0e26 100644 --- a/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py +++ b/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py @@ -320,7 +320,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 have been added to the database yet") + # search = search.sort({"id.keyword" : {"order" : "asc"}}) search = search.query()[0 : search_request.limit] response = search.execute().to_dict() From 2e7073ce979b85fe941386d9ae2ea67d1a0db791 Mon Sep 17 00:00:00 2001 From: jonhealy1 Date: Tue, 1 Mar 2022 01:54:54 -0800 Subject: [PATCH 4/5] run pre-commit --- .../elasticsearch/stac_fastapi/elasticsearch/core.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py b/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py index 863a0e26..54958aa8 100644 --- a/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py +++ b/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py @@ -57,7 +57,7 @@ def all_collections(self, **kwargs) -> Collections: index="stac_collections", doc_type="_doc", query={"match_all": {}} ) except elasticsearch.exceptions.NotFoundError: - raise NotFoundError(f"No collections exist in the database yet") + raise NotFoundError("No collections exist in the database yet") serialized_collections = [ self.collection_serializer.db_to_stac( collection["_source"], base_url=base_url @@ -111,7 +111,7 @@ def item_collection( try: count = search.count() except elasticsearch.exceptions.NotFoundError: - raise NotFoundError(f"No items exist for this collection yet") + raise NotFoundError("No items exist for this collection yet") # search = search.sort({"id.keyword" : {"order" : "asc"}}) search = search.query()[0:limit] collection_children = search.execute().to_dict() @@ -142,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(f"Item {item_id} does not exist in Collection {collection_id}") + 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): From 03a9c767440076bd9d3748ab189d966dab944cfd Mon Sep 17 00:00:00 2001 From: jonhealy1 Date: Tue, 1 Mar 2022 11:42:16 -0800 Subject: [PATCH 5/5] clean up error messaging --- .../elasticsearch/stac_fastapi/elasticsearch/core.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py b/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py index 54958aa8..8c3468fe 100644 --- a/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py +++ b/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py @@ -57,7 +57,7 @@ def all_collections(self, **kwargs) -> Collections: index="stac_collections", doc_type="_doc", query={"match_all": {}} ) except elasticsearch.exceptions.NotFoundError: - raise NotFoundError("No collections exist in the database yet") + raise NotFoundError("No collections exist") serialized_collections = [ self.collection_serializer.db_to_stac( collection["_source"], base_url=base_url @@ -111,7 +111,7 @@ def item_collection( try: count = search.count() except elasticsearch.exceptions.NotFoundError: - raise NotFoundError("No items exist for this collection yet") + raise NotFoundError("No items exist") # search = search.sort({"id.keyword" : {"order" : "asc"}}) search = search.query()[0:limit] collection_children = search.execute().to_dict() @@ -325,7 +325,7 @@ def post_search( try: count = search.count() except elasticsearch.exceptions.NotFoundError: - raise NotFoundError("No items have been added to the database yet") + raise NotFoundError("No items exist") # search = search.sort({"id.keyword" : {"order" : "asc"}}) search = search.query()[0 : search_request.limit]