diff --git a/.gitignore b/.gitignore index f00589ff..0bf0e27b 100644 --- a/.gitignore +++ b/.gitignore @@ -131,6 +131,8 @@ dmypy.json # mongo db data /mongo_data +/esdata + # Virtualenv venv diff --git a/docker-compose.elasticsearch.yml b/docker-compose.elasticsearch.yml index 9696bc94..9165064d 100644 --- a/docker-compose.elasticsearch.yml +++ b/docker-compose.elasticsearch.yml @@ -24,6 +24,7 @@ services: volumes: - ./stac_fastapi:/app/stac_fastapi - ./scripts:/app/scripts + - ./esdata:/usr/share/elasticsearch/data depends_on: - elasticsearch command: @@ -44,4 +45,4 @@ services: networks: default: - name: stac-fastapi-network + name: stac-fastapi-es-network diff --git a/stac_fastapi/elasticsearch/setup.py b/stac_fastapi/elasticsearch/setup.py index 196cd26a..93653197 100644 --- a/stac_fastapi/elasticsearch/setup.py +++ b/stac_fastapi/elasticsearch/setup.py @@ -33,7 +33,7 @@ setup( - name="stac-fastapi.elasticsearch", + name="stac-fastapi.nosql.elasticsearch", description="An implementation of STAC API based on the FastAPI framework.", long_description=desc, long_description_content_type="text/markdown", diff --git a/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py b/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py index c17b2dce..89ce4989 100644 --- a/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py +++ b/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py @@ -100,11 +100,17 @@ def item_collection( links = [] base_url = str(kwargs["request"].base_url) - collection_children = self.client.search( - index="stac_items", - doc_type="_doc", - query={"match_phrase": {"collection": collection_id}}, + 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() + # search = search.sort({"id.keyword" : {"order" : "asc"}}) + search = search.query()[0:limit] + collection_children = search.execute().to_dict() serialized_children = [ self.item_serializer.db_to_stac(item["_source"], base_url=base_url) @@ -113,8 +119,11 @@ def item_collection( context_obj = None if self.extension_is_enabled("ContextExtension"): - count = len(serialized_children) - context_obj = {"returned": count, "limit": limit, "matched": count} + context_obj = { + "returned": count if count < limit else limit, + "limit": limit, + "matched": count, + } return ItemCollection( type="FeatureCollection", @@ -307,7 +316,9 @@ def post_search( field = sort.field + ".keyword" search = search.sort({field: {"order": sort.direction}}) + count = search.count() # search = search.sort({"id.keyword" : {"order" : "asc"}}) + search = search.query()[0 : search_request.limit] response = search.execute().to_dict() if len(response["hits"]["hits"]) > 0: @@ -347,9 +358,8 @@ def post_search( limit = 10 context_obj = None if self.extension_is_enabled("ContextExtension"): - count = len(response_features) context_obj = { - "returned": count if count <= 10 else limit, + "returned": count if count < limit else limit, "limit": limit, "matched": count, } diff --git a/stac_fastapi/mongo/setup.py b/stac_fastapi/mongo/setup.py index f0737ca1..847e32b6 100644 --- a/stac_fastapi/mongo/setup.py +++ b/stac_fastapi/mongo/setup.py @@ -32,7 +32,7 @@ setup( - name="stac-fastapi.mongo", + name="stac-fastapi.nosql.mongo", description="An implementation of STAC API based on the FastAPI framework.", long_description=desc, long_description_content_type="text/markdown", diff --git a/stac_fastapi/mongo/stac_fastapi/mongo/core.py b/stac_fastapi/mongo/stac_fastapi/mongo/core.py index b9927ed1..e7c11fec 100644 --- a/stac_fastapi/mongo/stac_fastapi/mongo/core.py +++ b/stac_fastapi/mongo/stac_fastapi/mongo/core.py @@ -103,12 +103,19 @@ def item_collection( base_url = str(kwargs["request"].base_url) with self.client.start_session() as session: - collection_children = self.item_table.find( - {"collection": collection_id}, session=session - ).sort( - [("properties.datetime", pymongo.ASCENDING), ("id", pymongo.ASCENDING)] + collection_children = ( + self.item_table.find({"collection": collection_id}, session=session) + .limit(limit) + .sort( + [ + ("properties.datetime", pymongo.ASCENDING), + ("id", pymongo.ASCENDING), + ] + ) ) + matched = self.item_table.count_documents({"collection": collection_id}) + for item in collection_children: response_features.append( self.item_serializer.db_to_stac(item, base_url=base_url) @@ -120,7 +127,7 @@ def item_collection( context_obj = { "returned": count if count <= 10 else limit, "limit": limit, - "matched": len(response_features) or None, + "matched": matched or None, } return ItemCollection( @@ -296,6 +303,8 @@ def post_search( .sort(sort_list) ) + matched = self.item_table.count_documents(queries) + results = [] links = [] @@ -329,7 +338,7 @@ def post_search( context_obj = { "returned": count if count <= 10 else search_request.limit, "limit": search_request.limit, - "matched": len(results) or None, + "matched": matched or None, } return ItemCollection(