$def with (input, q_param, do_search, get_doc, get_availability_of_ocaids, fulltext_search, facet_fields) $ fulltext_names = {'true': 'Ebooks', 'false': 'Exclude ebooks'} $def add_facet_url(k, v): $if k != 'has_fulltext': $changequery(page=None,**{k:param.get(k, []) + [v]}) $else: $changequery(page=None,**{k:v}) $def del_facet_url(k, v): $if k != 'has_fulltext': $changequery(page=None,**{k:[i for i in param.get(k, []) if i != v]}) $else: $changequery(page=None,**{k:None}) $ 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
$if param and not error and num_found:
$if num_found > 0: $ungettext('1 hit', '%(count)s hits', num_found, count=commify(num_found))   $if num_found > 1: $_('Sorting by') $code: sort_options = [ { 'sort': None, 'name': _("Relevance"), 'ga_key': 'Relevance' }, { 'sort': 'editions', 'name': _("Most Editions"), 'ga_key': 'Editions' }, { 'sort': 'old', 'name': _("First Published"), 'ga_key': 'Old' }, { 'sort': 'new', 'name': _("Most Recent"), 'ga_key': 'New' }, { 'sort': 'random', 'name': _("Random"), 'ga_key': 'Random', 'selected': sort and sort.startswith('random') }, ] $for sort_option in sort_options: $ is_selected = sort_option.get('selected') or sort_option['sort'] == sort $if is_selected: $sort_option['name'] $else: $sort_option['name'] $if not loop.last: | $if sort and sort.startswith('random'): ($_('Shuffle')) $_('Advanced Search')
$ 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')), $ )
$ sticky = set(['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)
$elif param and not error and len(docs):
$ungettext('1 hit', '%(count)s hits', num_found, count=commify(num_found))
    $ works = add_availability([get_doc(d) for d in docs]) $for work in works: $ ocaid = work.ia[0] if work.ia else None $ availability = (work.get('availability') or {}).get('status') $# if we're explicitly showing *everything*... $# or if we're showing only things with ocaids which are available... $if 'has_fulltext' not in param or (ocaid and availability and availability not in ['error', 'private']): $:macros.SearchResultsWork(work)
$: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): $ wks = '|'.join([w.key for w in works]) $if ctx.user and ctx.user.is_admin():

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