Skip to content

Commit

Permalink
WIP: table of contents search
Browse files Browse the repository at this point in the history
  • Loading branch information
mekarpeles authored and root committed Oct 20, 2018
1 parent 59936ee commit aadb35e
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 8 deletions.
52 changes: 51 additions & 1 deletion openlibrary/core/fulltext.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,57 @@
from infogami import config
from openlibrary.core.lending import get_availability_of_ocaids

logger = logging.getLogger("openlibrary.inside")

def toc_search_api(q, page=1, limit=50):
if not hasattr(config, 'plugin_inside'):
return {'error': 'Unable to prepare search engine'}
offset = (page - 1) * limit
params = {
'limit': limit,
'q': q,
'nofacets': 'true',
'olonly': 'true',
'fields': 'toc',
'from': offset,
'size': limit
}

toc_search_url = '%s?%s' % (
config.toc_search['toc_endpoint'],
urllib.urlencode(params, 'utf-8'))

try:
json_data = urllib2.urlopen(toc_search_url, timeout=30).read()
except:
return {'error': 'Unable to query toc search engine'}

try:
return simplejson.loads(json_data)
except:
return {
'error': 'Error converting toc search engine data to JSON'
}

def toc_search(q, page=1, limit=50):
ia_results = toc_search_api(q, page=page, limit=limit)

if 'error' not in ia_results and ia_results['hits']:
hits = ia_results['hits'].get('hits', [])
ocaids = [hit['_source'].get('meta_identifier', '') for hit in hits]
availability = get_availability_of_ocaids(ocaids)
if 'error' in availability:
return []
editions = web.ctx.site.get_many([
'/books/%s' % availability[ocaid].get('openlibrary_edition')
for ocaid in availability
if availability[ocaid].get('openlibrary_edition')])
for ed in editions:
idx = ocaids.index(ed.ocaid)
ia_results['hits']['hits'][idx]['edition'] = ed
ia_results['hits']['hits'][idx]['availability'] = availability[ed.ocaid]
return ia_results


def fulltext_search_api(params):
if not hasattr(config, 'plugin_inside'):
Expand All @@ -17,7 +68,6 @@ def fulltext_search_api(params):

try:
json_data = urllib2.urlopen(search_select, timeout=30).read()
logger = logging.getLogger("openlibrary.inside")
logger.debug('URL: ' + search_select)
except:
return {'error': 'Unable to query search engine'}
Expand Down
4 changes: 2 additions & 2 deletions openlibrary/macros/FulltextResults.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
$ num_found = results['hits'].get('total', 0)
<div id="searchResults">
<ul id="siteSearch">
$for doc in hits:
$for doc in hits:
$if doc.get('edition'):
$ snippet = macros.FulltextSnippet(query, doc=doc)
$:macros.SearchResultsWork(doc['edition'], availability=doc['availability'], user=user, extra=snippet)
</ul>
$:macros.Pager(page, num_found)
</div>
</div>
18 changes: 18 additions & 0 deletions openlibrary/macros/TocResults.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
$def with(query, results, page=1)

$if results and results.get('hits'):
$ loans = ctx.user.get_loans() if ctx.user else []
$ waiting_loans = ctx.user.get_waitinglist() if ctx.user else []
$ user = {'loans': loans, 'waitlists': waiting_loans}

$ hits = results['hits'].get('hits', [])
$ num_found = results['hits'].get('total', 0)
<div id="searchResults">
<ul id="siteSearch">
$for doc in hits:
$if doc.get('edition'):
$ toc = macros.TocSnippet(query, doc=doc)
$:macros.SearchResultsWork(doc['edition'], availability=doc['availability'], user=user, extra=toc)
</ul>
$:macros.Pager(page, num_found)
</div>
9 changes: 9 additions & 0 deletions openlibrary/macros/TocSnippet.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
$def with (q, doc=None)

$ toc = doc['_source']['toc']
<section class="fulltext-excerpts" aria-label="search results from: $doc['edition']['title']">
<pre>
$for sec in toc:
$sec.split(':', 1)[1]
</pre>
</section>
19 changes: 18 additions & 1 deletion openlibrary/plugins/inside/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,28 @@
import web
from infogami.utils import delegate
from infogami.utils.view import render_template
from openlibrary.core.fulltext import fulltext_search
from openlibrary.core.fulltext import fulltext_search, toc_search


RESULTS_PER_PAGE = 20

class search_toc(delegate.page):

path = '/search/toc'

def GET(self):
search_start = time() # should probably use a @timeit decorator
i = web.input(q='', page=1)
query = i.q
page = int(i.page)
results = toc_search(query, page=page, limit=RESULTS_PER_PAGE)
search_time = time() - search_start

page = render_template('search/toc.tmpl', query, results, search_time,
page=page, results_per_page=RESULTS_PER_PAGE)
page.v2 = True # page is mobile-first
return page

class search_inside(delegate.page):

path = '/search/inside'
Expand Down
4 changes: 0 additions & 4 deletions openlibrary/plugins/worksearch/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,10 +464,6 @@ def GET(self):

if i.get('ftokens') and ',' not in i.ftokens:
token = i.ftokens
#if ftoken_db is None:
# ftoken_db = dbm.open('/olsystem/ftokens', 'r')
#if ftoken_db.get(token):
# raise web.seeother('/subjects/' + ftoken_db[token].decode('utf-8').lower().replace(' ', '_'))

if i.get('wisbn'):
i.isbn = i.wisbn
Expand Down
32 changes: 32 additions & 0 deletions openlibrary/templates/search/toc.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
$def with (q, results, search_time, page=1, results_per_page=20)

$var title: Search Open Library for $q

<div id="contentHead">
<h1>$_("Search Tables of Contents")</h1>
</div>

<div id="contentBody">

<form class="siteSearch searchInsideForm" action="/search/toc">
<input type="text" class="larger" name="q" value="$q"/>
<input type="submit" class="generic-button" value="$_('Search')">
</form>
$ num_found = 0
$if q:
$if results and 'error' not in results and results['hits']:
$ hits = results['hits'].get('hits', [])
$ num_found = results['hits'].get('total', 0)

$if 'error' in results:
<div class="searchResultsError">$results['error']</div>

$if not num_found:
<p class="sansserif red collapse">No hits for: <strong>$q</strong></span></p>

$else:
<p class="search-results-stats">About $commify(num_found) result$("s" if num_found != 1 else "") ($("%.2f" % search_time) seconds)</p>

$:macros.TocResults(q, results, page=page)
</div>

0 comments on commit aadb35e

Please sign in to comment.