$def with (input, q_param, do_search, get_doc, fulltext_search, facet_fields) $ fulltext_names = {'true': 'Ebooks', 'false': 'Exclude ebooks'} $code: def add_facet_url(k, v): if k != 'has_fulltext': return changequery(page=None,**{k:param.get(k, []) + [v]}) else: return changequery(page=None,**{k:v}) def del_facet_url(k, v): if k != 'has_fulltext': return changequery(page=None,**{k:[i for i in param.get(k, []) if i != v]}) else: return changequery(page=None,**{k:None}) def add_track(key: str) -> str: """ KeyError will be raised if key is not in the following dict. """ facets = { "has_fulltext": "Ebook", "author_key": "Author", "subject_facet": "Subjects", "person_facet": "People", "place_facet": "Places", "time_facet": "Times", "first_publish_year": "FirstPublished", "publisher_facet": "Publisher", "language": "Language", } return "SearchFacet|" + facets[key] $ param = {} $for p in ['q', 'title', 'author', 'page', 'sort', 'isbn', 'oclc', 'contributor', 'publish_place', 'lccn', 'ia', 'first_sentence', 'publisher', 'author_key', 'debug', 'subject', 'place', 'person', 'time'] + facet_fields: $if p in input and input[p]: $ param[p] = input[p] $if list(param) == ['has_fulltext']: $ param = {} $ error = None $if param: $ page = int(param.get('page', 1)) $ sort = param.get('sort', None) $ rows = 20 $ search_start = time() $ results = do_search(param, sort, page, rows=rows, spellcheck_count=3) $ search_secs = time() - search_start $ docs = results.docs $ facet_counts = results.facet_counts $ num_found = results.num_found $ error = results.error $else: $ num_found = 0 $ start_facet_count = 5 $ facet_inc = 10

$_("Search Books")

$ facet_map = ( $ ('has_fulltext', _('eBook?')), $ ('author_key', _('Author')), $ ('subject_facet', _('Subjects')), $ ('person_facet', _('People')), $ ('place_facet', _('Places')), $ ('time_facet', _('Times')), $ ('first_publish_year', _('First published')), $ ('publisher_facet', _('Publisher')), $ ('language', _('Language')), $ ('public_scan_b', _('Classic eBooks')), $ )
$:macros.SearchNavigation()
$if ctx.user and (ctx.user.is_admin() or ctx.user.is_usergroup_member('/usergroup/super-librarians')):
$ sticky = set(['sort', 'author_facet', 'language', 'first_publish_year', 'publisher_facet', 'subject_facet', 'person_facet', 'place_facet', 'time_facet', 'public_scan_b']) $for k, values in param.items(): $if k not in sticky: $continue $for v in values if isinstance(values, list) else [values]:
$if param and not error: $ title = [] $if q_param: $title.append(q_param) $if 'has_fulltext' in param: $title.append(_('eBook')) $if 'public_scan_b' in param: $title.append(_('Classic eBook')) $if any(header in param for header, label in facet_map):

Search facets $for header, label in facet_map: $ counts = facet_counts[header] $for k, display, count in counts: $if k not in param.get(header, []): $continue $if header not in ['has_fulltext', 'public_scan_b']: $title.append(display) $if header == 'has_fulltext': $fulltext_names.get(k, '') $elif header == 'public_scan_b': $if display == 'true': $_('Only Classic eBooks') $else: Classic eBooks hidden $elif header == 'subject_facet': $display $elif header == 'person_facet': $display $elif header == 'place_facet': $display $elif header == 'time_facet': $display $elif header == 'first_publish_year': $display $elif header == 'language': $display $elif header == 'publisher_facet': $display $elif header == 'author_key': $display [x]

$var title: $_('%(title)s - search', title=', '.join(title))
$if param and not docs: $ query = query_param('q') $ page = int(query_param('page') or 1) $# Temporarily (2020-03-26) disable automatically showing full-text $# results because of performance issues due to increased load. See $# this commit to revert.
$_("No results found.")

$_('Search for books containing the phrase "%s"?' % query)

$_("Add a new book to Open Library?")
$elif param and not error and len(docs):
$ungettext('1 hit', '%(count)s hits', num_found, count=commify(num_found)) $if num_found > 1: $:render_template("search/sort_options.html", sort)
    $ works = [get_doc(d) for d in docs] $ add_availability([(w.get('editions') or [None])[0] or w for w in works]) $ username = ctx.user and ctx.user.key.split('/')[-1] $if username: $ works = add_read_statuses(username, works) $ show_librarian_extras = ctx.user and (ctx.user.is_admin() or ctx.user.is_usergroup_member('/usergroup/librarians')) $for work in works: $ read_status = work.get('readinglog', None) $ dropper = macros.ReadingLogDropper([], work=work, reading_log_only=True, page_url="/search", users_work_read_status=read_status) $:macros.SearchResultsWork(work, reading_log=dropper, show_librarian_extras=show_librarian_extras)
$:macros.Pager(page, num_found, rows)
$if error:

BARF! Search engine ERROR!

$error.decode('utf-8', 'ignore')
$elif param and len(docs):

$_("Zoom In")

$_("Focus your results using these") $_("filters")
$for header, label in facet_map: $if header=='has_fulltext' and 'has_fulltext' in param: $continue $if header=='public_scan_b': $continue $ counts = [i for i in facet_counts[header] if i[0] not in param.get(header, [])] $if not counts: $continue
$if header == 'author_key' and len(counts) > 1 and ctx.user and ("merge-authors" in ctx.features or ctx.user.is_admin()): $ keys = '&'.join('key=%s' % k for k, display, count in counts)

$label $_('Merge duplicates')

$else:

$label

$ num = 0 $for k, display, count in counts: $ num = num + 1 $if num <= start_facet_count:
$else:
$if header == 'has_fulltext': $if display == 'yes': $ display = _('yes') $else: $ display = _('no') $display $commify(count) $else: $display  $commify(count)
$if len(counts) > start_facet_count:
$if param and not error and len(docs): $if ctx.user and ctx.user.is_admin():

Searching solr took $("%.2f" % search_secs) seconds