Skip to content

Commit 0e2024b

Browse files
committed
Experiment with acquisition field in solr
1 parent 8d46fa4 commit 0e2024b

File tree

5 files changed

+45
-0
lines changed

5 files changed

+45
-0
lines changed

conf/solr/conf/managed-schema.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@
194194
<field name="time_facet" type="string" stored="false" multiValued="true"/>
195195
<field name="time_key" type="string" stored="false" multiValued="true"/>
196196

197+
<!-- only on edition -->
198+
<field name="acquisition" type="string" multiValued="true"/>
199+
197200
<!-- Ratings -->
198201
<field name="ratings_average" type="pfloat"/>
199202
<field name="ratings_sortable" type="pfloat"/>

openlibrary/book_providers.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,33 @@ def get_acquisitions(self, ed_or_solr: Edition | dict) -> list[Acquisition]:
594594
]
595595

596596

597+
class BetterWorldBooksProvider(AbstractBookProvider):
598+
short_name = 'betterworldbooks'
599+
long_name = 'Better World Books'
600+
identifier_key = 'betterworldbooks'
601+
602+
def is_own_ocaid(self, ocaid: str) -> bool:
603+
return False
604+
605+
def get_identifiers(self, ed_or_solr: Edition | dict) -> list[str]:
606+
# basically just check if it has an isbn?
607+
return (ed_or_solr.get('isbn_10') or []) + (ed_or_solr.get('isbn_13') or [])
608+
609+
def get_acquisitions(
610+
self,
611+
ed_or_solr: Edition | dict,
612+
) -> list[Acquisition]:
613+
return [
614+
Acquisition(
615+
access='buy',
616+
format='web',
617+
price=None,
618+
url=f'https://www.betterworldbooks.com/product/detail/{self.get_best_identifier(ed_or_solr)}',
619+
provider_name=self.short_name,
620+
)
621+
]
622+
623+
597624
PROVIDER_ORDER: list[AbstractBookProvider] = [
598625
# These providers act essentially as their own publishers, so link to the first when
599626
# we're on an edition page
@@ -607,6 +634,8 @@ def get_acquisitions(self, ed_or_solr: Edition | dict) -> list[Acquisition]:
607634
WikisourceProvider(),
608635
# Then link to IA
609636
InternetArchiveProvider(),
637+
# Then link to purchase options
638+
BetterWorldBooksProvider(),
610639
]
611640

612641

openlibrary/plugins/worksearch/schemes/works.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,10 @@ def remove_work_prefix(field: str) -> str:
325325
# Removes the indicator prefix from queries with the 'work field' before appending them to parameters.
326326
final_work_query = deepcopy(work_q_tree)
327327
luqum_replace_field(final_work_query, remove_work_prefix)
328+
EDITION_ONLY_FIELDS = {'acquisition'}
328329
try:
329330
luqum_remove_field(final_work_query, lambda f: f.startswith('edition.'))
331+
luqum_remove_field(final_work_query, lambda f: f in EDITION_ONLY_FIELDS)
330332
except EmptyTreeError:
331333
# If the whole tree is removed, we should just search for everything
332334
final_work_query = luqum_parser('*:*')
@@ -396,6 +398,7 @@ def remove_work_prefix(field: str) -> str:
396398
'ia_collection': 'ia_collection',
397399
'ia_box_id': 'ia_box_id',
398400
'public_scan_b': 'public_scan_b',
401+
'acquisition': 'acquisition',
399402
}
400403

401404
def convert_work_field_to_edition_field(

openlibrary/solr/solr_types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class SolrDocument(TypedDict):
6565
time: Optional[list[str]]
6666
time_facet: Optional[list[str]]
6767
time_key: Optional[list[str]]
68+
acquisition: Optional[list[str]]
6869
ratings_average: Optional[float]
6970
ratings_sortable: Optional[float]
7071
ratings_count: Optional[int]

openlibrary/solr/updater/edition.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,14 @@ def has_fulltext(self) -> bool:
293293
def public_scan_b(self) -> bool:
294294
return self.ebook_access == bp.EbookAccess.PUBLIC
295295

296+
def acquisition(self) -> list[str]:
297+
"""Get acquisition methods for the edition."""
298+
return [
299+
f'{acq.access},{acq.format},{acq.url}'
300+
for provider in self._providers
301+
for acq in provider.get_acquisitions(self._edition)
302+
]
303+
296304
def build(self) -> SolrDocument:
297305
"""
298306
Build the solr document for the given edition to store as a nested
@@ -339,6 +347,7 @@ def build(self) -> SolrDocument:
339347
'ia_collection': self.ia_collection,
340348
'ia_box_id': self.ia_box_id,
341349
# Ebook access
350+
'acquisition': self.acquisition(),
342351
'ebook_access': self.ebook_access.to_solr_str(),
343352
'ebook_provider': self.ebook_provider,
344353
'has_fulltext': self.has_fulltext,

0 commit comments

Comments
 (0)