Skip to content

Commit

Permalink
Merge pull request #9734 from merwhite11/1001/fulltext-search-box
Browse files Browse the repository at this point in the history
Fulltext Suggestion Macro for 0 Results
  • Loading branch information
mekarpeles authored Aug 20, 2024
2 parents 71425e6 + dbbde6e commit 078cdf8
Show file tree
Hide file tree
Showing 19 changed files with 571 additions and 35 deletions.
2 changes: 1 addition & 1 deletion bundlesize.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
},
{
"path": "static/build/page-user.css",
"maxSize": "27KB"
"maxSize": "27.05KB"
}
]
}
84 changes: 65 additions & 19 deletions openlibrary/i18n/messages.pot
Original file line number Diff line number Diff line change
Expand Up @@ -1030,17 +1030,13 @@ msgstr ""
msgid "Solr Editions"
msgstr ""

#: admin/inspect/store.html search/lists.html work_search.html
msgid "No results found."
msgstr ""

#: search/lists.html work_search.html
#: work_search.html
#, python-format
msgid "Search for books containing the phrase \"%s\"?"
msgid "No <strong>%(path_id)s</strong> directly matched your search"
msgstr ""

#: work_search.html
msgid "Add a new book to Open Library?"
#: search/authors.html search/lists.html search/subjects.html work_search.html
msgid "Checking Search Inside matches"
msgstr ""

#: account/reading_log.html search/authors.html search/subjects.html
Expand Down Expand Up @@ -1490,8 +1486,8 @@ msgstr ""
msgid "Thanks!"
msgstr ""

#: BookByline.html SearchResultsWork.html account/notes.html
#: account/observations.html
#: BookByline.html FulltextSearchSuggestionItem.html SearchResultsWork.html
#: account/notes.html account/observations.html
msgid "Unknown author"
msgstr ""

Expand Down Expand Up @@ -2668,6 +2664,10 @@ msgstr ""
msgid "Json"
msgstr ""

#: admin/inspect/store.html
msgid "No results found."
msgstr ""

#: admin/ip/index.html
msgid "[Admin Center] IP Addresses"
msgstr ""
Expand Down Expand Up @@ -3688,10 +3688,10 @@ msgstr ""
msgid "Links"
msgstr ""

#: IABook.html SearchResultsWork.html books/edition-sort.html
#: jsdef/LazyWorkPreview.html lists/list_overview.html lists/preview.html
#: lists/snippet.html lists/widget.html my_books/dropdown_content.html
#: type/work/editions.html
#: FulltextSearchSuggestionItem.html IABook.html SearchResultsWork.html
#: books/edition-sort.html jsdef/LazyWorkPreview.html lists/list_overview.html
#: lists/preview.html lists/snippet.html lists/widget.html
#: my_books/dropdown_content.html type/work/editions.html
#, python-format
msgid "Cover of: %(title)s"
msgstr ""
Expand Down Expand Up @@ -6050,22 +6050,21 @@ msgid "Merge authors"
msgstr ""

#: search/authors.html
msgid "No hits"
msgid "No <strong>authors</strong> directly matched your search"
msgstr ""

#: search/inside.html
#, python-format
msgid "Search Open Library for %s"
msgstr ""

#: BookSearchInside.html SearchNavigation.html search/inside.html
#: search/snippets.html
#: BookSearchInside.html FulltextSearchSuggestion.html SearchNavigation.html
#: search/inside.html search/snippets.html
msgid "Search Inside"
msgstr ""

#: search/inside.html
#, python-format
msgid "No hits for: %(query)s"
msgid "No <strong>Search Inside</strong> text matched your search"
msgstr ""

#: search/inside.html
Expand All @@ -6090,6 +6089,10 @@ msgstr ""
msgid "Search Lists"
msgstr ""

#: search/lists.html
msgid "No <strong>lists</strong> directly matched your search"
msgstr ""

#: search/publishers.html
msgid "Publishers Search"
msgstr ""
Expand Down Expand Up @@ -6155,6 +6158,10 @@ msgstr ""
msgid "Search Subjects"
msgstr ""

#: search/subjects.html
msgid "No <strong>subjects</strong> directly matched your search"
msgstr ""

#: search/subjects.html
msgid "time"
msgstr ""
Expand Down Expand Up @@ -7325,10 +7332,49 @@ msgstr ""
msgid "Expires"
msgstr ""

#: FulltextSearchSuggestion.html
msgid "Checking for Search Inside matches"
msgstr ""

#: FulltextSearchSuggestion.html
msgid "Search Inside Icon"
msgstr ""

#: FulltextSearchSuggestion.html
msgid "search inside icon"
msgstr ""

