Skip to content

Commit f0fb363

Browse files
Sadashiipre-commit-ci[bot]RayBB
authored
Make search facets fully asynchronous (#12262)
* Make search facets fully asynchronous by updating search functions and imports * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add missing await in searchfacets partials * Update parameters for async update * Add tests for /partials/SearchFacets.json endpoint with async mocks * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Refactor SearchFacetsPartial to support asynchronous generation * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * precommit on upgrading searchfacetspartial to async * Revert unintended vendor/infogami submodule update * Delete openlibrary/tests/fastapi/test_search_facets_partial.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Raymond Berger <RayBB@users.noreply.github.com>
1 parent c826a23 commit f0fb363

3 files changed

Lines changed: 15 additions & 10 deletions

File tree

openlibrary/fastapi/partials.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,13 @@ async def search_facets_partial(
4242
- param: dict with search parameters (q, author_key, subject_facet, etc.)
4343
- path: str (e.g., '/search')
4444
- query: str (e.g., '?q=python')
45-
# TODO: Make this fully async
4645
"""
4746
try:
4847
parsed_data = json.loads(data)
4948
except json.JSONDecodeError:
5049
raise HTTPException(status_code=400, detail="Invalid JSON in data parameter")
5150

52-
return SearchFacetsPartial(data=parsed_data, sfw=sfw == "yes").generate()
51+
return await SearchFacetsPartial(data=parsed_data, sfw=sfw == "yes").generate_async()
5352

5453

5554
@router.get("/partials/AffiliateLinks.json", include_in_schema=SHOW_PARTIALS_IN_SCHEMA)

openlibrary/plugins/openlibrary/partials.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from openlibrary.i18n import gettext as _
1313
from openlibrary.plugins.openlibrary.lists import get_lists_async, get_user_lists
1414
from openlibrary.plugins.upstream.yearly_reading_goals import get_reading_goals
15-
from openlibrary.plugins.worksearch.code import do_search, work_search
15+
from openlibrary.plugins.worksearch.code import do_search_async, work_search
1616
from openlibrary.plugins.worksearch.subjects import (
1717
date_range_to_publish_year_filter,
1818
get_subject,
@@ -218,13 +218,16 @@ def __init__(self, data: dict, sfw: bool = False):
218218
)
219219

220220
def generate(self) -> dict:
221+
raise NotImplementedError("Use generate_async instead")
222+
223+
async def generate_async(self) -> dict:
221224
path = self.data.get('path')
222225
query = self.data.get('query', '')
223226
parsed_qs = parse_qs(query.replace('?', ''))
224227
param = self.data.get('param', {})
225228

226229
sort = None
227-
search_response = do_search(
230+
search_response = await do_search_async(
228231
param,
229232
sort,
230233
rows=0,

openlibrary/plugins/worksearch/code.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ def get_matches(fragment: str) -> set[str]:
544544
return highlighting or None
545545

546546

547-
def do_search(
547+
async def do_search_async(
548548
param: dict,
549549
sort: str | None,
550550
page=1,
@@ -576,13 +576,13 @@ def do_search(
576576
if sfw:
577577
fields |= {'subject'}
578578

579-
return run_solr_query(
579+
return await run_solr_query_async(
580580
WorkSearchScheme(),
581581
param,
582-
rows,
583-
page,
584-
sort,
585-
spellcheck_count,
582+
rows=rows,
583+
page=page,
584+
sort=sort,
585+
spellcheck_count=spellcheck_count,
586586
fields=list(fields),
587587
facet=facet,
588588
highlight=highlight,
@@ -591,6 +591,9 @@ def do_search(
591591
)
592592

593593

594+
do_search = async_bridge.wrap(do_search_async)
595+
596+
594597
def get_doc(doc: SolrDocument):
595598
"""
596599
Coerce a solr document to look more like an Open Library edition/work. Ish.

0 commit comments

Comments
 (0)