Skip to content

Commit da30e88

Browse files
author
Phil Varner
committed
refactor execute_search to return count also
1 parent 561f3fe commit da30e88

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/core.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,7 @@ async def post_search(
250250
search=search, field=sort.field, direction=sort.direction
251251
)
252252

253-
maybe_count = await self.database.search_count(search=search)
254-
255-
response_features = await self.database.execute_search(
253+
response_features, maybe_count = await self.database.execute_search(
256254
search=search, limit=search_request.limit, base_url=base_url
257255
)
258256

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,14 @@ async def get_all_collections(self, base_url: str) -> List[Collection]:
7575
for c in collections["hits"]["hits"]
7676
]
7777

78-
async def search_count(self, search: Search) -> Optional[int]:
79-
"""Database logic to count search results."""
80-
return (
81-
await self.client.count(index=ITEMS_INDEX, body=search.to_dict(count=True))
82-
).get("count")
83-
8478
async def get_collection_items(
8579
self, collection_id: str, limit: int, base_url: str
8680
) -> Tuple[List[Item], Optional[int]]:
8781
"""Database logic to retrieve an ItemCollection and a count of items contained."""
8882
search = self.apply_collections_filter(Search(), [collection_id])
89-
items = await self.execute_search(search=search, limit=limit, base_url=base_url)
90-
maybe_count = await self.search_count(search)
83+
items, maybe_count = await self.execute_search(
84+
search=search, limit=limit, base_url=base_url
85+
)
9186

9287
return items, maybe_count
9388

@@ -199,20 +194,28 @@ def apply_sort(search: Search, field, direction):
199194
"""Database logic to sort search instance."""
200195
return search.sort({field: {"order": direction}})
201196

202-
async def execute_search(self, search, limit: int, base_url: str) -> List[Item]:
197+
async def execute_search(
198+
self, search, limit: int, base_url: str
199+
) -> Tuple[List[Item], Optional[int]]:
203200
"""Database logic to execute search with limit."""
204201
search = search[0:limit]
205202
body = search.to_dict()
206203

204+
maybe_count = (
205+
await self.client.count(index=ITEMS_INDEX, body=search.to_dict(count=True))
206+
).get("count")
207+
207208
es_response = await self.client.search(
208209
index=ITEMS_INDEX, query=body.get("query"), sort=body.get("sort")
209210
)
210211

211-
return [
212+
items = [
212213
self.item_serializer.db_to_stac(hit["_source"], base_url=base_url)
213214
for hit in es_response["hits"]["hits"]
214215
]
215216

217+
return items, maybe_count
218+
216219
""" TRANSACTION LOGIC """
217220

218221
async def check_collection_exists(self, collection_id: str):

0 commit comments

Comments
 (0)