#: FulltextSearchSuggestion.html
#, python-format
msgid "%(book_count)s books found with matching passages"
msgstr ""

#: FulltextSearchSuggestion.html
#, python-format
msgid "See all %(results_count)s Search Inside Matches"
msgstr ""

#: FulltextSearchSuggestion.html
msgid "right chevron"
msgstr ""

#: FulltextSnippet.html
msgid "See All Results"
msgstr ""

#: FulltextSuggestionSnippet.html
msgid "❝"
msgstr ""

#: FulltextSuggestionSnippet.html
msgid "❞"
msgstr ""

#: FulltextSuggestionSnippet.html
#, python-format
msgid "Page: %(page)s"
msgstr ""

#: IABook.html
#, python-format
msgid "Borrowed from Internet Archive: %(title)s"
Expand Down
35 changes: 35 additions & 0 deletions openlibrary/macros/FulltextSearchSuggestion.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
$def with(query, results, page=1)

$def render_snippet(query, doc):
$:macros.FulltextSuggestionSnippet(query, doc=doc)

$if results and results.get('hits'):
$ hits = results['hits'].get('hits', [])[:4]
$ num_found = commify(results['hits'].get('total', 0))
$ ia_base_url = "https://archive.org"

$:macros.LoadingIndicator(_("Checking for Search Inside matches"))
<div id="fulltextSuggestions" class="fulltext-suggestions">
<div class="fulltext-suggestions__header">
<a class="fulltext-suggestions__icon" data-ol-link-track="SearchInsideSuggestion|SeeAllClick" href="/search/inside?$urlencode(dict(q=query))" title="$_('Search Inside Icon')">
<img src="/static/images/icons/icon_search-inside.svg"
width="30" height="30"
alt="$_('search inside icon')"/>
</a>
<h3 class="fulltext-suggestions__title">
<a data-ol-link-track="SearchInsideSuggestion|SeeAllClick"
href="/search/inside?$urlencode(dict(q=query))">$_("Search Inside")</a> &mdash; $_('%(book_count)s books found with matching passages', book_count=num_found)
</h3>
</div>
$for doc in hits:
$if doc.get('edition'):
$:macros.FulltextSearchSuggestionItem(doc['edition'], snippet=render_snippet(query, doc))
<h3 class="fulltext-suggestions__footer">
<a class="fulltext-suggestions__footer-link" data-ol-link-track="SearchInsideSuggestion|SeeAllClick"
href="/search/inside?$urlencode(dict(q=query))">$_('See all %(results_count)s Search Inside Matches', results_count=num_found)
<span class="fulltext-suggestions__right-chevron">
<img src="/static/images/icons/right-chevron.svg" alt="$_('right chevron')"/>
</span>
</a>
</h3>
</div>
91 changes: 91 additions & 0 deletions openlibrary/macros/FulltextSearchSuggestionItem.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
$def with (doc, snippet=None, attrs=None, blur=False)

$code:
max_rendered_authors = 9
doc_type = (
'infogami_work' if doc.get('type', {}).get('key') == '/type/work' else
'infogami_edition' if doc.get('type', {}).get('key') == '/type/edition' else
'solr_work' if not doc.get('editions') else
'solr_edition'
)

selected_ed = doc
if doc_type == 'solr_edition':
selected_ed = doc.get('editions')[0]

book_url = doc.url() if doc_type.startswith('infogami_') else doc.key
if doc_type == 'solr_edition':
work_edition_url = book_url + '?edition=' + urlquote('key:' + selected_ed.key)
else:
book_provider = get_book_provider(doc)
if book_provider and doc_type.endswith('_work'):
work_edition_url = book_url + '?edition=' + urlquote(book_provider.get_best_identifier_slug(doc))
else:
work_edition_url = book_url

work_edition_all_url = work_edition_url
if '?' in work_edition_url:
work_edition_all_url += '&mode=all'
else:
work_edition_all_url += '?mode=all'

edition_work = None
if doc_type == 'infogami_edition' and 'works' in doc:
edition_work = doc['works'][0]

full_title = selected_ed.get('title', '') + (': ' + selected_ed.subtitle if selected_ed.get('subtitle') else '')
if doc_type == 'infogami_edition' and edition_work:
full_work_title = edition_work.get('title', '') + (': ' + edition_work.subtitle if edition_work.get('subtitle') else '')
else:
full_work_title = doc.get('title', '') + (': ' + doc.subtitle if doc.get('subtitle') else '')

$ blur_cover = "fsi__bookcover-img-blur" if blur else ""

