Skip to content

Commit 583b959

Browse files
committed
account for > 10,000 hits
1 parent 609b178 commit 583b959

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,15 +574,32 @@ async def execute_search(
574574
)
575575
)
576576

577+
count_task = asyncio.create_task(
578+
self.client.count(
579+
index=index_param,
580+
ignore_unavailable=ignore_unavailable,
581+
body=search.to_dict(count=True),
582+
)
583+
)
584+
577585
try:
578586
es_response = await search_task
579587
except exceptions.NotFoundError:
580588
raise NotFoundError(f"Collections '{collection_ids}' do not exist")
581589

582-
matched = es_response["hits"]["total"]["value"]
583590
hits = es_response["hits"]["hits"]
584591
items = (hit["_source"] for hit in hits)
585592

593+
matched = es_response["hits"]["total"]["value"]
594+
if es_response["hits"]["total"]["relation"] != "eq":
595+
if count_task.done():
596+
try:
597+
matched = count_task.result().get("count")
598+
except Exception as e:
599+
logger.error(f"Count task failed: {e}")
600+
else:
601+
count_task.cancel()
602+
586603
next_token = None
587604
if matched > page * limit:
588605
if hits and (sort_array := hits[-1].get("sort")):

stac_fastapi/opensearch/stac_fastapi/opensearch/database_logic.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,14 +605,31 @@ async def execute_search(
605605
)
606606
)
607607

608+
count_task = asyncio.create_task(
609+
self.client.count(
610+
index=index_param,
611+
ignore_unavailable=ignore_unavailable,
612+
body=search.to_dict(count=True),
613+
)
614+
)
615+
608616
try:
609617
es_response = await search_task
610618
except exceptions.NotFoundError:
611619
raise NotFoundError(f"Collections '{collection_ids}' do not exist")
612620

613621
hits = es_response["hits"]["hits"]
614622
items = (hit["_source"] for hit in hits)
623+
615624
matched = es_response["hits"]["total"]["value"]
625+
if es_response["hits"]["total"]["relation"] != "eq":
626+
if count_task.done():
627+
try:
628+
matched = count_task.result().get("count")
629+
except Exception as e:
630+
logger.error(f"Count task failed: {e}")
631+
else:
632+
count_task.cancel()
616633

617634
next_token = None
618635
if matched > page * limit:

0 commit comments

Comments
 (0)