Skip to content

Commit cbd08db

Browse files
committed
refactor: Migrate work_search to an async-bridged implementation with highlight support and update request context access to site.get().
1 parent 365b825 commit cbd08db

File tree

2 files changed

+9
-37
lines changed

2 files changed

+9
-37
lines changed

openlibrary/plugins/worksearch/code.py

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,40 +1087,6 @@ def _prepare_work_search_query(
10871087

10881088

10891089
# Note: these could share an "implementation" to keep a common interface but it creates a lot more complexity
1090-
# Warning: when changing this please also change the async version
1091-
@public
1092-
def work_search(
1093-
query: dict,
1094-
sort: str | None = None,
1095-
page: int = 1,
1096-
offset: int = 0,
1097-
limit: int = 100,
1098-
fields: str = '*',
1099-
facet: bool = True,
1100-
highlight: bool = False,
1101-
spellcheck_count: int | None = None,
1102-
request_label: SolrRequestLabel = 'UNLABELLED',
1103-
) -> dict:
1104-
prepared = _prepare_work_search_query(query, page, offset, limit)
1105-
1106-
resp = run_solr_query(
1107-
WorkSearchScheme(),
1108-
prepared.query,
1109-
rows=prepared.limit,
1110-
page=prepared.page,
1111-
sort=sort,
1112-
offset=prepared.offset,
1113-
fields=fields,
1114-
facet=facet,
1115-
highlight=highlight,
1116-
spellcheck_count=spellcheck_count,
1117-
request_label=request_label,
1118-
)
1119-
1120-
return _process_solr_search_response(resp, fields)
1121-
1122-
1123-
# Warning: when changing this please also change the sync version
11241090
@public
11251091
async def work_search_async(
11261092
query: dict,
@@ -1130,6 +1096,7 @@ async def work_search_async(
11301096
limit: int = 100,
11311097
fields: str | list[str] = '*',
11321098
facet: bool = True,
1099+
highlight: bool = False,
11331100
spellcheck_count: int | None = None,
11341101
request_label: SolrRequestLabel = 'UNLABELLED',
11351102
lang: str | None = None,
@@ -1145,13 +1112,19 @@ async def work_search_async(
11451112
offset=prepared.offset,
11461113
fields=fields,
11471114
facet=facet,
1115+
highlight=highlight,
11481116
spellcheck_count=spellcheck_count,
11491117
request_label=request_label,
11501118
)
11511119

11521120
return _process_solr_search_response(resp, fields)
11531121

11541122

1123+
work_search = async_bridge.wrap(work_search_async)
1124+
work_search.__name__ = 'work_search'
1125+
public(work_search)
1126+
1127+
11551128
def validate_search_json_query(q: str | None) -> str | None:
11561129
if q and len(q) < 3:
11571130
return 'Query too short, must be at least 3 characters'

openlibrary/plugins/worksearch/schemes/works.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from typing import Any, cast
88

99
import luqum.tree
10-
import web
1110
from typing_extensions import deprecated
1211

1312
import infogami
@@ -35,7 +34,7 @@
3534
normalize_lcc_range,
3635
short_lcc_to_sortable_lcc,
3736
)
38-
from openlibrary.utils.request_context import req_context
37+
from openlibrary.utils.request_context import req_context, site
3938

4039
logger = logging.getLogger("openlibrary.worksearch")
4140
re_author_key = re.compile(r'(OL\d+A)')
@@ -632,7 +631,7 @@ def add_non_solr_fields(
632631
for ed_doc in doc.get('editions', {}).get('docs', [])
633632
]
634633

635-
things = cast(list[Work | Edition], web.ctx.site.get_many(keys))
634+
things = cast(list[Work | Edition], site.get().get_many(keys))
636635
key_to_thing = {t.key: t for t in things if t.key in keys}
637636

638637
from openlibrary.book_providers import get_acquisitions

0 commit comments

Comments
 (0)