<div class="fsi" itemscope itemtype="https://schema.org/Book" $:attrs>
<div class="fsi__main">
<span class="fsi__book-cover">
$ cover = get_cover_url(selected_ed) or "/images/icons/avatar_book-sm.png"
<a data-ol-link-track="SearchInsideSuggestion|BookClick" href="$work_edition_url">
<img class="fsi__book-cover-img $blur_cover"
itemprop="image"
src="$cover"
alt="$_('Cover of: %(title)s', title=full_title)"
title="$_('Cover of: %(title)s', title=full_title)" />
</a>
</span>
<div class="fsi__book-details">
<div class="fsi__title-author">
<h3 class="fsi__book-title" itemprop="name">
<a itemprop="url" data-ol-link-track="SearchInsideSuggestion|BookClick" href="$work_edition_url">$full_title</a>
</h3>
<p class="fsi__book-author" itemprop="author" itemscope itemtype="https://schema.org/Organization">
$ authors = None
$if doc_type == 'infogami_work':
$ authors = doc.get_authors()
$elif doc_type == 'infogami_edition':
$ authors = edition_work.get_authors() if edition_work else doc.get_authors()
$elif doc_type.startswith('solr_'):
$if 'authors' in doc:
$ authors = doc['authors']
$elif 'author_key' in doc:
$ authors = [ { 'key': '/authors/' + key, 'name': name } for key, name in zip(doc['author_key'], doc['author_name']) ]
$if not authors:
<em>$_('Unknown author')</em>
$else:
$code:
author_data = [
{
'name': a.get('name') or a.get('author', {}).get('name'),
'url': (a.get('url') or a.get('key') or a.get('author', {}).get('url') or a.get('author', {}).get('key'))
}
for a in authors
]
$:macros.BookByline(author_data, limit=max_rendered_authors, overflow_url=work_edition_url, attrs='class="results"')
</p>
</div>
$if snippet:
$:snippet
</div>
</div>
</div>

26 changes: 26 additions & 0 deletions openlibrary/macros/FulltextSuggestionSnippet.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
$def with (q, doc=None)

$ ia = doc.get('fields', {}).get('identifier', [''])[0]
$ ia_base_url = "https://archive.org"
$ availability = doc.get('availability', {})
$ snippet = doc.get('highlight', {}).get('text', [''])[0]
$ page_nums = doc.get('fields', {}).get('page_num', [])
$if len(page_nums) == 1 and isinstance(page_nums[0], list):
$ page_nums = page_nums[0]
$ page = ', '.join(str(num) for num in page_nums)

$if snippet:
<div class="fsi-snippet">
$if snippet:
<div class="fsi-snippet__main">
<div class="fsi-snippet__quotation-mark">$_('❝')</div>
<a class="fsi-snippet__link" href="$ia_base_url/details/$ia" data-ol-link-track="SearchInsideSuggestion|QuoteClick">
&hellip;$:(snippet.replace("<", "&laquo;").replace(">", "&raquo;").replace("{{{", "<mark class='highlight'><strong>").replace("}}}", "</strong></mark>"))&hellip;
</a>&nbsp;
<div class="fsi-snippet__quotation-mark">$_('❞')</div>
$if page:
<div class="fsi-snippet__page-num" href="$ia_base_url/details/$ia">
<a data-ol-link-track="SearchInsideSuggestion|QuoteClick">$_('Page: %(page)s', page=page)</a>
</div>
</div>
</div>
14 changes: 14 additions & 0 deletions openlibrary/plugins/openlibrary/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from openlibrary.utils.isbn import isbn_13_to_isbn_10, isbn_10_to_isbn_13, canonical
from openlibrary.core.models import Edition
from openlibrary.core.lending import get_availability
from openlibrary.core.fulltext import fulltext_search
import openlibrary.core.stats
from openlibrary.plugins.openlibrary.home import format_work_data
from openlibrary.plugins.openlibrary.stats import increment_error_count
Expand Down Expand Up @@ -1114,6 +1115,7 @@ def GET(self):
args[0], args[1]
)
partial = {"partials": str(macro)}

elif component == 'SearchFacets':
data = json.loads(i.data)
path = data.get('path')
Expand Down Expand Up @@ -1150,6 +1152,18 @@ def GET(self):
"activeFacets": str(active_facets).strip(),
}

elif component == "FulltextSearchSuggestion":
query = i.get('data', '')
data = fulltext_search(query)
hits = data.get('hits', [])
if not hits['hits']:
macro = '<div></div>'
else:
macro = web.template.Template.globals[
'macros'
].FulltextSearchSuggestion(query, data)
partial = {"partials": str(macro)}

return delegate.RawText(json.dumps(partial))


Expand Down
Loading

0 comments on commit 078cdf8

Please sign in to comment.