@@ -75,19 +75,14 @@ async def get_all_collections(self, base_url: str) -> List[Collection]:
75
75
for c in collections ["hits" ]["hits" ]
76
76
]
77
77
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
-
84
78
async def get_collection_items (
85
79
self , collection_id : str , limit : int , base_url : str
86
80
) -> Tuple [List [Item ], Optional [int ]]:
87
81
"""Database logic to retrieve an ItemCollection and a count of items contained."""
88
82
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
+ )
91
86
92
87
return items , maybe_count
93
88
@@ -199,20 +194,28 @@ def apply_sort(search: Search, field, direction):
199
194
"""Database logic to sort search instance."""
200
195
return search .sort ({field : {"order" : direction }})
201
196
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 ]]:
203
200
"""Database logic to execute search with limit."""
204
201
search = search [0 :limit ]
205
202
body = search .to_dict ()
206
203
204
+ maybe_count = (
205
+ await self .client .count (index = ITEMS_INDEX , body = search .to_dict (count = True ))
206
+ ).get ("count" )
207
+
207
208
es_response = await self .client .search (
208
209
index = ITEMS_INDEX , query = body .get ("query" ), sort = body .get ("sort" )
209
210
)
210
211
211
- return [
212
+ items = [
212
213
self .item_serializer .db_to_stac (hit ["_source" ], base_url = base_url )
213
214
for hit in es_response ["hits" ]["hits" ]
214
215
]
215
216
217
+ return items , maybe_count
218
+
216
219
""" TRANSACTION LOGIC """
217
220
218
221
async def check_collection_exists (self , collection_id : str ):
0 commit comments